README.pretty-printers 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. README for the glibc Python pretty printers
  2. ===========================================
  3. Pretty printers are gdb extensions that allow it to print useful, human-readable
  4. information about a program's variables. For example, for a pthread_mutex_t
  5. gdb would usually output something like this:
  6. (gdb) print mutex
  7. $1 = {
  8. __data = {
  9. __lock = 22020096,
  10. __count = 0,
  11. __owner = 0,
  12. __nusers = 0,
  13. __kind = 576,
  14. __spins = 0,
  15. __elision = 0,
  16. __list = {
  17. __prev = 0x0,
  18. __next = 0x0
  19. }
  20. },
  21. __size = "\000\000P\001", '\000' <repeats 12 times>, "@\002", '\000' <repeats 21 times>,
  22. __align = 22020096
  23. }
  24. However, with a pretty printer gdb will output something like this:
  25. (gdb) print mutex
  26. $1 = pthread_mutex_t = {
  27. Type = Normal,
  28. Status = Not acquired,
  29. Robust = No,
  30. Shared = No,
  31. Protocol = Priority protect,
  32. Priority ceiling = 42
  33. }
  34. Before printing a value, gdb will first check if there's a pretty printer
  35. registered for it. If there is, it'll use it, otherwise it'll print the value
  36. as usual. Pretty printers can be registered in various ways; for our purposes
  37. we register them for the current objfile by calling
  38. gdb.printing.register_pretty_printer().
  39. Currently our printers are based on gdb.RegexpCollectionPrettyPrinter, which
  40. means they'll be triggered if the type of the variable we're printing matches
  41. a given regular expression. For example, MutexPrinter will be triggered if
  42. our variable's type matches the regexp '^pthread_mutex_t$'.
  43. Besides the printers themselves, each module may have a constants file which the
  44. printers will import. These constants are generated from C headers during the
  45. build process, and need to be in the Python search path when loading the
  46. printers.
  47. Installing and loading
  48. ----------------------
  49. The pretty printers and their constant files may be installed in different paths
  50. for each distro, though gdb should be able to automatically load them by itself.
  51. When in doubt, you can use the 'info pretty-printer' gdb command to list the
  52. loaded pretty printers.
  53. If the printers aren't automatically loaded for some reason, you should add the
  54. following to your .gdbinit:
  55. python
  56. import sys
  57. sys.path.insert(0, '/path/to/constants/file/directory')
  58. end
  59. source /path/to/printers.py
  60. If you're building glibc manually, '/path/to/constants/file/directory' should be
  61. '/path/to/glibc-build/submodule', where 'submodule' is e.g. nptl.
  62. Testing
  63. -------
  64. The pretty printers come with a small test suite based on PExpect, which is a
  65. Python module with Expect-like features for spawning and controlling interactive
  66. programs. Each printer has a corresponding C program and a Python script
  67. that uses PExpect to drive gdb through the program and compare its output to
  68. the expected printer's.
  69. The tests run on the glibc host, which is assumed to have both gdb and PExpect;
  70. if any of those is absent the tests will fail with code 77 (UNSUPPORTED).
  71. Native builds can be tested simply by doing 'make check'; cross builds must use
  72. cross-test-ssh.sh as test-wrapper, like this:
  73. make test-wrapper='/path/to/scripts/cross-test-ssh.sh user@host' check
  74. (Remember to share the build system's filesystem with the glibc host's through
  75. NFS or something similar).
  76. Running 'make check' on a cross build will only compile the test programs,
  77. without running the scripts.
  78. Adding new pretty printers
  79. --------------------------
  80. Adding new pretty printers to glibc requires following these steps:
  81. 1. Identify which constants must be generated from C headers, and write the
  82. corresponding .pysym file. See scripts/gen-as-const.py for more information
  83. on how this works. The name of the .pysym file must be added to the
  84. 'gen-py-const-headers' variable in your submodule's Makefile (without the .pysym
  85. extension).
  86. 2. Write the pretty printer code itself. For this you can follow the gdb
  87. Python API documentation, and use the existing printers as examples. The printer
  88. code must import the generated constants file (which will have the same name
  89. as your .pysym file). The names of the pretty printer files must be added
  90. to the 'pretty-printers' variable in your submodule's Makefile (without the .py
  91. extension).
  92. 3. Write the unit tests for your pretty printers. The build system calls each
  93. test script passing it the paths to the test program source, the test program
  94. binary, and the printer files you added to 'pretty-printers' in the previous
  95. step. The test scripts, in turn, must import scripts/test_printers_common
  96. and call the init_test function passing it, among other things, the name of the
  97. set of pretty printers to enable (as seen by running 'info pretty-printer').
  98. You can use the existing unit tests as examples.
  99. 4. Add the names of the pretty printer tests to the 'tests-printers' variable
  100. in your submodule's Makefile (without extensions). In addition, for each test
  101. program you must define a corresponding CFLAGS-* and CPPFLAGS-* variable and
  102. set it to $(CFLAGS-printers-tests) to ensure they're compiled correctly. For
  103. example, test-foo-printer.c requires the following:
  104. CFLAGS-test-foo-printer.c := $(CFLAGS-printers-tests)
  105. CPPFLAGS-test-foo-printer.c := $(CFLAGS-printers-tests)
  106. Finally, if your programs need to be linked with a specific library, you can add
  107. its name to the 'tests-printers-libs' variable in your submodule's Makefile.
  108. Known issues
  109. ------------
  110. * Pretty printers are inherently coupled to the code they're targetting, thus
  111. any changes to the target code must also update the corresponding printers.
  112. On the plus side, the printer code itself may serve as a kind of documentation
  113. for the target code.
  114. * There's no guarantee that the information the pretty printers provide is
  115. complete, i.e. some details might be left off. For example, the pthread_mutex_t
  116. printers won't report whether a thread is spin-waiting in an attempt to acquire
  117. the mutex.
  118. * Older versions of the gdb Python API have a bug where
  119. gdb.RegexpCollectionPrettyPrinter would not be able to get a value's real type
  120. if it was typedef'd. This would cause gdb to ignore the pretty printers for
  121. types like pthread_mutex_t, which is defined as:
  122. typedef union
  123. {
  124. ...
  125. } pthread_mutex_t;
  126. This was fixed in commit 1b588015839caafc608a6944a78aea170f5fb2f6, and released
  127. as part of gdb 7.8. However, typedef'ing an already typedef'd type may cause
  128. a similar issue, e.g.:
  129. typedef pthread_mutex_t mutex;
  130. mutex a_mutex;
  131. Here, trying to print a_mutex won't trigger the pthread_mutex_t printer.
  132. * The test programs must be compiled without optimizations. This is necessary
  133. because the test scripts rely on the C code structure being preserved when
  134. stepping through the programs. Things like aggressive instruction reordering
  135. or optimizing variables out may make this kind of testing impossible.