| 1 |
from ptrace.func_arg import FunctionArgument |
| 2 |
|
| 3 |
|
| 4 |
class FunctionCallOptions(object): |
| 5 |
""" |
| 6 |
Options to format a function call and its arguments. |
| 7 |
""" |
| 8 |
|
| 9 |
def __init__(self, |
| 10 |
write_types=False, write_argname=False, |
| 11 |
replace_socketcall=True, string_max_length=300, |
| 12 |
write_address=False, max_array_count=20): |
| 13 |
self.write_address = write_address |
| 14 |
self.write_types = write_types |
| 15 |
self.write_argname = write_argname |
| 16 |
self.replace_socketcall = replace_socketcall |
| 17 |
self.string_max_length = string_max_length |
| 18 |
self.max_array_count = max_array_count |
| 19 |
self.instr_pointer = False |
| 20 |
|
| 21 |
|
| 22 |
class FunctionCall(object): |
| 23 |
""" |
| 24 |
A function call. Attributes: |
| 25 |
- name (str): function name |
| 26 |
- arguments: list of FunctionArgument objects |
| 27 |
- restype (str, optional): result type |
| 28 |
- resvalue (optional): result value |
| 29 |
- argument_class: class used to build the new arguments |
| 30 |
|
| 31 |
Methods: |
| 32 |
- format(): create a string representation of the call |
| 33 |
- addArgument(): add a new argument |
| 34 |
- clearArguments(): remove all arguments |
| 35 |
""" |
| 36 |
|
| 37 |
def __init__(self, name, options, argument_class=FunctionArgument): |
| 38 |
self.name = name |
| 39 |
self.options = options |
| 40 |
self.arguments = [] |
| 41 |
self.restype = None |
| 42 |
self.resvalue = None |
| 43 |
self.argument_class = argument_class |
| 44 |
|
| 45 |
def addArgument(self, value=None, name=None, type=None): |
| 46 |
arg = self.argument_class( |
| 47 |
self, len(self.arguments), self.options, value, type, name) |
| 48 |
self.arguments.append(arg) |
| 49 |
|
| 50 |
def clearArguments(self): |
| 51 |
self.arguments = [] |
| 52 |
|
| 53 |
def __getitem__(self, key): |
| 54 |
if isinstance(key, str): |
| 55 |
for arg in self.arguments: |
| 56 |
if arg.name == key: |
| 57 |
return arg |
| 58 |
raise KeyError("%r has no argument called %r" % (self, key)) |
| 59 |
else: |
| 60 |
# Integer key |
| 61 |
return self.arguments[key] |
| 62 |
|
| 63 |
def format(self): |
| 64 |
arguments = [arg.format() for arg in self.arguments] |
| 65 |
|
| 66 |
# Remove empty optionnal arguments |
| 67 |
while arguments and not arguments[-1]: |
| 68 |
arguments.pop(-1) |
| 69 |
|
| 70 |
arguments = ", ".join(arguments) |
| 71 |
if self.restype and self.options.write_types: |
| 72 |
return "%s %s(%s)" % (self.restype, self.name, arguments) |
| 73 |
else: |
| 74 |
return "%s(%s)" % (self.name, arguments) |
| 75 |
|
| 76 |
def __repr__(self): |
| 77 |
return "<FunctionCall name=%r>" % self.name |