-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path.gdbinit
132 lines (96 loc) · 3.76 KB
/
.gdbinit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
python
# Custom Printers to format cstr_t and str_t objects like real strings.
class StrPrinter(gdb.ValuePrinter):
"""Print a str or cstr object."""
def __init__(self, val):
self.val = val
def to_string(self):
if self.val is None:
return '(null)'
if self.val['str'] == 0 or self.val['len'] == 0:
return '(empty)'
return f'"{self.val['str'].string(length=self.val['len'])}"'
def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter('custom_str_printers')
pp.add_printer('cstr', '^cstr$', StrPrinter)
pp.add_printer('str', '^str$', StrPrinter)
return pp
gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer())
# https://stackoverflow.com/questions/33049201/gdb-add-symbol-file-all-sections-and-load-address
#
# add-symbol-file-all <file> <address>
#
# Loads the symbols from the given file at the specified base address.
# Useful for debugging dynamically linked libraries.
import subprocess
import re
def relocatesections(filename, addr):
p = subprocess.Popen(["build/toolchain/bin/x86_64-linux-musl-readelf", "-SW", filename], stdout = subprocess.PIPE)
sections = []
textaddr = '0'
for line in p.stdout.readlines():
line = line.decode("utf-8").strip()
if not line.startswith('[') or line.startswith('[Nr]'):
continue
line = re.sub(r' +', ' ', line)
line = re.sub(r'\[ *(\d+)\]', r'\g<1>', line)
fieldsvalue = line.split(' ')
fieldsname = ['number', 'name', 'type', 'addr', 'offset', 'size', 'entsize', 'flags', 'link', 'info', 'addralign']
sec = dict(zip(fieldsname, fieldsvalue))
if sec['number'] == '0':
continue
sections.append(sec)
if sec['name'] == '.text':
textaddr = sec['addr']
return (textaddr, sections)
class AddSymbolFileAll(gdb.Command):
"""The right version for add-symbol-file"""
def __init__(self):
super(AddSymbolFileAll, self).__init__("add-symbol-file-all", gdb.COMMAND_USER)
self.dont_repeat()
def invoke(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
filename = argv[0]
if len(argv) > 1:
offset = int(str(gdb.parse_and_eval(argv[1])), 0)
else:
offset = 0
(textaddr, sections) = relocatesections(filename, offset)
cmd = "add-symbol-file %s 0x%08x" % (filename, int(textaddr, 16) + offset)
for s in sections:
addr = int(s['addr'], 16)
if s['name'] == '.text' or addr == 0:
continue
cmd += " -s %s 0x%08x" % (s['name'], addr + offset)
gdb.execute(cmd)
class RemoveSymbolFileAll(gdb.Command):
"""The right version for remove-symbol-file"""
def __init__(self):
super(RemoveSymbolFileAll, self).__init__("remove-symbol-file-all", gdb.COMMAND_USER)
self.dont_repeat()
def invoke(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
filename = argv[0]
if len(argv) > 1:
offset = int(str(gdb.parse_and_eval(argv[1])), 0)
else:
offset = 0
(textaddr, _) = relocatesections(filename, offset)
cmd = "remove-symbol-file -a 0x%08x" % (int(textaddr, 16) + offset)
gdb.execute(cmd)
AddSymbolFileAll()
RemoveSymbolFileAll()
end
set disassembly-flavor intel
add-symbol-file build/kernel.elf
# Loading in-kernel debugging info is too slow to be used while also
# debugging with gdb. We disable it via the runtime option before the
# kernel starts.
b kmain
commands
set variable is_debug_enabled = 0
add-symbol-file-all build/musl/x86_64-linux-musl/lib/libc.so 0x7fc0000000
add-symbol-file-all build/sbin/init/init 0x00000000000
#b _dlstart_c
#b _start_c
end