Skip to content

Commit

Permalink
Merge tag 'printk-for-5.20-sane' of git://git.kernel.org/pub/scm/linu…
Browse files Browse the repository at this point in the history
…x/kernel/git/printk/linux

Pull printk updates from Petr Mladek:

 - Allow reading kernel log in gdb even on 32 bits systems

 - More granular check of the buffer usage in printf selftest

 - Clang warning fix

* tag 'printk-for-5.20-sane' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux:
  lib/test_printf.c: fix clang -Wformat warnings
  scripts/gdb: fix 'lx-dmesg' on 32 bits arch
  lib/test_printf.c: split write-beyond-buffer check in two
  • Loading branch information
torvalds committed Aug 5, 2022
2 parents 965a9d7 + 96dd9a2 commit a1b0275
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
21 changes: 17 additions & 4 deletions lib/test_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
#define PAD_SIZE 16
#define FILL_CHAR '$'

#define NOWARN(option, comment, block) \
__diag_push(); \
__diag_ignore_all(#option, comment); \
block \
__diag_pop();

KSTM_MODULE_GLOBALS();

static char *test_buffer __initdata;
Expand Down Expand Up @@ -78,12 +84,17 @@ do_test(int bufsize, const char *expect, int elen,
return 1;
}

if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
bufsize, fmt);
return 1;
}

if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", bufsize, fmt);
return 1;
}

if (memcmp(test_buffer, expect, written)) {
pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
bufsize, fmt, test_buffer, written, expect);
Expand Down Expand Up @@ -154,9 +165,11 @@ test_number(void)
test("0x1234abcd ", "%#-12x", 0x1234abcd);
test(" 0x1234abcd", "%#12x", 0x1234abcd);
test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
})
/*
* POSIX/C99: »The result of converting zero with an explicit
* precision of zero shall be no characters.« Hence the output
Expand Down
9 changes: 3 additions & 6 deletions scripts/gdb/linux/dmesg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
prb_desc_ring_type = utils.CachedType("struct prb_desc_ring")
prb_data_ring_type = utils.CachedType("struct prb_data_ring")
printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer")
atomic_long_type = utils.CachedType("atomic_long_t")

class LxDmesg(gdb.Command):
"""Print Linux kernel log buffer."""
Expand Down Expand Up @@ -68,8 +67,6 @@ def invoke(self, arg, from_tty):
off = prb_data_ring_type.get_type()['data'].bitpos // 8
text_data_addr = utils.read_ulong(text_data_ring, off)

counter_off = atomic_long_type.get_type()['counter'].bitpos // 8

sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8

off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8
Expand All @@ -89,9 +86,9 @@ def invoke(self, arg, from_tty):

# read in tail and head descriptor ids
off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8
tail_id = utils.read_u64(desc_ring, off + counter_off)
tail_id = utils.read_atomic_long(desc_ring, off)
off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8
head_id = utils.read_u64(desc_ring, off + counter_off)
head_id = utils.read_atomic_long(desc_ring, off)

did = tail_id
while True:
Expand All @@ -102,7 +99,7 @@ def invoke(self, arg, from_tty):
desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes()

# skip non-committed record
state = 3 & (utils.read_u64(desc, sv_off + counter_off) >> desc_flags_shift)
state = 3 & (utils.read_atomic_long(desc, sv_off) >> desc_flags_shift)
if state != desc_committed and state != desc_finalized:
if did == head_id:
break
Expand Down
14 changes: 12 additions & 2 deletions scripts/gdb/linux/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ def get_type(self):


long_type = CachedType("long")

atomic_long_type = CachedType("atomic_long_t")

def get_long_type():
global long_type
return long_type.get_type()


def offset_of(typeobj, field):
element = gdb.Value(0).cast(typeobj)
return int(str(element[field].address).split()[0], 16)
Expand Down Expand Up @@ -129,6 +128,17 @@ def read_ulong(buffer, offset):
else:
return read_u32(buffer, offset)

atomic_long_counter_offset = atomic_long_type.get_type()['counter'].bitpos
atomic_long_counter_sizeof = atomic_long_type.get_type()['counter'].type.sizeof

def read_atomic_long(buffer, offset):
global atomic_long_counter_offset
global atomic_long_counter_sizeof

if atomic_long_counter_sizeof == 8:
return read_u64(buffer, offset + atomic_long_counter_offset)
else:
return read_u32(buffer, offset + atomic_long_counter_offset)

target_arch = None

Expand Down

0 comments on commit a1b0275

Please sign in to comment.