cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dmesg.py (5967B)


      1#
      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
     15import sys
     16
     17from linux import utils
     18
     19printk_info_type = utils.CachedType("struct printk_info")
     20prb_data_blk_lpos_type = utils.CachedType("struct prb_data_blk_lpos")
     21prb_desc_type = utils.CachedType("struct prb_desc")
     22prb_desc_ring_type = utils.CachedType("struct prb_desc_ring")
     23prb_data_ring_type = utils.CachedType("struct prb_data_ring")
     24printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer")
     25atomic_long_type = utils.CachedType("atomic_long_t")
     26
     27class LxDmesg(gdb.Command):
     28    """Print Linux kernel log buffer."""
     29
     30    def __init__(self):
     31        super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
     32
     33    def invoke(self, arg, from_tty):
     34        inf = gdb.inferiors()[0]
     35
     36        # read in prb structure
     37        prb_addr = int(str(gdb.parse_and_eval("(void *)'printk.c'::prb")).split()[0], 16)
     38        sz = printk_ringbuffer_type.get_type().sizeof
     39        prb = utils.read_memoryview(inf, prb_addr, sz).tobytes()
     40
     41        # read in descriptor ring structure
     42        off = printk_ringbuffer_type.get_type()['desc_ring'].bitpos // 8
     43        addr = prb_addr + off
     44        sz = prb_desc_ring_type.get_type().sizeof
     45        desc_ring = utils.read_memoryview(inf, addr, sz).tobytes()
     46
     47        # read in descriptor count, size, and address
     48        off = prb_desc_ring_type.get_type()['count_bits'].bitpos // 8
     49        desc_ring_count = 1 << utils.read_u32(desc_ring, off)
     50        desc_sz = prb_desc_type.get_type().sizeof
     51        off = prb_desc_ring_type.get_type()['descs'].bitpos // 8
     52        desc_addr = utils.read_ulong(desc_ring, off)
     53
     54        # read in info size and address
     55        info_sz = printk_info_type.get_type().sizeof
     56        off = prb_desc_ring_type.get_type()['infos'].bitpos // 8
     57        info_addr = utils.read_ulong(desc_ring, off)
     58
     59        # read in text data ring structure
     60        off = printk_ringbuffer_type.get_type()['text_data_ring'].bitpos // 8
     61        addr = prb_addr + off
     62        sz = prb_data_ring_type.get_type().sizeof
     63        text_data_ring = utils.read_memoryview(inf, addr, sz).tobytes()
     64
     65        # read in text data size and address
     66        off = prb_data_ring_type.get_type()['size_bits'].bitpos // 8
     67        text_data_sz = 1 << utils.read_u32(text_data_ring, off)
     68        off = prb_data_ring_type.get_type()['data'].bitpos // 8
     69        text_data_addr = utils.read_ulong(text_data_ring, off)
     70
     71        counter_off = atomic_long_type.get_type()['counter'].bitpos // 8
     72
     73        sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8
     74
     75        off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8
     76        begin_off = off + (prb_data_blk_lpos_type.get_type()['begin'].bitpos // 8)
     77        next_off = off + (prb_data_blk_lpos_type.get_type()['next'].bitpos // 8)
     78
     79        ts_off = printk_info_type.get_type()['ts_nsec'].bitpos // 8
     80        len_off = printk_info_type.get_type()['text_len'].bitpos // 8
     81
     82        # definitions from kernel/printk/printk_ringbuffer.h
     83        desc_committed = 1
     84        desc_finalized = 2
     85        desc_sv_bits = utils.get_long_type().sizeof * 8
     86        desc_flags_shift = desc_sv_bits - 2
     87        desc_flags_mask = 3 << desc_flags_shift
     88        desc_id_mask = ~desc_flags_mask
     89
     90        # read in tail and head descriptor ids
     91        off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8
     92        tail_id = utils.read_u64(desc_ring, off + counter_off)
     93        off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8
     94        head_id = utils.read_u64(desc_ring, off + counter_off)
     95
     96        did = tail_id
     97        while True:
     98            ind = did % desc_ring_count
     99            desc_off = desc_sz * ind
    100            info_off = info_sz * ind
    101
    102            desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes()
    103
    104            # skip non-committed record
    105            state = 3 & (utils.read_u64(desc, sv_off + counter_off) >> desc_flags_shift)
    106            if state != desc_committed and state != desc_finalized:
    107                if did == head_id:
    108                    break
    109                did = (did + 1) & desc_id_mask
    110                continue
    111
    112            begin = utils.read_ulong(desc, begin_off) % text_data_sz
    113            end = utils.read_ulong(desc, next_off) % text_data_sz
    114
    115            info = utils.read_memoryview(inf, info_addr + info_off, info_sz).tobytes()
    116
    117            # handle data-less record
    118            if begin & 1 == 1:
    119                text = ""
    120            else:
    121                # handle wrapping data block
    122                if begin > end:
    123                    begin = 0
    124
    125                # skip over descriptor id
    126                text_start = begin + utils.get_long_type().sizeof
    127
    128                text_len = utils.read_u16(info, len_off)
    129
    130                # handle truncated message
    131                if end - text_start < text_len:
    132                    text_len = end - text_start
    133
    134                text_data = utils.read_memoryview(inf, text_data_addr + text_start,
    135                                                  text_len).tobytes()
    136                text = text_data[0:text_len].decode(encoding='utf8', errors='replace')
    137
    138            time_stamp = utils.read_u64(info, ts_off)
    139
    140            for line in text.splitlines():
    141                msg = u"[{time:12.6f}] {line}\n".format(
    142                    time=time_stamp / 1000000000.0,
    143                    line=line)
    144                # With python2 gdb.write will attempt to convert unicode to
    145                # ascii and might fail so pass an utf8-encoded str instead.
    146                if sys.hexversion < 0x03000000:
    147                    msg = msg.encode(encoding='utf8', errors='replace')
    148                gdb.write(msg)
    149
    150            if did == head_id:
    151                break
    152            did = (did + 1) & desc_id_mask
    153
    154
    155LxDmesg()