blob: 2fa7bb83885f068e05648448267b3f590bfb2ab9 [file] [log] [blame]
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -08001#
2# gdb helper commands and functions for Linux kernel debugging
3#
4# kernel log buffer dump
5#
6# Copyright (c) Siemens AG, 2011, 2012
7#
8# Authors:
9# Jan Kiszka <jan.kiszka@siemens.com>
10#
11# This work is licensed under the terms of the GNU GPL version 2.
12#
13
14import gdb
Leonard Crestez46d10a02017-07-12 14:34:19 -070015import sys
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080016
17from linux import utils
18
Joel Colledgeca210ba2019-10-18 20:19:26 -070019printk_log_type = utils.CachedType("struct printk_log")
20
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080021
22class LxDmesg(gdb.Command):
23 """Print Linux kernel log buffer."""
24
25 def __init__(self):
26 super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
27
28 def invoke(self, arg, from_tty):
André Draszikd6c97082017-06-02 14:46:51 -070029 log_buf_addr = int(str(gdb.parse_and_eval(
Leonard Crestezc4547562017-07-12 14:34:16 -070030 "(void *)'printk.c'::log_buf")).split()[0], 16)
André Draszikd6c97082017-06-02 14:46:51 -070031 log_first_idx = int(gdb.parse_and_eval("'printk.c'::log_first_idx"))
32 log_next_idx = int(gdb.parse_and_eval("'printk.c'::log_next_idx"))
33 log_buf_len = int(gdb.parse_and_eval("'printk.c'::log_buf_len"))
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080034
35 inf = gdb.inferiors()[0]
36 start = log_buf_addr + log_first_idx
37 if log_first_idx < log_next_idx:
38 log_buf_2nd_half = -1
39 length = log_next_idx - log_first_idx
Dom Coted21d5b9eb2016-05-23 16:25:19 -070040 log_buf = utils.read_memoryview(inf, start, length).tobytes()
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080041 else:
42 log_buf_2nd_half = log_buf_len - log_first_idx
Dom Coted21d5b9eb2016-05-23 16:25:19 -070043 a = utils.read_memoryview(inf, start, log_buf_2nd_half)
44 b = utils.read_memoryview(inf, log_buf_addr, log_next_idx)
45 log_buf = a.tobytes() + b.tobytes()
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080046
Joel Colledgeca210ba2019-10-18 20:19:26 -070047 length_offset = printk_log_type.get_type()['len'].bitpos // 8
48 text_len_offset = printk_log_type.get_type()['text_len'].bitpos // 8
49 time_stamp_offset = printk_log_type.get_type()['ts_nsec'].bitpos // 8
50 text_offset = printk_log_type.get_type().sizeof
51
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080052 pos = 0
53 while pos < log_buf.__len__():
Joel Colledgeca210ba2019-10-18 20:19:26 -070054 length = utils.read_u16(log_buf, pos + length_offset)
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080055 if length == 0:
56 if log_buf_2nd_half == -1:
57 gdb.write("Corrupted log buffer!\n")
58 break
59 pos = log_buf_2nd_half
60 continue
61
Joel Colledgeca210ba2019-10-18 20:19:26 -070062 text_len = utils.read_u16(log_buf, pos + text_len_offset)
63 text_start = pos + text_offset
64 text = log_buf[text_start:text_start + text_len].decode(
Leonard Crestez46d10a02017-07-12 14:34:19 -070065 encoding='utf8', errors='replace')
Joel Colledgeca210ba2019-10-18 20:19:26 -070066 time_stamp = utils.read_u64(log_buf, pos + time_stamp_offset)
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080067
Kieran Binghamb3b08422016-05-23 16:25:21 -070068 for line in text.splitlines():
Leonard Crestez46d10a02017-07-12 14:34:19 -070069 msg = u"[{time:12.6f}] {line}\n".format(
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080070 time=time_stamp / 1000000000.0,
Leonard Crestez46d10a02017-07-12 14:34:19 -070071 line=line)
72 # With python2 gdb.write will attempt to convert unicode to
73 # ascii and might fail so pass an utf8-encoded str instead.
74 if sys.hexversion < 0x03000000:
75 msg = msg.encode(encoding='utf8', errors='replace')
76 gdb.write(msg)
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080077
78 pos += length
79
80
81LxDmesg()