Skip to content

Pretty printk-ing inside the Linux Kernel

License

Notifications You must be signed in to change notification settings

tpiekarski/pretty-printk

Repository files navigation

pretty-printk

Build Status

Overview / Usage / Features / License / Links

Debugging the Linux Kernel in a way most developers are familiar with is challenging. Setting breakpoints, stepping through code and watching for the last important variable to change is hard. Sometimes just printing values to the Kernel Ring Buffer can be a better and especially faster approach to debugging. After having print almost the hundredths message with printk it started to feel dull and pointed me to tinker with its output.

When developing for the Web it is quite common to pretty such output and I was wondering why not to take my chances and pretty the printk-ing inside the Kernel. The goal is to ease on the hand the process of printing and on the other one it should increase readability when running dmesg.

#include "pretty_printk.h"

  // [...]

  pp_warn("Shortcut for severity level and flushing '\\n' character");

  // Extended Metadata (function, filename and line number)
  // pp_debug will only print when PP_DEBUG is defined
  // ---
  pp_debug("Extended metadata while printk-ing with debug=1 or PP_DEBUG");

  // Dumping multiple variables with valid type specifiers
  // i: integer, c: character, s: character array (string)
  // ---
  pp_dump("ics", 10, 'a', "some char array");

  // Walking through code
  // ---
  pp_walker();

  // Color Output
  // pp_<color> will only colorize when either PP_COLORS is defined
  // ---
  pp_info("Colorize dmesg output with colors=1 or PP_COLORS");
  pp_info("Example colors: %s, %s, %s... and a few more.", pp_red("red"),
  pp_green("green"), pp_blue("blue"));

  // Shortcut-ed output of condition for tracing not-yet-understood logic
  // ---
  int x_pos = 16;
  int x = 64;

  if (x_pos <= x) {
    pp_true("x_pos <= x");
  } else {
    pp_false("x_pos <= x");

    goto out;
  }

   // [...]
[ 12.300004] pp_demo_module: Shortcut for severity level and flushing '\n' character
[ 12.300006] pp_demo_module (pretty_printk_demo_init @ pretty_printk_demo.c, 61): Extended metadata while printk-ing with debug=1 or PP_DEBUG
[ 12.300010] pp_demo_module: Dumping data
[ 12.300011]  1. 10
[ 12.300012]  2. a
[ 12.300013]  3. some char array
[ 12.300015] pp_demo_module (pretty_printk_demo_init @ pretty_printk_demo.c, 70): It worked up to this line
[ 12.300017] pp_demo_module: Colorize dmesg output with colors=1 or PP_COLORS
[ 12.300018] pp_demo_module: Example colors: red, green, blue... and a few more. Yes, colors are in the output, but not in this README :)
[ 12.300019] pp_demo_module (pretty_printk_demo_init @ pretty_printk_demo.c, 85): x_pos <= x is true

For testing, debugging and looking at the features the repository provides a demo module pp_demo_module to illustrate the different features. For building just call make and insert/remove the module in the common tools. Inside the init function all features are demonstrated.

Feature Issue Merge Commit Status
Metadata when printk-ing #3 7fcb473 done
Severity shortcuts #4 3dfc02d done
Switch for debugging #5 62be4d5 done
Pretty print of multiple variables #6 e11d951 done
Walking Macro #7 ab52416 done
Shortcut for discovering conditional expressions #8 f35b3ec done
Generic testing pipeline with Travis CI #9 5b459c7 done
Testing pipeline for different major Kernel versions #10 - todo
Testing pipeline for different architectures #11 - todo
Convenient throttling of printk #13 - todo
Colorize output #14 530fa57 done

pretty-printk is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

pretty-printk is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with pretty-printk. If not, see https://www.gnu.org/licenses/.