log_stap.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. """
  3. Generate .stp file that printfs log messages (DTrace with SystemTAP only).
  4. """
  5. __author__ = "Daniel P. Berrange <berrange@redhat.com>"
  6. __copyright__ = "Copyright (C) 2014-2019, Red Hat, Inc."
  7. __license__ = "GPL version 2 or (at your option) any later version"
  8. __maintainer__ = "Daniel Berrange"
  9. __email__ = "berrange@redhat.com"
  10. import re
  11. from tracetool import out
  12. from tracetool.backend.dtrace import binary, probeprefix
  13. from tracetool.backend.simple import is_string
  14. from tracetool.format.stap import stap_escape
  15. def global_var_name(name):
  16. return probeprefix().replace(".", "_") + "_" + name
  17. STATE_SKIP = 0
  18. STATE_LITERAL = 1
  19. STATE_MACRO = 2
  20. def c_macro_to_format(macro):
  21. if macro.startswith("PRI"):
  22. return macro[3]
  23. raise Exception("Unhandled macro '%s'" % macro)
  24. def c_fmt_to_stap(fmt):
  25. state = 0
  26. bits = []
  27. literal = ""
  28. macro = ""
  29. escape = 0;
  30. for i in range(len(fmt)):
  31. if fmt[i] == '\\':
  32. if escape:
  33. escape = 0
  34. else:
  35. escape = 1
  36. if state != STATE_LITERAL:
  37. raise Exception("Unexpected escape outside string literal")
  38. literal = literal + fmt[i]
  39. elif fmt[i] == '"' and not escape:
  40. if state == STATE_LITERAL:
  41. state = STATE_SKIP
  42. bits.append(literal)
  43. literal = ""
  44. else:
  45. if state == STATE_MACRO:
  46. bits.append(c_macro_to_format(macro))
  47. macro = ""
  48. state = STATE_LITERAL
  49. elif fmt[i] == ' ' or fmt[i] == '\t':
  50. if state == STATE_MACRO:
  51. bits.append(c_macro_to_format(macro))
  52. macro = ""
  53. state = STATE_SKIP
  54. elif state == STATE_LITERAL:
  55. literal = literal + fmt[i]
  56. else:
  57. escape = 0
  58. if state == STATE_SKIP:
  59. state = STATE_MACRO
  60. if state == STATE_LITERAL:
  61. literal = literal + fmt[i]
  62. else:
  63. macro = macro + fmt[i]
  64. if state == STATE_MACRO:
  65. bits.append(c_macro_to_format(macro))
  66. elif state == STATE_LITERAL:
  67. bits.append(literal)
  68. # All variables in systemtap are 64-bit in size
  69. # The "%l" integer size qualifier is thus redundant
  70. # and "%ll" is not valid at all. Similarly the size_t
  71. # based "%z" size qualifier is not valid. We just
  72. # strip all size qualifiers for sanity.
  73. fmt = re.sub(r"%(\d*)(l+|z)(x|u|d)", r"%\1\3", "".join(bits))
  74. return fmt
  75. def generate(events, backend, group):
  76. out('/* This file is autogenerated by tracetool, do not edit. */',
  77. '')
  78. for event_id, e in enumerate(events):
  79. if 'disable' in e.properties:
  80. continue
  81. out('probe %(probeprefix)s.log.%(name)s = %(probeprefix)s.%(name)s ?',
  82. '{',
  83. probeprefix=probeprefix(),
  84. name=e.name)
  85. # Get references to userspace strings
  86. for type_, name in e.args:
  87. name = stap_escape(name)
  88. if is_string(type_):
  89. out(' try {',
  90. ' arg%(name)s_str = %(name)s ? ' +
  91. 'user_string_n(%(name)s, 512) : "<null>"',
  92. ' } catch {}',
  93. name=name)
  94. # Determine systemtap's view of variable names
  95. fields = ["pid()", "gettimeofday_ns()"]
  96. for type_, name in e.args:
  97. name = stap_escape(name)
  98. if is_string(type_):
  99. fields.append("arg" + name + "_str")
  100. else:
  101. fields.append(name)
  102. # Emit the entire record in a single SystemTap printf()
  103. arg_str = ', '.join(arg for arg in fields)
  104. fmt_str = "%d@%d " + e.name + " " + c_fmt_to_stap(e.fmt) + "\\n"
  105. out(' printf("%(fmt_str)s", %(arg_str)s)',
  106. fmt_str=fmt_str, arg_str=arg_str)
  107. out('}')
  108. out()