diff options
| author | Peter LaFosse <peter@vector35.com> | 2023-08-03 09:51:16 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2023-11-03 10:41:47 -0400 |
| commit | 96434bc675f93766c30f4917948988717e21e7e9 (patch) | |
| tree | dcf32d0998578e5f309bea59c85db3ba1f2f2a84 /python/debuginfo.py | |
| parent | 9e06c61f699423ef20673e963207fed4dd02bf42 (diff) | |
Add 'parsers' property and ensure DebugInfo APIs free properly on internal errors
Diffstat (limited to 'python/debuginfo.py')
| -rw-r--r-- | python/debuginfo.py | 81 |
1 files changed, 40 insertions, 41 deletions
diff --git a/python/debuginfo.py b/python/debuginfo.py index c2f750a4..1b9bc712 100644 --- a/python/debuginfo.py +++ b/python/debuginfo.py @@ -288,28 +288,27 @@ class DebugInfo(object): def __del__(self) -> None: core.BNFreeDebugInfoReference(self.handle) - def get_parsers(self): + def get_parsers(self) -> List[str]: + """Kept for backward compatibility. Use ``parsers`` property instead.""" return self.parsers @property - def parsers(self): + def parsers(self) -> List[str]: count = ctypes.c_ulonglong() parsers = core.BNGetDebugParserNames(self.handle, count) - - result = [] - for i in range(count.value): + try: assert parsers is not None, "core.BNGetDebugParserNames returned None" - result.append(parsers[i].decode("utf-8")) - core.BNFreeStringList(parsers, count.value) + result.append(parsers[i].encode("utf-8")) + finally: + core.BNFreeStringList(parsers, count.value) - return result def types_from_parser(self, name: Optional[str] = None) -> Iterator[Tuple[str, _types.Type]]: """Returns a generator of all types provided by a named DebugInfoParser""" count = ctypes.c_ulonglong(0) name_and_types = core.BNGetDebugTypes(self.handle, name, count) - assert name_and_types is not None, "core.BNGetDebugTypes returned None" try: + assert name_and_types is not None, "core.BNGetDebugTypes returned None" for i in range(0, count.value): yield (name_and_types[i].name, _types.Type.create(core.BNNewTypeReference(name_and_types[i].type))) finally: @@ -324,8 +323,8 @@ class DebugInfo(object): """Returns a generator of all functions provided by a named DebugInfoParser""" count = ctypes.c_ulonglong(0) functions = core.BNGetDebugFunctions(self.handle, name, count) - assert functions is not None, "core.BNGetDebugFunctions returned None" try: + assert functions is not None, "core.BNGetDebugFunctions returned None" for i in range(0, count.value): if functions[i].type: @@ -354,8 +353,8 @@ class DebugInfo(object): """Returns a generator of all data variables provided by a named DebugInfoParser""" count = ctypes.c_ulonglong(0) data_variables = core.BNGetDebugDataVariables(self.handle, name, count) - assert data_variables is not None, "core.BNGetDebugDataVariables returned None" try: + assert data_variables is not None, "core.BNGetDebugDataVariables returned None" for i in range(0, count.value): yield binaryview.DataVariableAndName( data_variables[i].address, @@ -396,46 +395,46 @@ class DebugInfo(object): """ The first element in the Tuple returned in the list is the name of the debug info parser the type came from """ count = ctypes.c_ulonglong() names_and_types = core.BNGetDebugTypesByName(self.handle, name, count) - - result = [] - for i in range(count.value): - assert names_and_types is not None, "core.BNGetDebugTypesByName returned None" - result.append((names_and_types[i].name, _types.Type.create(core.BNNewTypeReference(names_and_types[i].type)))) - - core.BNFreeNameAndTypeList(names_and_types, count.value) - return result + try: + result = [] + for i in range(count.value): + assert names_and_types is not None, "core.BNGetDebugTypesByName returned None" + result.append((names_and_types[i].name, _types.Type.create(core.BNNewTypeReference(names_and_types[i].type)))) + return result + finally: + core.BNFreeNameAndTypeList(names_and_types, count.value) def get_data_variables_by_name(self, name: str) -> List[Tuple[str, _types.Type]]: """ The values in the tuples returned in the list is (DebugInfoParserName, address, type) """ count = ctypes.c_ulonglong() variables_and_name = core.BNGetDebugDataVariablesByName(self.handle, name, count) - - result = [] - for i in range(count.value): - assert variables_and_name is not None, "core.BNGetDebugDataVariablesByName returned None" - result.append(( - variables_and_name[i].name, variables_and_name[i].address, - _types.Type.create(core.BNNewTypeReference(variables_and_name[i].type)) - )) - - core.BNFreeDataVariablesAndName(variables_and_name, count.value) - return result + try: + result = [] + for i in range(count.value): + assert variables_and_name is not None, "core.BNGetDebugDataVariablesByName returned None" + result.append(( + variables_and_name[i].name, variables_and_name[i].address, + _types.Type.create(core.BNNewTypeReference(variables_and_name[i].type)) + )) + return result + finally: + core.BNFreeDataVariablesAndName(variables_and_name, count.value) def get_data_variables_by_address(self, address: int) -> List[Tuple[str, _types.Type]]: """ The values in the tuples returned in the list is (DebugInfoParserName, TypeName, type) """ count = ctypes.c_ulonglong() variables_and_name = core.BNGetDebugDataVariablesByAddress(self.handle, address, count) - - result = [] - for i in range(count.value): - assert variables_and_name is not None, "core.BNGetDebugDataVariablesByAddress returned None" - result.append(( - variables_and_name[i].parser, variables_and_name[i].name, - _types.Type.create(core.BNNewTypeReference(variables_and_name[i].type)) - )) - - core.BNFreeDataVariableAndNameAndDebugParserList(variables_and_name, count.value) - return result + try: + result = [] + for i in range(count.value): + assert variables_and_name is not None, "core.BNGetDebugDataVariablesByAddress returned None" + result.append(( + variables_and_name[i].parser, variables_and_name[i].name, + _types.Type.create(core.BNNewTypeReference(variables_and_name[i].type)) + )) + return result + finally: + core.BNFreeDataVariableAndNameAndDebugParserList(variables_and_name, count.value) def remove_parser_info(self, parser_name: str): return core.BNRemoveDebugParserInfo(self.handle, parser_name) |
