printk-index.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ============
  3. Printk Index
  4. ============
  5. There are many ways how to monitor the state of the system. One important
  6. source of information is the system log. It provides a lot of information,
  7. including more or less important warnings and error messages.
  8. There are monitoring tools that filter and take action based on messages
  9. logged.
  10. The kernel messages are evolving together with the code. As a result,
  11. particular kernel messages are not KABI and never will be!
  12. It is a huge challenge for maintaining the system log monitors. It requires
  13. knowing what messages were updated in a particular kernel version and why.
  14. Finding these changes in the sources would require non-trivial parsers.
  15. Also it would require matching the sources with the binary kernel which
  16. is not always trivial. Various changes might be backported. Various kernel
  17. versions might be used on different monitored systems.
  18. This is where the printk index feature might become useful. It provides
  19. a dump of printk formats used all over the source code used for the kernel
  20. and modules on the running system. It is accessible at runtime via debugfs.
  21. The printk index helps to find changes in the message formats. Also it helps
  22. to track the strings back to the kernel sources and the related commit.
  23. User Interface
  24. ==============
  25. The index of printk formats are split in into separate files. The files are
  26. named according to the binaries where the printk formats are built-in. There
  27. is always "vmlinux" and optionally also modules, for example::
  28. /sys/kernel/debug/printk/index/vmlinux
  29. /sys/kernel/debug/printk/index/ext4
  30. /sys/kernel/debug/printk/index/scsi_mod
  31. Note that only loaded modules are shown. Also printk formats from a module
  32. might appear in "vmlinux" when the module is built-in.
  33. The content is inspired by the dynamic debug interface and looks like::
  34. $> head -1 /sys/kernel/debug/printk/index/vmlinux; shuf -n 5 vmlinux
  35. # <level[,flags]> filename:line function "format"
  36. <5> block/blk-settings.c:661 disk_stack_limits "%s: Warning: Device %s is misaligned\n"
  37. <4> kernel/trace/trace.c:8296 trace_create_file "Could not create tracefs '%s' entry\n"
  38. <6> arch/x86/kernel/hpet.c:144 _hpet_print_config "hpet: %s(%d):\n"
  39. <6> init/do_mounts.c:605 prepare_namespace "Waiting for root device %s...\n"
  40. <6> drivers/acpi/osl.c:1410 acpi_no_auto_serialize_setup "ACPI: auto-serialization disabled\n"
  41. , where the meaning is:
  42. - :level: log level value: 0-7 for particular severity, -1 as default,
  43. 'c' as continuous line without an explicit log level
  44. - :flags: optional flags: currently only 'c' for KERN_CONT
  45. - :filename\:line: source filename and line number of the related
  46. printk() call. Note that there are many wrappers, for example,
  47. pr_warn(), pr_warn_once(), dev_warn().
  48. - :function: function name where the printk() call is used.
  49. - :format: format string
  50. The extra information makes it a bit harder to find differences
  51. between various kernels. Especially the line number might change
  52. very often. On the other hand, it helps a lot to confirm that
  53. it is the same string or find the commit that is responsible
  54. for eventual changes.
  55. printk() Is Not a Stable KABI
  56. =============================
  57. Several developers are afraid that exporting all these implementation
  58. details into the user space will transform particular printk() calls
  59. into KABI.
  60. But it is exactly the opposite. printk() calls must _not_ be KABI.
  61. And the printk index helps user space tools to deal with this.
  62. Subsystem specific printk wrappers
  63. ==================================
  64. The printk index is generated using extra metadata that are stored in
  65. a dedicated .elf section ".printk_index". It is achieved using macro
  66. wrappers doing __printk_index_emit() together with the real printk()
  67. call. The same technique is used also for the metadata used by
  68. the dynamic debug feature.
  69. The metadata are stored for a particular message only when it is printed
  70. using these special wrappers. It is implemented for the commonly
  71. used printk() calls, including, for example, pr_warn(), or pr_once().
  72. Additional changes are necessary for various subsystem specific wrappers
  73. that call the original printk() via a common helper function. These needs
  74. their own wrappers adding __printk_index_emit().
  75. Only few subsystem specific wrappers have been updated so far,
  76. for example, dev_printk(). As a result, the printk formats from
  77. some subsystes can be missing in the printk index.
  78. Subsystem specific prefix
  79. =========================
  80. The macro pr_fmt() macro allows to define a prefix that is printed
  81. before the string generated by the related printk() calls.
  82. Subsystem specific wrappers usually add even more complicated
  83. prefixes.
  84. These prefixes can be stored into the printk index metadata
  85. by an optional parameter of __printk_index_emit(). The debugfs
  86. interface might then show the printk formats including these prefixes.
  87. For example, drivers/acpi/osl.c contains::
  88. #define pr_fmt(fmt) "ACPI: OSL: " fmt
  89. static int __init acpi_no_auto_serialize_setup(char *str)
  90. {
  91. acpi_gbl_auto_serialize_methods = FALSE;
  92. pr_info("Auto-serialization disabled\n");
  93. return 1;
  94. }
  95. This results in the following printk index entry::
  96. <6> drivers/acpi/osl.c:1410 acpi_no_auto_serialize_setup "ACPI: auto-serialization disabled\n"
  97. It helps matching messages from the real log with printk index.
  98. Then the source file name, line number, and function name can
  99. be used to match the string with the source code.