threads / discuss / 51759

How to build to debug with gdb?

Subject: How to build to debug with gdb?

## tl;dr

9 messages between Aug 27, 2019 and Aug 28, 2019.

replies: 8people: 5as markdown or json

Giuseppe Crino'· Aug 27, 2019, 16:27 UTC · lore
Hello, to debug some issues I built and installed git via

$ make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g" $ sudo make install $ git --version # git version 2.23.0.40.g4d8aada92f

But it seems there's still some optimization going on that prevents gdb from working correctly.
For example

(gdb) b builtin/config.c:752 Breakpoint 1 at 0x43942: file builtin/config.c, line 752. (gdb) r config --global --edit Starting program: /usr/local/bin/git config --global --edit [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libth read_db.so.1".

Breakpoint 1, cmd_config (argc=0, argv=<optimized out>,
    prefix=<optimized out>) at builtin/config.c:753
    753                             if (fd >= 0) {
    (gdb) p fd
    $1 = <optimized out>
 What am I missing?
 -Giuseppe
Matheus Tavares Bernardino· Aug 27, 2019, 17:55 UTC · re: Giuseppe Crino' · lore

Re: How to build to debug with gdb?

On Tue, Aug 27, 2019 at 1:27 PM Giuseppe Crino' <giuscri@gmail.com> wrote:
Show 6 quoted lines
>
> Hello, to debug some issues I built and installed git via
>
> $ make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g"
> $ sudo make install
> $ git --version # git version 2.23.0.40.g4d8aada92f

Hmm, could it be perhaps that CFLAGS is being overwritten? To debug Git with GDB I always add this to my config.mak file:

CFLAGS += -g3 -O0 LDFLAGS += -g3 -O0

And then just compile with `make`. Maybe give it a try?
Show 21 quoted lines
> But it seems there's still some optimization going on that prevents gdb from working correctly.
>
> For example
>
> (gdb) b builtin/config.c:752
> Breakpoint 1 at 0x43942: file builtin/config.c, line 752.
> (gdb) r config --global --edit
> Starting program: /usr/local/bin/git config --global --edit
> [Thread debugging using libthread_db enabled]
> Using host libthread_db library "/lib/x86_64-linux-gnu/libth
> read_db.so.1".
>
> Breakpoint 1, cmd_config (argc=0, argv=<optimized out>,
>     prefix=<optimized out>) at builtin/config.c:753
>     753                             if (fd >= 0) {
>     (gdb) p fd
>     $1 = <optimized out>
>
>  What am I missing?
>
>  -Giuseppe
Randall S. Becker· Aug 27, 2019, 18:07 UTC · re: Matheus Tavares Bernardino · lore

RE: How to build to debug with gdb?

On August 27, 2019 1:56 PM, Matheus Tavares Bernardino wrote:
Show 35 quoted lines
> On Tue, Aug 27, 2019 at 1:27 PM Giuseppe Crino' <giuscri@gmail.com>
> wrote:
> > Hello, to debug some issues I built and installed git via
> >
> > $ make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g"
> > $ sudo make install
> > $ git --version # git version 2.23.0.40.g4d8aada92f
> 
> Hmm, could it be perhaps that CFLAGS is being overwritten? To debug Git
> with GDB I always add this to my config.mak file:
> 
> CFLAGS += -g3 -O0
> LDFLAGS += -g3 -O0
> 
> And then just compile with `make`. Maybe give it a try?
> 
> > But it seems there's still some optimization going on that prevents gdb
> from working correctly.
> >
> > For example
> >
> > (gdb) b builtin/config.c:752
> > Breakpoint 1 at 0x43942: file builtin/config.c, line 752.
> > (gdb) r config --global --edit
> > Starting program: /usr/local/bin/git config --global --edit [Thread
> > debugging using libthread_db enabled] Using host libthread_db library
> > "/lib/x86_64-linux-gnu/libth read_db.so.1".
> >
> > Breakpoint 1, cmd_config (argc=0, argv=<optimized out>,
> >     prefix=<optimized out>) at builtin/config.c:753
> >     753                             if (fd >= 0) {
> >     (gdb) p fd
> >     $1 = <optimized out>
> >
> >  What am I missing?
Given that you are 40 commits in from 2.23.0, is it possible that you have compiled a few objects, like main, using the old CFLAGS? Gdb is notorious for not showing symbols if main does not have it. Have you done a git clean -dxf before building with the new CFLAGS?
Just a thought.
Randall
Johannes Sixt· Aug 27, 2019, 18:34 UTC · re: Giuseppe Crino' · lore

Re: How to build to debug with gdb?

Am 27.08.19 um 18:27 schrieb Giuseppe Crino':
Show 7 quoted lines
> Hello, to debug some issues I built and installed git via
> 
> $ make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g"
> $ sudo make install
> $ git --version # git version 2.23.0.40.g4d8aada92f 
> 
> But it seems there's still some optimization going on that prevents gdb from working correctly.

That is because the command sequence above does not do what you think it does. Didn't you notice that everything was recompiled during `sudo make install`?

You must run
  sudo make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g" install
-- Hannes
Giuseppe Crino'· Aug 28, 2019, 08:15 UTC · re: Johannes Sixt · lore

Re: How to build to debug with gdb?

On Tue, Aug 27, 2019 at 08:36:40PM +0200, SZEDER Gábor wrote:
> Try using the same build flags for the install, i.e.:
> 
>   make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g" install
Yes, now it works thanks!
On Tue, Aug 27, 2019 at 08:34:23PM +0200, Johannes Sixt wrote:
> That is because the command sequence above does not do what you think it
> does. Didn't you notice that everything was recompiled during `sudo make
> install`?
Shouldn't be this documented somewhere?

To my knowledge `make install` is expected to copy artifacts of the build under prefix. It's unusual to me that `install` has the `all` target as prerequisite.

SZEDER Gábor· Aug 28, 2019, 10:32 UTC · re: Giuseppe Crino' · lore

Re: How to build to debug with gdb?

On Wed, Aug 28, 2019 at 08:15:57AM +0000, Giuseppe Crino' wrote:
Show 13 quoted lines
> On Tue, Aug 27, 2019 at 08:36:40PM +0200, SZEDER Gábor wrote:
> > Try using the same build flags for the install, i.e.:
> > 
> >   make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g" install
> 
> Yes, now it works thanks!
> 
> On Tue, Aug 27, 2019 at 08:34:23PM +0200, Johannes Sixt wrote:
> > That is because the command sequence above does not do what you think it
> > does. Didn't you notice that everything was recompiled during `sudo make
> > install`?
> 
> Shouldn't be this documented somewhere?

Well, perhaps. But would users actually read it? Dunno.

Case in point: the first few lines of 'INSTALL' already tell you to build and install Git with the following commands:

  $ make prefix=/usr all doc info ;# as yourself
  # make prefix=/usr install install-doc install-html install-info ;# as root

Note the same 'prefix=...' in both commands, yet you omitted it when you ran 'make install' ;)

> To my knowledge `make install` is expected to copy artifacts of the
> build under prefix. It's unusual to me that `install` has the `all`
> target as prerequisite.

I don't think it's unusual at all that the 'install' target depends on 'all'. A quick and incomprehensive survey of Makefiles from a few open source projects that I happen to have lying around seems to confirm this.

What might be unusual (but is rather convenient for developers) is that Git's build process checks the build flags, and rebuilds the whole thing after the flags changed.

Giuseppe Crino'· Aug 28, 2019, 11:00 UTC · re: SZEDER Gábor · lore

Re: How to build to debug with gdb?

On Wed, Aug 28, 2019 at 12:32:17PM +0200, SZEDER Gábor wrote:
Show 8 quoted lines
> I don't think it's unusual at all that the 'install' target depends on
> 'all'.  A quick and incomprehensive survey of Makefiles from a few
> open source projects that I happen to have lying around seems to
> confirm this.
> 
> What might be unusual (but is rather convenient for developers) is
> that Git's build process checks the build flags, and rebuilds the
> whole thing after the flags changed.

Maybe I'm biased by my incompetence with Makefile's but since rebuilding the project with `install` but different flags from the first make is unusual this is worth mentioning to developers.

Giuseppe Crino'· Aug 28, 2019, 12:43 UTC · re: Giuseppe Crino' · lore

Re: How to build to debug with gdb?

On Wed, Aug 28, 2019 at 11:00:46AM +0000, Giuseppe Crino' wrote:
> Maybe I'm biased by my incompetence with Makefile's but since rebuilding
> the project with `install` but different flags from the first make is
> unusual this is worth mentioning to developers.
Definitely *not* sarcastic
SZEDER Gábor· Aug 27, 2019, 18:36 UTC · re: Giuseppe Crino' · lore

Re: How to build to debug with gdb?

On Tue, Aug 27, 2019 at 04:27:25PM +0000, Giuseppe Crino' wrote:
> Hello, to debug some issues I built and installed git via
> 
> $ make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g"
> $ sudo make install

What do the output of these two command look like? After the first command build Git with the custom prefix and CFLAGS, the second command is supposed to start with printing "* new build flags" and then to build Git again with the default prefix and CFLAGS.

>  What am I missing?
Just a couple hundred lines worth of build output :)
Try using the same build flags for the install, i.e.:
  make prefix=/usr/local DEVELOPER=1 CFLAGS="-O0 -g" install

← back to recent threads