| 1 |
from ptrace.ctypes_tools import formatAddress, formatWordHex |
| 2 |
from ptrace.cpu_info import CPU_WORD_SIZE, CPU_MAX_UINT |
| 3 |
from ptrace import PtraceError |
| 4 |
|
| 5 |
|
| 6 |
class BacktraceFrame(object): |
| 7 |
""" |
| 8 |
Backtrace frame. |
| 9 |
|
| 10 |
Attributes: |
| 11 |
- ip: instruction pointer |
| 12 |
- name: name of the function |
| 13 |
- arguments: value of the arguments |
| 14 |
""" |
| 15 |
|
| 16 |
def __init__(self, ip): |
| 17 |
self.ip = ip |
| 18 |
self.name = u"???" |
| 19 |
self.arguments = [] |
| 20 |
|
| 21 |
def __str__(self): |
| 22 |
arguments = (formatWordHex(arg) for arg in self.arguments) |
| 23 |
return u"IP=%s: %s (%s)" % (formatAddress(self.ip), self.name, ", ".join(arguments)) |
| 24 |
|
| 25 |
|
| 26 |
class Backtrace(object): |
| 27 |
""" |
| 28 |
Backtrace: all process frames since the start function. |
| 29 |
""" |
| 30 |
|
| 31 |
def __init__(self): |
| 32 |
self.frames = [] |
| 33 |
self.truncated = False |
| 34 |
|
| 35 |
def append(self, frame): |
| 36 |
self.frames.append(frame) |
| 37 |
|
| 38 |
def __iter__(self): |
| 39 |
return iter(self.frames) |
| 40 |
|
| 41 |
def __len__(self): |
| 42 |
return len(self.frames) |
| 43 |
|
| 44 |
|
| 45 |
def getBacktrace(process, max_args=6, max_depth=20): |
| 46 |
""" |
| 47 |
Get the current backtrace of the specified process: |
| 48 |
- max_args: maximum number of arguments in a frame |
| 49 |
- max_depth: maximum number of frames |
| 50 |
|
| 51 |
Return a Backtrace object. |
| 52 |
""" |
| 53 |
backtrace = Backtrace() |
| 54 |
|
| 55 |
# Get current instruction and frame pointer |
| 56 |
ip = process.getInstrPointer() |
| 57 |
fp = process.getFramePointer() |
| 58 |
depth = 0 |
| 59 |
while True: |
| 60 |
# Hit maximum trace depth? |
| 61 |
if max_depth <= depth: |
| 62 |
backtrace.truncated = True |
| 63 |
break |
| 64 |
|
| 65 |
# Read next frame pointer |
| 66 |
try: |
| 67 |
nextfp = process.readWord(fp) |
| 68 |
except PtraceError: |
| 69 |
nextfp = None |
| 70 |
|
| 71 |
# Guess number of function argument |
| 72 |
if fp and nextfp: |
| 73 |
nargs = ((nextfp - fp) // CPU_WORD_SIZE) - 2 |
| 74 |
nargs = min(nargs, max_args) |
| 75 |
else: |
| 76 |
nargs = 0 |
| 77 |
|
| 78 |
# Create frame |
| 79 |
frame = getBacktraceFrame(process, ip, fp, nargs) |
| 80 |
backtrace.append(frame) |
| 81 |
|
| 82 |
# End of the stack? |
| 83 |
if not nextfp: |
| 84 |
break |
| 85 |
|
| 86 |
# Move to next instruction/frame pointer |
| 87 |
ip = process.readWord(fp + CPU_WORD_SIZE) |
| 88 |
if ip == CPU_MAX_UINT: |
| 89 |
# Linux hack to detect end of the stack |
| 90 |
break |
| 91 |
fp = nextfp |
| 92 |
depth += 1 |
| 93 |
return backtrace |
| 94 |
|
| 95 |
|
| 96 |
def getBacktraceFrame(process, ip, fp, nargs): |
| 97 |
""" |
| 98 |
Get a backtrace frame: |
| 99 |
- ip: instruction pointer |
| 100 |
- fp: frame pointer |
| 101 |
- nargs: number of arguments |
| 102 |
|
| 103 |
Return a BacktraceFrame object. |
| 104 |
""" |
| 105 |
frame = BacktraceFrame(ip) |
| 106 |
address = fp + CPU_WORD_SIZE |
| 107 |
try: |
| 108 |
for index in range(nargs): |
| 109 |
address += CPU_WORD_SIZE |
| 110 |
word = process.readWord(address) |
| 111 |
frame.arguments.append(word) |
| 112 |
except PtraceError: |
| 113 |
# Ignore argument read error |
| 114 |
pass |
| 115 |
return frame |