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 -arg2
a.py:
#!/usr/bin/env python
import gdb
import sys
print('The args are: {0}'.format(sys.argv))
gdb.execute('quit')
Error raised:
AttributeError: 'module' object has no attribute 'argv'
Versions:
- GNU gdb (GDB) 7.2
- Python 2.6.6
Edit:
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....


a.pydefine a python function, saya, then you can give gdb an argument such as-ex "python a('the answer is ',42)".$ gdb -ex "python import os,sys; sys.path.append(os.getcwd()); from a import *; main('arg1', 'arg2')". My best idea is to write a helper bash script that launches a.py (what I'm already doing), but redirects the command line arguments to a temp file that the a.py script reads from <- this is less kludgy especially with tricky arguments and can use optparse..