使用 gdb 调用 printDebug 方法 [英] Use gdb to call a printDebug method

查看:14
本文介绍了使用 gdb 调用 printDebug 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 printDebug 方法的类.它没有在代码中的任何地方使用,但我想在使用 gdb 调试时使用它(使用调用).这基本上是以格式良好的方式打印对象的内容,例如我可能有一个集合向量.用于此的 g++ 选项是什么?我试过 -O0 但这不起作用.

I have a class with a printDebug method. Its not used anywhere in the code but I would like to use it when I am debugging with gdb (using call). This is basically to print the contents of the object in a nicely formatted way, for instance I may have a vector of sets. What is the g++ option to use for this? I have tried -O0 but that does not work.

我使用的解决方法是在构造函数中进行伪调用以调试打印并提供一个布尔值来指示您是否真的想要打印或什么都不做.这工作正常,但必须有更好的方法来做到这一点.

The work around I used was to make a psuedo call in the constructor to debugPrint and provide a bool indicating if you actually want to print or do nothing. This works fine but there has to be a better way to do this.

如果我理解正确 -O0 不应该做任何优化,所以不应该消除死代码,但也许我错了.

If I understand correctly -O0 should not do any optimisations so dead code should not be eliminated but perhaps I am wrong.

推荐答案

如果你有一个没有在代码中任何地方使用的方法 gcc 智能功能可以识别它并在编译你的应用程序时忽略它.这就是为什么当您显示应用程序的符号(使用 nm)时,该方法不会显示在结果中.

If you have a method that is not used anywhere on the code gcc smart features can identify this and ignore it while compiling your application. That's why when you display the symbols (using nm) of the application that method doesn't show on the results.

但是,如果您想强制编译该方法,则需要指定 _属性_ used 在方法声明上.例如:

However, if you want to force that method to be compiled anyway you need to specify the _attribute_ used on the method declaration. For instance:

  1 
  2 #include <iostream>
  3 #include <stdio.h>
  4 
  5 
  6 class aClass
  7 {
  8     public:
  9         void __attribute__ ((used)) publicPrint()
 10         {
 11             std::cout << "public method" << std::endl;
 12         }
 13 };
 14 
 15 
 16 int main()
 17 {
 18     aClass my_obj;
 19 
 20     getchar();
 21 }

出于测试目的,我使用 -g 编译了此源代码:

For testing purposes I compiled this source code with -g:

g++ -g print_dbg.cpp -o print_dbg

我要说的可能是不必要的,但我还是会这样做:注意 my_obj 在 main() 中被声明为局部变量.这意味着当我在此范围内调试代码时,我只能访问方法 publicPrint().当代码执行跳转到 getchar() 的开头时,代码执行将在另一个范围内,即另一个堆栈帧,并且 my_obj 将不再存在于这个新的上下文中.这只是一个提醒.

What I'm about to say is probably unnecessary, but I'll do it anyway: notice that my_obj is declared as a local variable inside main(). This means I only have access to the method publicPrint() while I'm debugging code inside this scope. When the code execution jumps to the beginning of getchar(), code execution will be at another scope, i.e. another stack frame, and my_obj will no longer exist in this new context. This is just a heads up.

在gdb上,如果在my_obj有效的地方设置断点,可以通过call my_obj.publicPrint()publicPrint()方法执行/代码>

On gdb, if you set a breakpoint where my_obj is valid, you can execute the method publicPrint() through: call my_obj.publicPrint()

$ gdb print_dbg 
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/karl/workspace/gdb/print_dbg...done.

(gdb) list main
12              }
13      };
14
15
16      int main()
17      {
18          aClass my_obj;
19
20          getchar();
21      }
(gdb) break main
Breakpoint 1 at 0x804871d: file print_dbg.cpp, line 20.

(gdb) run
Starting program: /home/karl/workspace/gdb/print_dbg 
Breakpoint 1, main () at print_dbg.cpp:20
20          getchar();

(gdb) call my_obj.publicPrint()
public method

(gdb)

这篇关于使用 gdb 调用 printDebug 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆