-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtarget.py
More file actions
129 lines (105 loc) · 4 KB
/
target.py
File metadata and controls
129 lines (105 loc) · 4 KB
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
# -*- coding: utf-8 -*-
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
import gdb
import sys
from kdumpfile import kdumpfile, KDUMP_KVADDR
from kdumpfile.exceptions import *
import addrxlat
class SymbolCallback(object):
"addrxlat symbolic callback"
def __init__(self, ctx=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ctx = ctx
def __call__(self, symtype, *args):
if self.ctx is not None:
try:
return self.ctx.next_cb_sym(symtype, *args)
except addrxlat.BaseException:
self.ctx.clear_err()
if symtype == addrxlat.SYM_VALUE:
ms = gdb.lookup_minimal_symbol(args[0])
if ms is not None:
return int(ms.value().address)
raise addrxlat.NoDataError()
class Target(gdb.Target):
def __init__(self, debug=False):
super().__init__()
self.debug = debug
self.shortname = "kdumpfile"
self.longname = "Use a Linux kernel kdump file as a target"
self.register()
def open(self, filename, from_tty):
if len(gdb.objfiles()) == 0:
raise gdb.GdbError("kdumpfile target requires kernel to be already loaded for symbol resolution")
try:
self.kdump = kdumpfile(file=filename)
except Exception as e:
raise gdb.GdbError("Failed to open `{}': {}"
.format(filename, str(e)))
self.kdump.attr['addrxlat.ostype'] = 'linux'
ctx = self.kdump.get_addrxlat_ctx()
ctx.cb_sym = SymbolCallback(ctx)
KERNELOFFSET = "linux.vmcoreinfo.lines.KERNELOFFSET"
try:
attr = self.kdump.attr.get(KERNELOFFSET, "0")
self.base_offset = int(attr, base=16)
except Exception as e:
self.base_offset = 0
vmlinux = gdb.objfiles()[0].filename
# Load the kernel at the relocated address
# Unfortunately, the percpu section has an offset of 0 and
# ends up getting placed at the offset base. This is easy
# enough to handle in the percpu code.
result = gdb.execute("add-symbol-file {} -o {:#x}"
.format(vmlinux, self.base_offset),
to_string=True)
if self.debug:
print(result)
# Clear out the old symbol cache
gdb.execute("file {}".format(vmlinux))
def close(self):
try:
self.unregister()
except:
pass
del self.kdump
@classmethod
def report_error(cls, addr, length, error):
print("Error while reading {:d} bytes from {:#x}: {}"
.format(length, addr, str(error)),
file=sys.stderr)
def xfer_partial(self, obj, annex, readbuf, writebuf, offset, ln):
ret = -1
if obj == self.TARGET_OBJECT_MEMORY:
try:
r = self.kdump.read(KDUMP_KVADDR, offset, ln)
readbuf[:] = r
ret = ln
except EOFException as e:
if self.debug:
self.report_error(offset, ln, e)
raise gdb.TargetXferEof(str(e))
except NoDataException as e:
if self.debug:
self.report_error(offset, ln, e)
raise gdb.TargetXferUnavailable(str(e))
except AddressTranslationException as e:
if self.debug:
self.report_error(offset, ln, e)
raise gdb.TargetXferUnavailable(str(e))
else:
raise IOError("Unknown obj type")
return ret
def thread_alive(self, ptid):
return True
def pid_to_str(self, ptid):
return "pid {:d}".format(ptid[1])
def fetch_registers(self, register):
return False
def prepare_to_store(self, thread):
pass
# We don't need to store anything; The regcache is already written.
def store_registers(self, thread):
pass
def has_execution(self, ptid):
return False