|
4 | 4 | import platform
|
5 | 5 | import sys
|
6 | 6 | import traceback
|
| 7 | +import tempfile |
7 | 8 |
|
8 | 9 | import binaryninja
|
9 | 10 | from binaryninja import BinaryView, Symbol, SymbolType, Type, Structure, StructureType, FunctionGraphType, LowLevelILOperation, MediumLevelILOperation
|
@@ -395,6 +396,8 @@ def __init__(self, bv):
|
395 | 396 | self.old_symbols = []
|
396 | 397 | self.old_dvs = set()
|
397 | 398 |
|
| 399 | + self.temp_file = None |
| 400 | + |
398 | 401 | # ensure bv path adheres to OS convention
|
399 | 402 | if platform.system() == 'Windows':
|
400 | 403 | self.bv.file.original_filename = self.bv.file.original_filename.replace('/', '\\')
|
@@ -573,8 +576,24 @@ def exec(self):
|
573 | 576 | self.connecting = True
|
574 | 577 | fpath = self.bv.file.original_filename
|
575 | 578 |
|
| 579 | + # TODO: Detect modifications and run from tmp if any exist #60 |
| 580 | + run_from_tmp = False |
576 | 581 | if not os.path.exists(fpath):
|
577 |
| - raise Exception('cannot find debug target: ' + fpath) |
| 582 | + # Original target was deleted |
| 583 | + run_from_tmp = True |
| 584 | + |
| 585 | + if run_from_tmp: |
| 586 | + self.temp_file = tempfile.NamedTemporaryFile(prefix="bndebugfile_") |
| 587 | + temp_name = self.temp_file.name |
| 588 | + # macOS symlinks /tmp to /private/tmp |
| 589 | + temp_name = os.path.realpath(temp_name) |
| 590 | + |
| 591 | + self.bv.file.raw.save(temp_name) |
| 592 | + os.chmod(temp_name, 0o700) |
| 593 | + |
| 594 | + # Swap the real path for this temp one |
| 595 | + fpath = temp_name |
| 596 | + self.modules.translations[fpath] = self.bv.file.original_filename |
578 | 597 |
|
579 | 598 | adapter = DebugAdapter.get_new_adapter(self.adapter_type, stdout=self.on_stdout)
|
580 | 599 | self.adapter = QueuedAdapter.QueuedAdapter(adapter)
|
@@ -641,6 +660,10 @@ def quit(self):
|
641 | 660 | self.remote_arch = None
|
642 | 661 | self.memory_dirty()
|
643 | 662 |
|
| 663 | + # Clean up temporary dumped file |
| 664 | + if self.temp_file is not None: |
| 665 | + self.temp_file = None |
| 666 | + |
644 | 667 | def restart(self):
|
645 | 668 | self.quit()
|
646 | 669 | time.sleep(1)
|
|
0 commit comments