Skip to content

Commit 64d6057

Browse files
committed
Rewrite gdbserver vFile command with real fd
1 parent f09bab8 commit 64d6057

File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

qiling/debugger/gdb/gdb.py

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -673,65 +673,42 @@ def handle_v(subcmd):
673673
self.send("")
674674

675675
elif subcmd.startswith('File:open'):
676-
self.lib_path = subcmd.split(':')[-1].split(',')[0]
677-
self.lib_path = unhexlify(self.lib_path).decode(encoding='UTF-8')
676+
(file_path, flags, mode) = subcmd.split(':')[-1].split(',')
677+
file_path = unhexlify(file_path).decode(encoding='UTF-8')
678+
flags = int(flags, base=16)
679+
mode = int(mode, base=16)
680+
if file_path.startswith(self.rootfs_abspath):
681+
file_abspath = file_path
682+
else:
683+
file_abspath = self.ql.os.transform_to_real_path(file_path)
678684

679-
if self.lib_path != "just probing":
680-
if self.lib_path.startswith(self.rootfs_abspath):
681-
self.lib_abspath = self.lib_path
682-
else:
683-
self.lib_abspath = self.ql.os.transform_to_real_path(self.lib_path)
684-
685-
logging.debug("gdb> target file: %s" % (self.lib_abspath))
686-
687-
if os.path.exists(self.lib_abspath):
688-
self.send("F5")
689-
else:
690-
self.send("F0")
685+
logging.debug("gdb> target file: %s" % (file_abspath))
686+
if os.path.exists(file_abspath) and not (file_path).startswith("/proc"):
687+
fd = os.open(file_abspath, flags, mode)
688+
self.send("F%x" % fd)
691689
else:
692-
self.send("F0")
690+
self.send("F-1")
693691

694692
elif subcmd.startswith('File:pread:'):
693+
(fd, count, offset) = subcmd.split(':')[-1].split(',')
695694

696-
offset = subcmd.split(',')[-1]
697-
count = subcmd.split(',')[-2]
698-
offset = ((int(offset, base=16)))
699-
count = ((int(count, base=16)))
700-
701-
if os.path.exists(self.lib_abspath) and not (self.lib_path).startswith("/proc"):
695+
fd = int(fd, base=16)
696+
offset = int(offset, base=16)
697+
count = int(count, base=16)
702698

703-
with open(self.lib_abspath, "rb") as f:
704-
preadheader = f.read()
705-
706-
if offset != 0:
707-
shift_count = offset + count
708-
read_offset = preadheader[offset:shift_count]
709-
else:
710-
read_offset = preadheader[offset:count]
699+
data = os.pread(fd, count, offset)
700+
size = len(data)
701+
data = self.bin_to_escstr(data)
711702

712-
preadheader_len = len(preadheader)
713-
714-
read_offset = self.bin_to_escstr(read_offset)
715-
716-
if count == 1 and (preadheader_len >= offset):
717-
if read_offset:
718-
self.send(b'F1;' + (read_offset))
719-
else:
720-
self.send('F1;\x00')
721-
722-
elif count > 1:
723-
self.send(("F%x;" % len(read_offset)).encode() + (read_offset))
724-
725-
else:
726-
self.send("F0;")
727-
728-
elif re.match("\/proc\/.*\/maps", self.lib_abspath):
729-
self.send("F0;")
730-
703+
if data:
704+
self.send(("F%x;" % size).encode() + (data))
731705
else:
732706
self.send("F0;")
733707

734708
elif subcmd.startswith('File:close'):
709+
fd = subcmd.split(':')[-1]
710+
fd = int(fd, base=16)
711+
os.close(fd)
735712
self.send("F0")
736713

737714
elif subcmd.startswith('Kill'):

0 commit comments

Comments
 (0)