0

I need to set breakpoints in a cpp source file. The current setup to call the cpp target is through a shell target, with additional dependencies, which means it's not feasible to directly invoke the cpp target in Linux console. As I searched online, there are generally two ways:

  1. invoke gdb in shell
  2. pause in cpp, let gdb connect to the process

I don't know how to do the first way, so I choose the second way here. I insert sleep(30) in cpp file, then in another terminal I open gdb and connect to the running process. I confirm the gdb can stop at the sleep() function in gdb. But the problem is the gdb seems only knowing the sleep function context, without knowing the call site of the sleep function. If I force setting breakpoint in the main program, gdb shows no such file. If I continue in gdb, it will not stop at any breakpoints I set in cpp file.

1 Answer 1

0

You need to compile the program with debug symbols. Otherwise GDB will only know of symbols in the dynamic symbol table. Turning off optimizations also helps debugging. So add the flags -O0 -g.

If this is not possible, you'll have have to step through the disassembly (Ctrl+X, 2).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the reply! Turning on debug build works for me! In fact, I tried debug build before, but turns out I did it in a wrong way: I first build the target with -c dbg flag, then bazel run. It turns out each time I call bazel run, the build will be invoked automatically regardless. Hence, I need to use bazel run -c dbg [target] in order to get a debug build.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.