From: Ramon F Herrera on

I am mystified by something that seems like magic.

In the simplest case, I wrote the source code and gdb finds it because
it happens to be in the current directory. What about, however, harder
cases? What if the *.c file is in some other directory? Is all the
source code saved in the *.o (and later in the *.a) files??

I am currently working on a program that uses the LIBTIFF library. I
don't believe I have source code for such library, and yet I can
single step and see source code in gdb.

This is probably documented somewhere, but an expert explanation is
most welcome.

TIA,

-Ramon

From: Bjarni Juliusson on
Ramon F Herrera wrote:
> I am mystified by something that seems like magic.
>
> In the simplest case, I wrote the source code and gdb finds it because
> it happens to be in the current directory. What about, however, harder
> cases? What if the *.c file is in some other directory? Is all the
> source code saved in the *.o (and later in the *.a) files??
>
> I am currently working on a program that uses the LIBTIFF library. I
> don't believe I have source code for such library, and yet I can
> single step and see source code in gdb.
>
> This is probably documented somewhere, but an expert explanation is
> most welcome.

When compiling and linking, you can choose whether to include debug info
in the object files. That's what gdb is reading.


Bjarni
--

INFORMATION WANTS TO BE FREE
From: Jens Thoms Toerring on
In comp.unix.programmer Bjarni Juliusson <bjarni(a)update.uu.se> wrote:
> Ramon F Herrera wrote:
> > I am mystified by something that seems like magic.
> >
> > In the simplest case, I wrote the source code and gdb finds it because
> > it happens to be in the current directory. What about, however, harder
> > cases? What if the *.c file is in some other directory? Is all the
> > source code saved in the *.o (and later in the *.a) files??
> >
> > I am currently working on a program that uses the LIBTIFF library. I
> > don't believe I have source code for such library, and yet I can
> > single step and see source code in gdb.
> >
> > This is probably documented somewhere, but an expert explanation is
> > most welcome.

> When compiling and linking, you can choose whether to include debug info
> in the object files. That's what gdb is reading.

But the debug information does not contain the original source
code, that has to be somewhere on the machine. As far as I know
the place where the sources were when the program was compiled
is normally part of the debugging information and gdb tries to
find the sources there (and in the current working directory if
not found in this place). If the sources got moved then you can
set the new location with the 'dir' command, e.g.

(gdb) dir /home/jens/old_stuff/progname:/home/jens/backup/progname

to make gdb also look into these two directories. Or one can use

(gdb) set substitute-path old-path new-path

to replace the old-path (where the sources were during compilation)
by new-path (where they are now).

In the case of the libtiff library the OP mentioned I'd be rather
astonished if the sources aren't on the machine (obviously in the
original place). If the library wasn't compiled on the machine
but installed e.g. via apt-get or some similar package manager
then I would guess that also the source-package is installed
(which should be in the "correct"e location (i.e. the one gdb is
looking for it). The command

(gdb) info sources

should show a list of all files gdb found as the sources of the
program being debugged.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Gary R. Schmidt on
Jens Thoms Toerring wrote:
> In comp.unix.programmer Bjarni Juliusson <bjarni(a)update.uu.se> wrote:
>> Ramon F Herrera wrote:
>>> I am mystified by something that seems like magic.
>>>
>>> In the simplest case, I wrote the source code and gdb finds it because
>>> it happens to be in the current directory. What about, however, harder
>>> cases? What if the *.c file is in some other directory? Is all the
>>> source code saved in the *.o (and later in the *.a) files??
>>>
>>> I am currently working on a program that uses the LIBTIFF library. I
>>> don't believe I have source code for such library, and yet I can
>>> single step and see source code in gdb.
>>>
>>> This is probably documented somewhere, but an expert explanation is
>>> most welcome.
>
>> When compiling and linking, you can choose whether to include debug info
>> in the object files. That's what gdb is reading.
>
> But the debug information does not contain the original source
> code, that has to be somewhere on the machine. As far as I know
> the place where the sources were when the program was compiled
> is normally part of the debugging information and gdb tries to
> find the sources there (and in the current working directory if
> not found in this place). If the sources got moved then you can
> set the new location with the 'dir' command, e.g.
>
> (gdb) dir /home/jens/old_stuff/progname:/home/jens/backup/progname
>
> to make gdb also look into these two directories. Or one can use
>
> (gdb) set substitute-path old-path new-path
>
> to replace the old-path (where the sources were during compilation)
> by new-path (where they are now).
>
> In the case of the libtiff library the OP mentioned I'd be rather
> astonished if the sources aren't on the machine (obviously in the
> original place). If the library wasn't compiled on the machine
> but installed e.g. via apt-get or some similar package manager
> then I would guess that also the source-package is installed
> (which should be in the "correct"e location (i.e. the one gdb is
> looking for it). The command
>
> (gdb) info sources
>
> should show a list of all files gdb found as the sources of the
> program being debugged.
> Regards, Jens
The binary contains the *paths* of the source code files.

Read up on the "strings" command and apply it to the binary.

Cheers,
Gary B-)
From: Jens Thoms Toerring on
In comp.unix.programmer Gary R. Schmidt <grschmidt(a)acm.org> wrote:
> Jens Thoms Toerring wrote:
> > In comp.unix.programmer Bjarni Juliusson <bjarni(a)update.uu.se> wrote:
> >> Ramon F Herrera wrote:
> >>> I am mystified by something that seems like magic.
> >>>
> >>> In the simplest case, I wrote the source code and gdb finds it because
> >>> it happens to be in the current directory. What about, however, harder
> >>> cases? What if the *.c file is in some other directory? Is all the
> >>> source code saved in the *.o (and later in the *.a) files??
> >>>
> >>> I am currently working on a program that uses the LIBTIFF library. I
> >>> don't believe I have source code for such library, and yet I can
> >>> single step and see source code in gdb.
> >>>
> >>> This is probably documented somewhere, but an expert explanation is
> >>> most welcome.
> >
> >> When compiling and linking, you can choose whether to include debug info
> >> in the object files. That's what gdb is reading.
> >
> > But the debug information does not contain the original source
> > code, that has to be somewhere on the machine. As far as I know
> > the place where the sources were when the program was compiled
> > is normally part of the debugging information and gdb tries to
> > find the sources there (and in the current working directory if
> > not found in this place). If the sources got moved then you can
> > set the new location with the 'dir' command, e.g.
> >
> > (gdb) dir /home/jens/old_stuff/progname:/home/jens/backup/progname
> >
> > to make gdb also look into these two directories. Or one can use
> >
> > (gdb) set substitute-path old-path new-path
> >
> > to replace the old-path (where the sources were during compilation)
> > by new-path (where they are now).
> >
> > In the case of the libtiff library the OP mentioned I'd be rather
> > astonished if the sources aren't on the machine (obviously in the
> > original place). If the library wasn't compiled on the machine
> > but installed e.g. via apt-get or some similar package manager
> > then I would guess that also the source-package is installed
> > (which should be in the "correct"e location (i.e. the one gdb is
> > looking for it). The command
> >
> > (gdb) info sources
> >
> > should show a list of all files gdb found as the sources of the
> > program being debugged.
>
> The binary contains the *paths* of the source code files.

I had hoped to convey that with "the place where the sources were
when the program was compiled is normally part of the debugging
information."

> Read up on the "strings" command and apply it to the binary.

I had also thought about mentioning 'strings' but then it's often
not simple to determine what of the output is meant to be the path
to the code, so asking gdb seemed to me to be a bit more efficient.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de