| 1 |
from ptrace.error import PTRACE_ERRORS, writeError |
| 2 |
from logging import getLogger |
| 3 |
from ptrace.ctypes_tools import formatAddress |
| 4 |
|
| 5 |
|
| 6 |
class FunctionArgument(object): |
| 7 |
""" |
| 8 |
Description of a function argument. Attributes: |
| 9 |
- function: a Function object |
| 10 |
- index (int): index of the argument (starting at zero) |
| 11 |
- options: a FunctionCallOptions object |
| 12 |
- value (int) |
| 13 |
- type (str, optional) |
| 14 |
- text (str): string describing the argument |
| 15 |
|
| 16 |
Don't use text attribute directly, use getText() to format the |
| 17 |
argument instead. |
| 18 |
""" |
| 19 |
|
| 20 |
def __init__(self, function, index, options, |
| 21 |
value=None, type=None, name=None): |
| 22 |
self.function = function |
| 23 |
self.index = index |
| 24 |
self.options = options |
| 25 |
self.value = value |
| 26 |
self.type = type |
| 27 |
self.name = name |
| 28 |
self.text = None |
| 29 |
|
| 30 |
def getText(self): |
| 31 |
if not self.text: |
| 32 |
try: |
| 33 |
text = self.createText() |
| 34 |
if text is not None: |
| 35 |
self.text = str(text) |
| 36 |
elif self.type and self.type.endswith("*"): |
| 37 |
self.text = formatAddress(self.value) |
| 38 |
else: |
| 39 |
self.text = repr(self.value) |
| 40 |
except PTRACE_ERRORS as err: |
| 41 |
writeError(getLogger(), err, |
| 42 |
"Format argument %s of function %s() value error" |
| 43 |
% (self.name, self.function.name)) |
| 44 |
self.text = repr(self.value) |
| 45 |
return self.text |
| 46 |
|
| 47 |
def format(self): |
| 48 |
text = self.getText() |
| 49 |
options = self.options |
| 50 |
if options.write_argname and self.name: |
| 51 |
if options.write_types and self.type: |
| 52 |
return "%s %s=%s" % (self.type, self.name, text) |
| 53 |
else: |
| 54 |
return "%s=%s" % (self.name, text) |
| 55 |
elif options.write_types and self.type: |
| 56 |
return "(%s)%s" % (self.type, text) |
| 57 |
else: |
| 58 |
return text |
| 59 |
|
| 60 |
def createText(self): |
| 61 |
return repr(self.value) |
| 62 |
|
| 63 |
def formatPointer(self, value, address): |
| 64 |
if self.options.write_address: |
| 65 |
return "%s at %s" % (value, formatAddress(address)) |
| 66 |
else: |
| 67 |
return value |
| 68 |
|
| 69 |
def readStruct(self, address, struct): |
| 70 |
address = self.value |
| 71 |
|
| 72 |
struct_name = struct.__name__ |
| 73 |
data = self.function.process.readStruct(address, struct) |
| 74 |
arguments = [] |
| 75 |
for name, argtype in struct._fields_: |
| 76 |
value = getattr(data, name) |
| 77 |
try: |
| 78 |
text = self.formatStructValue(struct_name, name, value) |
| 79 |
if text is not None: |
| 80 |
text = str(text) |
| 81 |
else: |
| 82 |
text = repr(value) |
| 83 |
except PTRACE_ERRORS as err: |
| 84 |
writeError(getLogger(), err, "Format struct value error") |
| 85 |
text = repr(value) |
| 86 |
arguments.append("%s=%s" % (name, text)) |
| 87 |
|
| 88 |
data = "<%s %s>" % (struct_name, ", ".join(arguments)) |
| 89 |
return self.formatPointer(data, address) |
| 90 |
|
| 91 |
def formatStructValue(self, struct, name, value): |
| 92 |
return None |
| 93 |
|
| 94 |
def readArray(self, address, basetype, count): |
| 95 |
array = self.function.process.readArray(address, basetype, count) |
| 96 |
arguments = [] |
| 97 |
for index in range(count): |
| 98 |
value = array[index] |
| 99 |
value = str(value) |
| 100 |
arguments.append(value) |
| 101 |
arguments = ", ".join(arguments) |
| 102 |
return self.formatPointer("<(%s)>" % arguments, address) |
| 103 |
|
| 104 |
def __repr__(self): |
| 105 |
return "argument %s of %s()" % (self.name, self.function.name) |