forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 14
/
gdbutil
291 lines (270 loc) · 5.56 KB
/
gdbutil
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- gdb-script -*-
# Utility functions to pretty-print x86 segment/interrupt descriptors.
# To load this file, run "source gdbutil" in gdb.
# printdesc and printdescs are the main entry points.
# IA32 2007, Volume 3A, Table 3-2
set $STS_T16A = 0x1
set $STS_LDT = 0x2
set $STS_T16B = 0x3
set $STS_CG16 = 0x4
set $STS_TG = 0x5
set $STS_IG16 = 0x6
set $STS_TG16 = 0x7
set $STS_T32A = 0x9
set $STS_T32B = 0xB
set $STS_CG32 = 0xC
set $STS_IG32 = 0xE
set $STS_TG32 = 0xF
define outputsts
while 1
if $arg0 == $STS_T16A
echo STS_T16A
loop_break
end
if $arg0 == $STS_LDT
echo STS_LDT\
loop_break
end
if $arg0 == $STS_T16B
echo STS_T16B
loop_break
end
if $arg0 == $STS_CG16
echo STS_CG16
loop_break
end
if $arg0 == $STS_TG
echo STS_TG\ \
loop_break
end
if $arg0 == $STS_IG16
echo STS_IG16
loop_break
end
if $arg0 == $STS_TG16
echo STS_TG16
loop_break
end
if $arg0 == $STS_T32A
echo STS_T32A
loop_break
end
if $arg0 == $STS_T32B
echo STS_T32B
loop_break
end
if $arg0 == $STS_CG32
echo STS_CG32
loop_break
end
if $arg0 == $STS_IG32
echo STS_IG32
loop_break
end
if $arg0 == $STS_TG32
echo STS_TG32
loop_break
end
echo Reserved
loop_break
end
end
# IA32 2007, Volume 3A, Table 3-1
set $STA_X = 0x8
set $STA_E = 0x4
set $STA_C = 0x4
set $STA_W = 0x2
set $STA_R = 0x2
set $STA_A = 0x1
define outputsta
if $arg0 & $STA_X
# Code segment
echo code
if $arg0 & $STA_C
echo |STA_C
end
if $arg0 & $STA_R
echo |STA_R
end
else
# Data segment
echo data
if $arg0 & $STA_E
echo |STA_E
end
if $arg0 & $STA_W
echo |STA_W
end
end
if $arg0 & $STA_A
echo |STA_A
else
printf " "
end
end
# xv6-specific
set $SEG_KCODE = 1
set $SEG_KDATA = 2
set $SEG_KCPU = 3
set $SEG_UCODE = 4
set $SEG_UDATA = 5
set $SEG_TSS = 6
define outputcs
if ($arg0 & 4) == 0
if $arg0 >> 3 == $SEG_KCODE
printf "SEG_KCODE<<3"
end
if $arg0 >> 3 == $SEG_KDATA
printf "SEG_KDATA<<3"
end
if $arg0 >> 3 == $SEG_KCPU
printf "SEG_KCPU<<3"
end
if $arg0 >> 3 == $SEG_UCODE
printf "SEG_UCODE<<3"
end
if $arg0 >> 3 == $SEG_UDATA
printf "SEG_UDATA<<3"
end
if $arg0 >> 3 == $SEG_TSS
printf "SEG_TSS<<3"
end
if ($arg0 >> 3 < 1) + ($arg0 >> 3 > 6)
printf "GDT[%d]", $arg0 >> 3
end
else
printf "LDT[%d]", $arg0 >> 3
end
if ($arg0 & 3) > 0
printf "|"
outputdpl ($arg0&3)
end
end
define outputdpl
if $arg0 == 0
printf "DPL_KERN"
else
if $arg0 == 3
printf "DPL_USER"
else
printf "DPL%d", $arg0
end
end
end
define printdesc
if $argc != 1
echo Usage: printdesc expr
else
_printdesc ((uint*)&($arg0))[0] ((uint*)&($arg0))[1]
printf "\n"
end
end
document printdesc
Print an x86 segment or gate descriptor.
printdesc EXPR
EXPR must evaluate to a descriptor value. It can be of any C type.
end
define _printdesc
_printdesc1 $arg0 $arg1 ($arg1>>15&1) ($arg1>>13&3) ($arg1>>12&1) ($arg1>>8&15)
end
define _printdesc1
# 2:P 3:DPL 4:S 5:Type
if $arg2 == 0
printf "P = 0 (Not present)"
else
printf "type = "
if $arg4 == 0
# System segment
outputsts $arg5
printf " (0x%x) ", $arg5
_printsysdesc $arg0 $arg1 $arg5
else
# Code/data segment
outputsta $arg5
printf " "
_printsegdesc $arg0 $arg1
end
printf " DPL = "
outputdpl $arg3
printf " (%d)", $arg3
end
end
define _printsysdesc
# 2:Type
# GDB's || is buggy
if ($arg2 == $STS_TG) + (($arg2&7) == $STS_IG16) + (($arg2&7) == $STS_TG16)
# Gate descriptor
_printgate $arg2 ($arg0>>16) ($arg0&0xFFFF) ($arg1>>16)
else
# System segment descriptor
_printsegdesc $arg0 $arg1
end
end
define _printgate
# IA32 2007, Voume 3A, Figure 5-2
# 0:Type 1:CS 2:Offset 15..0 3:Offset 31..16
printf "CS = "
outputcs $arg1
printf " (%d)", $arg1
if (($arg0&7) == $STS_IG16) + (($arg0&7) == $STS_TG16)
printf " Offset = "
output/a $arg3 << 16 | $arg2
end
end
define _printsegdesc
# IA32 20007, Volume 3A, Figure 3-8 and Figure 4-1
_printsegdesc1 ($arg0>>16) ($arg1&0xFF) ($arg1>>24) ($arg0&0xFFFF) ($arg1>>16&15) ($arg1>>23&1)
if ($arg1>>12&1) == 1
printf " AVL = %d", $arg1>>20&1
if ($arg1>>11&1) == 0
# Data segment
if ($arg1>>22&1) == 0
printf " B = small (0) "
else
printf " B = big (1) "
end
else
# Code segment
printf " D = "
if ($arg1>>22&1) == 0
printf "16-bit (0)"
else
printf "32-bit (1)"
end
end
end
end
define _printsegdesc1
# 0:Base 0..15 1:Base 16..23 2:Base 24..32 3:Limit 0..15 4:Limit 16..19 5:G
printf "base = 0x%08x", $arg0 | ($arg1<<16) | ($arg2<<24)
printf " limit = 0x"
if $arg5 == 0
printf "%08x", $arg3 | ($arg4<<16)
else
printf "%08x", (($arg3 | ($arg4<<16)) << 12) | 0xFFF
end
end
define printdescs
if $argc < 1 || $argc > 2
echo Usage: printdescs expr [count]
else
if $argc == 1
_printdescs ($arg0) (sizeof($arg0)/sizeof(($arg0)[0]))
else
_printdescs ($arg0) ($arg1)
end
end
end
document printdescs
Print an array of x86 segment or gate descriptors.
printdescs EXPR [COUNT]
EXPR must evaluate to an array of descriptors.
end
define _printdescs
set $i = 0
while $i < $arg1
printf "[%d] ", $i
printdesc $arg0[$i]
set $i = $i + 1
end
end