|
20 | 20 | from collections import defaultdict, namedtuple |
21 | 21 | from dataclasses import dataclass |
22 | 22 | from pathlib import Path |
| 23 | +from tempfile import NamedTemporaryFile |
23 | 24 |
|
| 25 | +# third-party imports |
24 | 26 | from idascript import IDA |
25 | 27 | from numbat import SourcetrailDB |
26 | | - |
27 | | -# third-party imports |
28 | 28 | from quokka import Function, Program |
29 | 29 | from quokka.types import FunctionType |
30 | 30 | from rich.progress import ( |
@@ -216,16 +216,14 @@ def add_source_file( |
216 | 216 | info: DecompiledFunction, |
217 | 217 | log_prefix: str = "", |
218 | 218 | ) -> bool: |
219 | | - tmp = Path("/tmp/" + mangled_name) |
220 | | - with open(tmp, "w") as f: |
221 | | - f.write(info.text) |
222 | | - |
223 | | - # Record file |
224 | | - file_id = db.record_file(Path(tmp)) # , indexed=False) |
225 | | - if file_id is None: |
226 | | - return False |
227 | | - db.record_file_language(file_id, "cpp") |
228 | | - tmp.unlink() # QUESTION: Maybe we want to keep it for further analyses ? |
| 219 | + with NamedTemporaryFile(mode="wt", delete_on_close=False) as tmp: |
| 220 | + tmp.write(info.text) |
| 221 | + tmp.close() |
| 222 | + # Record file |
| 223 | + file_id = db.record_file(Path(tmp.name), name=mangled_name) |
| 224 | + if file_id is None: |
| 225 | + return False |
| 226 | + db.record_file_language(file_id, "cpp") |
229 | 227 |
|
230 | 228 | # Add the function to the file |
231 | 229 | logging.debug(f"{log_prefix}: add function to file {file_id}") |
@@ -285,7 +283,11 @@ def map_binary(db: SourcetrailDB, program_path: Path) -> bool: |
285 | 283 | f.name, |
286 | 284 | parent_id=None, |
287 | 285 | is_indexed=not is_imp, |
288 | | - ) # , hover_display=f"{f.type.name.lower()} function") |
| 286 | + ) |
| 287 | + if f_id is None: |
| 288 | + logging.error(f"{log_prefix}: error while recording function in db") |
| 289 | + progress.update(func_map, advance=1) |
| 290 | + continue |
289 | 291 | f_mapping[f_addr] = f_id |
290 | 292 |
|
291 | 293 | if not is_imp: |
|
0 commit comments