gdb: No symbol “i” in current context

后端 未结 6 477
谎友^
谎友^ 2021-02-02 10:02

While debugging a C program in gdb I have a breakpoint in a for loop. I cannot print the value of \"i\" ( I get : No symbol \"i\" in current context.). I can print the value of

6条回答
  •  既然无缘
    2021-02-02 10:38

    In case anyone else is using Google's Bazel build system for your project, I'd like to add that if you cannot print any variables from gdb, it may be because you need to properly add the -ggdb and -O0 (update: use -Og instead of -O0 use -O0 over -Og) C build flags using the --copt= option, INSTEAD OF using the --per_file_copt= option. In my case, although they both built and ran just fine, only the --copt= technique allowed me to fully use gdb and print variables, whereas the --per_file_copt= one also allowed me to use gdb but would NOT allow me to print variables.

    Note: in the below examples, just replace test with build if you do NOT need to run the unit tests as well.

    UDPATE: it turns out, you should prefer -Og over -O0 when doing debugging, so I'm updating these examples accordingly. See here: What's the difference between a compiler's `-O0` option and `-Og` option?.

    So, do this:

    time bazel test --copt=-ggdb --copt=-O0 \
    //my/build/folder1/... //my/build/folder2/...
    

    INSTEAD OF this:

    time bazel test --per_file_copt=//my/build/folder1/...,//my/build/folder2/...@-ggdb,-O0 \
    //my/build/folder1/... //my/build/folder2/...
    

    ...in order to be able to print variables from within gdb.

    Again, both of the above techniques build and run just fine, and both allow me to run and use gdb, but only the first one actually allows me to use gdb to its full extent.

    Lastly, if the first command above still doesn't work, try adding the --strip=never Bazel flag described here to prevent Bazel from ever stripping debugging information. Now the command will look like this:

    time bazel test --copt=-ggdb --copt=-O0 --strip=never \
    //my/build/folder1/... //my/build/folder2/...
    

    Reference documentation:

    1. --copt=:
      1. https://docs.bazel.build/versions/master/command-line-reference.html#flag--copt
      2. [better, with examples] https://docs.bazel.build/versions/master/user-manual.html#flag--copt
    2. --per_file_copt:
      1. https://docs.bazel.build/versions/master/command-line-reference.html#flag--per_file_copt
      2. [better, with examples] https://docs.bazel.build/versions/master/user-manual.html#flag--per_file_copt
    3. --strip=never:
      1. https://docs.bazel.build/versions/master/user-manual.html#flag--strip
    4. [my own Q&A] Prefer -Og over -O0 -O0 over -Og for debugging: What's the difference between a compiler's `-O0` option and `-Og` option?

提交回复
热议问题