-
Notifications
You must be signed in to change notification settings - Fork 12
Exact location through deparsing bytecodes
In C Python program locations are reported only with the granularity of a "line number". But there can be a lot going on in a line. That's why I provide in in the trepanning debuggers I provide an offset address in the location status.
Yeah, I have been asked: do people really want this detail? I suppose for most people the answer is no, but debuggers are sometimes used for hard-to-solve problems, and it gives the most precise answer of where Python is currently stopped.
I've been working exposing the exact position in another way. And of course, it needs underneath this byte-code offset. There is a CPython bytecode-to-Python disassembler called uncompyle2.
Here is an example using test/example/gcd.py from trepan debugger distribution:
$ trepan2 gcd.py 3 5
(test/example/gcd.py:10): <module>
-> 10 """
Evaluate unrecognized debugger commands is on.
output set for terminal with escape sequences for a dark background
Rocky's python trepan profile loaded
(trepan2) next 2
(test/example/gcd.py:40 @36): <module>
-- 40 if __name__=='__main__':
Above are stopped at at line 40 of the program with instruction offset 36 of the main program. But where exactly are we?
(trepan2) deparse
opcode: LOAD_NAME
if __name__ == '__main__':
--------
The opcode for that instruction is a "load name" instruction, the deparse command shows what source-code text is associated with that instruction.
If you want more information about that particular instruction you can use the disassemble command:
(trepan2) disassemble @36 @40
Disassembly of <frame object at 0x7fef12488220>: from offset 36 to offset 40
40--> 36 LOAD_NAME 4 (__name__)
39 LOAD_CONST 5 ('__main__')
In order to see a little more context We can also get information about the parent deparsed fragment by adding the -p option:
(trepan2) deparse -p
opcode: LOAD_NAME
if __name__ == '__main__':
--------
Contained in...
if __name__ == '__main__':
----------------------
parsed type: compare
Lastly I should note that although I use this technique in my trepan debuggers, it can be used in other places too such as in libraries that provide stack traces.
So let's get more programs giving better location information!