1

I've got a script that's been running for days and is about halfway done. It's got a stupid bug in it which is going to cause it to crash before it finishes if a certain counter gets too high (the counter is otherwise unused; I don't mind mangling it to keep the script going). I did not have the foresight to import a module or write a backdoor to let me change state in any way; I've been told I can still get at the counter using gdb.

For simplicity, imagine instead I'm talking about this script below, which I have run from the command line as "python foo.py"

from time import sleep
i = 0
while(True):
    i += 1
    if (i > 100):
        raise Exception("Explosion")
    sleep(10)

What exactly would I need to type in gdb from time to time to change the value of the variable i to 0, and avoid the explosion? (If it matters, my python binary is 2.7.3).

1 Answer 1

1

Went ahead and powered through on my own. This worked for me (not shown - 'file' to load the python binary, 'attach'; 'detach' from process):

  1. "bt" - see the stack trace
  2. "frame foo" - where foo is the first frame mentioning "globals" as arguments to a Py* function
  3. "print globals" (or, print *(PyDictObject*)globals) - confirm that your variable is there
  4. "down" - move just below the frame you're interested in.
  5. "finish" - (maybe unnecessary?) - let python complete whatever it was doing and release any locks or etc. that might interfere with interfacing with the globals object.
  6. "call PyDict_SetItemString(globals, "i", PyLong_FromLong(0))" - hope it doesn't segfault. Change "i" to whatever your variable is.
  7. "continue" - if you're lucky, the counter should now have been reset to 0.

However, for this to succeed, I think python2.7-gdb has to be installed/setup BEFORE you begin running the python process - can't do it after the fact, or gdb sees a garbled stack.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.