# How to build to debug with gdb?

9 messages from 2019-08-27 to 2019-08-28. Participants: Giuseppe Crino', Matheus Tavares Bernardino, Randall S. Becker, Johannes Sixt, SZEDER Gábor.
Thread: https://gitlist.dev/t/51759

## Giuseppe Crino', 2019-08-27 16:27

Subject: How to build to debug with gdb?
Message-ID: <20190827162725.GA29263@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>
URL: https://gitlist.dev/e/20190827162725.GA29263%40instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal

```
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, 2019-08-27 17:55

Subject: Re: How to build to debug with gdb?
Message-ID: <CAHd-oW6MtoYAwv36_Jsx5qQzvLguXdvrA6NRA+D3ayHGmfVSqw@mail.gmail.com>
URL: https://gitlist.dev/e/CAHd-oW6MtoYAwv36_Jsx5qQzvLguXdvrA6NRA%2BD3ayHGmfVSqw%40mail.gmail.com
In-Reply-To: <20190827162725.GA29263@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>

```
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?
>
>  -Giuseppe

```

## Randall S. Becker, 2019-08-27 18:07

Subject: RE: How to build to debug with gdb?
Message-ID: <018f01d55d02$5e89e1f0$1b9da5d0$@nexbridge.com>
URL: https://gitlist.dev/e/018f01d55d02%245e89e1f0%241b9da5d0%24%40nexbridge.com
In-Reply-To: <CAHd-oW6MtoYAwv36_Jsx5qQzvLguXdvrA6NRA+D3ayHGmfVSqw@mail.gmail.com>

```
On August 27, 2019 1:56 PM, Matheus Tavares Bernardino wrote:
> 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, 2019-08-27 18:34

Subject: Re: How to build to debug with gdb?
Message-ID: <1d44d8d6-6c6f-591e-dd2c-5102c9fd7d11@kdbg.org>
URL: https://gitlist.dev/e/1d44d8d6-6c6f-591e-dd2c-5102c9fd7d11%40kdbg.org
In-Reply-To: <20190827162725.GA29263@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>

```
Am 27.08.19 um 18:27 schrieb Giuseppe Crino':
> 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

```

## SZEDER Gábor, 2019-08-27 18:36

Subject: Re: How to build to debug with gdb?
Message-ID: <20190827183640.GC8571@szeder.dev>
URL: https://gitlist.dev/e/20190827183640.GC8571%40szeder.dev
In-Reply-To: <20190827162725.GA29263@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>

```
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


```

## Giuseppe Crino', 2019-08-28 08:15

Subject: Re: How to build to debug with gdb?
Message-ID: <20190828081557.GA18785@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>
URL: https://gitlist.dev/e/20190828081557.GA18785%40instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal
In-Reply-To: <1d44d8d6-6c6f-591e-dd2c-5102c9fd7d11@kdbg.org>

```
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, 2019-08-28 10:32

Subject: Re: How to build to debug with gdb?
Message-ID: <20190828103217.GA6400@szeder.dev>
URL: https://gitlist.dev/e/20190828103217.GA6400%40szeder.dev
In-Reply-To: <20190828081557.GA18785@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>

```
On Wed, Aug 28, 2019 at 08:15:57AM +0000, Giuseppe Crino' wrote:
> 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', 2019-08-28 11:00

Subject: Re: How to build to debug with gdb?
Message-ID: <20190828110046.GB4450@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>
URL: https://gitlist.dev/e/20190828110046.GB4450%40instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal
In-Reply-To: <20190828103217.GA6400@szeder.dev>

```
On Wed, Aug 28, 2019 at 12:32:17PM +0200, SZEDER Gábor wrote:
> 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', 2019-08-28 12:43

Subject: Re: How to build to debug with gdb?
Message-ID: <20190828124354.GA7585@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>
URL: https://gitlist.dev/e/20190828124354.GA7585%40instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal
In-Reply-To: <20190828110046.GB4450@instance-template-2.europe-west6-a.c.vaulted-journal-250706.internal>

```
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

```
