-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel.py
More file actions
168 lines (134 loc) · 4.42 KB
/
kernel.py
File metadata and controls
168 lines (134 loc) · 4.42 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
kernel.py - A collection of gdb utilities for analyzing kernel core dumps
"""
import gdb
import re
from list_util import KIterator
#
# MACRO DEFINES - x86 specific for now!
#
PAGE_SHIFT = 12
PAGE_SIZE = 1 << PAGE_SHIFT
################################################################################
#
################################################################################
def backtrace(count=0):
bt = []
bt_tmpl = re.compile('#\d+ (?P<entry>.*)')
if count == 0:
count = ""
backtrace = gdb.execute("bt {0}".format(str(count)), True, True).strip().split('\n')
for line in backtrace:
m = bt_tmpl.search(line)
if m:
bt.append(m.group('entry'))
return bt
def backtrace_top():
return backtrace(1)
################################################################################
#
################################################################################
def dmesg(tail=10):
__log_buf = gdb.parse_and_eval('__log_buf').address
log_buf_len = gdb.parse_and_eval('log_buf_len')
log_end = gdb.parse_and_eval('log_end') & (log_buf_len-1)
dmesg = []
line = []
for i in range(log_end):
if not tail:
break
c = gdb.execute('printf "%c", ((char *)({0}))[{1}]'.format(
__log_buf, log_end - 1 - i), True, True)[0]
line.insert(0, c)
if c == '\n':
dmesg.insert(0, "".join(line))
line = []
tail -= 1
return "".join(dmesg)
################################################################################
#
################################################################################
# Constants
SWP_USED = gdb.parse_and_eval('SWP_USED')
SWP_WRITEOK = gdb.parse_and_eval('SWP_WRITEOK')
# Globals
nr_swapfiles = gdb.parse_and_eval('nr_swapfiles')
nr_swap_pages = gdb.parse_and_eval('nr_swap_pages')
total_swap_pages = gdb.parse_and_eval('total_swap_pages')
total_swapcache_pages = gdb.parse_and_eval('swapper_space.nrpages')
def _global_page_state(key):
return gdb.parse_and_eval('vm_stat[{0}].counter'.format(key))
def _totalram():
return int(gdb.parse_and_eval('totalram_pages'))
def _sharedram():
return 0
def _freeram():
return _global_page_state('NR_FREE_PAGES')
def _bufferram():
rv = 0
for p in KIterator('all_bdevs', 'struct block_device', 'bd_list'):
rv += int(p['bd_inode']['i_mapping']['nrpages'])
return rv
def _cached():
cached = _global_page_state('NR_FILE_PAGES')
cached -= total_swapcache_pages
cached -= _bufferram()
return cached
def _totalhigh():
return 0
def _freehigh():
# UNIMPLEMENTED
return -1
def _memunit():
return PAGE_SIZE
def _swapinfo():
nr_to_be_used = 0
for type_ in range(nr_swapfiles):
si = gdb.parse_and_eval("swap_info[{0}]".format(type_))
if si['flags'] & SWP_USED and si['flags'] & SWP_WRITEOK:
nr_to_be_used += si['inuse_pages']
print nr_to_be_used
freeswap = nr_swap_pages + nr_to_be_used
totalswap = total_swap_pages + nr_to_be_used
return {'freeswap' : int(freeswap),
'totalswap' : int(totalswap)}
def K(x):
return int(x << (PAGE_SHIFT - 10))
def mem_total():
return K(_totalram())
def mem_free():
return K(_freeram())
def mem_buff():
return K(_bufferram())
def mem_cached():
return K(_cached())
################################################################################
#
################################################################################
def ps_e():
dic = {}
for p in KIterator('init_task.tasks', 'struct task_struct', 'tasks'):
pid = int(p['pid'])
name = str(p['comm'])
dic[pid] = name
return dic
################################################################################
#
################################################################################
def __uname(name):
return gdb.execute("""printf "%s", init_uts_ns.name.""" + name, True, True)
def unames():
return __uname('sysname')
def unamen():
return __uname('nodename')
def unamer():
return __uname('release')
def unamev():
return __uname('version')
def unamem():
return __uname('machine')
def unamea():
return '{0} {1} {2} {3} {4}'.format(unames(), unamen(), unamer(), unamev(), unamem())
################################################################################
#
################################################################################