如何在写入“定义”时在GDB中获取我的程序名称脚本? [英] How to get my program name in GDB when writting a "define" script?

查看:140
本文介绍了如何在写入“定义”时在GDB中获取我的程序名称脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现当定义一个GDB脚本时,获取程序名称非常烦人。我找不到任何相应的信息命令,并且由于多进程/线程和框架选择,我们也不能使用argv [0]。



所以,我该怎么做?

解决方案

如果您使用的是最近的gdb(7及以上版本) Python支持非常广泛(并且可能在您定义gdb脚本时想要去的地方)。我不是专家,但是我可以使用一个测试程序/ tmp / xyz:

 (gdb)python print gdb.current_progspace()。filename 
/ tmp / xyz

请参阅 http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html 更多关于Python支持的信息。



在普通gdb中,您可以使用info proc和info target获取进程名称,但我想你想要它不只是印刷出来,还能够在脚本中进一步使用它?不知道如何将python运行时的值转化为gdb变量,而不是非常丑陋的使用日志文件并获取它并希望获得最佳效果。这是如何实现的:

  define set-program-name 
设置日志文件tmp.gdb
设置日志覆盖
设置日志重定向
设置日志记录
python printset $ programname = \%s\%gdb.current_progspace()。filename
设置注销
设置日志重定向关闭
设置日志覆盖关闭
源tmp.gdb
结束

并在你自己的函数中执行:

$ p $ define your-own- func
set-program-name
printf程序名称是%s\\\
,$ programname
end

我会建议全力以赴支持python,并废除gdb脚本。我相信这是值得的。


I found it's very annoying to get the program name when defining a GDB script. I can't find any corresponding "info" command, and we can't use argv[0] either, because of multi-process/thread and frame-choosing.

So, what should I do?

解决方案

If you are using a recent gdb (7 and above) you can play around with the Python support which is quite extensive (and probably where you want to go when defining gdb-scripts in general). I'm no expert at it but for a test-program /tmp/xyz I could use:

(gdb) python print gdb.current_progspace().filename
/tmp/xyz

See http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html for more info on the Python support.

In "normal" gdb you could get the process name with "info proc" and "info target", but I guess you wanted it not just printed but being able to use it further in scripts? Don't know how to get the value out of the python runtime into a gdb variable other than the extremely ugly "using log files and sourcing it and hope for the best". This is how this could be done:

define set-program-name
  set logging file tmp.gdb
  set logging overwrite on
  set logging redirect on
  set logging on
  python print "set $programname = \"%s\"" % gdb.current_progspace().filename
  set logging off
  set logging redirect off
  set logging overwrite off
  source tmp.gdb
end

and in your own function doing:

define your-own-func
  set-program-name
  printf "The program name is %s\n", $programname
end

I would suggest "going all in" on the python support, and scrap gdb-scripting. I believe it is worth the effort.

这篇关于如何在写入“定义”时在GDB中获取我的程序名称脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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