user138126
user138126

Reputation: 1003

how to return to main function in gdb

I'm using gdb for debugging I get a segmentation fault, and then I want to set another break point in the main function and run the program from the beginning however, although I have finished the current run and it shows "THe program is not being run"

when I input 'list'

it shows a code snippet of a libarary file it means currently I'm not in the main function

If I re-run the program, even if I set the break point at the beginning of the main() it still get segmentation fault, it means the program is running within the library file

so how to return to the main() function? thanks!

tips: I'm using libpcap.h and I have a '-lpcap' option when compiling

BTW, when I use break 9 to set a breakpoint at 9, gdb runs the program to the 11-th line? what is wrong with this inaccuracy? thanks!

Upvotes: 1

Views: 777

Answers (3)

ks1322
ks1322

Reputation: 35825

If I re-run the program, even if I set the break point at the beginning of the main() it still get segmentation fault, it means the program is running within the library file

Actually it means that you either failed to set breakpoint on main function or program execution not reaches main and gets segmentation fault. Try the following steps:

  1. Rebuild program from scratch with debug info (-g gcc option). Reset breakpoint and watch for any warnings from gdb.
  2. If program still crashes with breakpoint set on main look at stack trace (bt command in gdb). It is probably happening before main and you will not see main in stack trace.

Upvotes: 0

gcbenison
gcbenison

Reputation: 11963

"BTW, when I use break 9 to set a breakpoint at 9, gdb runs the program to the 11-th line" - from this, and other information you've provided, it sounds like perhaps the source code is out of sync with gdb's mapping of addresses to source lines. Have you by any chance been editing the program? Have you recompiled it and restarted gdb? Have you seen any warnings similar to "executable is more recent than source"?

Upvotes: 0

unwind
unwind

Reputation: 400039

Simply re-issue the run command. You will lose program state, but not breakpoints which seems to match what you need.

Upvotes: 1

Related Questions