user1735401
user1735401

Reputation:

How to open a source file in GDB

I use GDB to attach to a running process and catch a breakpoint. I execute the following commands sequence:

$ gdb -tui

(gdb) attach PID

And now I have the GUI TUI view open without any source file in.

I have been desperately trying to google a way to load / open a source file to be able to execute command "break ", but unfortunately I could not find anything that would simply work.

Upvotes: 3

Views: 17782

Answers (5)

kingkong
kingkong

Reputation: 149

Just use list file.c:1 to show file.c content from line 1.

Upvotes: 1

ahnkle
ahnkle

Reputation: 477

I found that in gdb, the list knows about symbols, so

l main()

will open the main in whatever file it's defined (I'm assuming C in this example - you didn't specify a language).

TAB also completes function names (as on linux command line),

Upvotes: 1

UserX
UserX

Reputation: 525

I FINALLY found out how to do this in cgdb:

  1. Press Esc to go to the code window.
  2. Press Alt-O.
  3. Scroll down to the source file you want to open and press Enter.

Now you can set breakpoints in this file.

Upvotes: 3

Read the examine source file chapter of GDB documentation.

You could use the list command.

Of course you need to set appropriately the directory list. Use the dir command.

The GDB documentation starts with a tutorial chapter, so please read it.

Of course, all your software should be compiled with -g (or even  -g3) to get its debug information from the compiler.

BTW your source file is already opened by (that is : visible to) gdb. You could want to list part of it, or to put a breakpoint (using break or tbreak) in it. No specific action from your part is needed to have it visible to gdb (except perhaps the dir command).

Upvotes: 0

UserX
UserX

Reputation: 525

I have the same problem; it's amazing that such a basic function is so obscure. My workaround is to use the file name and function name to set the breakpoint:

b <filename>:<function name>

Upvotes: 2

Related Questions