本文介绍了如何将参数传递给从命令行启动的python gdb脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想将一些命令行参数传递给通过 gdb 命令运行的python脚本,但是在python中导入gdb模块会从sys中删除argv属性。如何在我的示例中显示的python脚本中访问arg1和arg2? 命令行执行: $ gdb -x a.py --args python -arg1 -arg2 a.py: #!/ usr / bin / env python 导入gdb 导入sys print('参数是:{0}'format(sys.argv)) gdb.execute('quit') 引发错误: AttributeError:'module'object has没有属性'argv' 版本: GNU gdb(GDB)7.2 Python 2.6.6 编辑: 我要调试的最终目标是一个已经运行的C可执行文件,所以稍后我会在脚本中附加它,所以 gdb -x a.py --args python -arg1 -arg2 不正确,因为 python 部分会打印gdb错误:读取/ usr / b中的符号in / python ...(找不到调试符号)... done。 ... 解决方案 argv.py: p $ p $ print(arg0) print(arg1) 调用: gdb --batch -ex'py arg0 = 12'-ex'py arg1 = 3.4'-x argv .py 然后,您可以将其包装在以下脚本中: #!/ usr / bin / env bash doc =将参数传递给python脚本。 用法: $ 0 scrpit.py 1 2 其中scrpit.py使用如下参数: print (arg0) print(arg1) py =$ 1 shift cli =''i = 0 为$ *; cli =$ cli -ex'py arg $ i = $ arg'i = $(($ i + 1))完成 eval gdb --batch $ cli -x$ py I'd like to pass some command line arguments to a python script run via gdb command, but importing the gdb module in python removes the argv attribute from sys. How do I access arg1 and arg2 within my python script shown in my example?Command line execution:$ gdb -x a.py --args python -arg1 -arg2a.py:#!/usr/bin/env pythonimport gdbimport sysprint('The args are: {0}'.format(sys.argv))gdb.execute('quit')Error raised:AttributeError: 'module' object has no attribute 'argv'Versions:GNU gdb (GDB) 7.2Python 2.6.6Edit:The end target I'll be debugging is a C executable that is already running, so I'll be attaching to it later in the script, so gdb -x a.py --args python -arg1 -arg2 is not correct either since the python part prints a gdb error: Reading symbols from /usr/bin/python...(no debugging symbols found)...done.... 解决方案 -ex py commands is a possibility.argv.py:print(arg0)print(arg1)Invocation:gdb --batch -ex 'py arg0 = 12' -ex 'py arg1 = 3.4' -x argv.pyThen you could wrap that in the following script:#!/usr/bin/env bashdoc="Pass parameters to python script.Usage: $0 scrpit.py 1 2Where scrpit.py uses the arguments like: print(arg0) print(arg1)"py="$1"shiftcli=''i=0for arg in $*; do cli="$cli -ex 'py arg$i = $arg'" i=$(($i+1))doneeval gdb --batch $cli -x "$py" 这篇关于如何将参数传递给从命令行启动的python gdb脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 22:18