linux的crash有个好处就是可以方便打印结构体成员变量的offset, 有时候对汇编的时候, 需要偏移, 可惜crash需要一个活体才行, 不能单纯的vmlinux, 因为它就是这么设计的
gdb天生没有这个功能, 不过python可以实现
cat offset.py
import gdb
class Offsets(gdb.Command):
def __init__(self):
super (Offsets, self).__init__ ('offsets-of', gdb.COMMAND_DATA)
def invoke(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 1:
raise gdb.GdbError('offsets-of takes exactly 1 argument.')
stype = gdb.lookup_type('struct %s' % argv[0])
print argv[0], '{'
for field in stype.fields():
print ' [0x%x] %s' % (field.bitpos//8, field.name)
print '}'
Offsets()
gdb vmlinux -x ~/offset.py
(gdb) offsets-of task_struct
task_struct {
[0x0] thread_info
[0x8] state
[0x10] stack
[0x18] usage
[0x1c] flags
[0x20] ptrace
[0x28] wake_entry
[0x30] on_cpu
[0x34] cpu
[0x38] wakee_flips
[0x40] wakee_flip_decay_ts
其实还是挺方便的, 省得用0指针来转