dtrace.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. DTrace/SystemTAP backend.
  5. """
  6. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  7. __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
  8. __license__ = "GPL version 2 or (at your option) any later version"
  9. __maintainer__ = "Stefan Hajnoczi"
  10. __email__ = "stefanha@linux.vnet.ibm.com"
  11. from tracetool import out
  12. PUBLIC = True
  13. PROBEPREFIX = None
  14. def _probeprefix():
  15. if PROBEPREFIX is None:
  16. raise ValueError("you must set PROBEPREFIX")
  17. return PROBEPREFIX
  18. BINARY = None
  19. def _binary():
  20. if BINARY is None:
  21. raise ValueError("you must set BINARY")
  22. return BINARY
  23. def c(events):
  24. pass
  25. def h(events):
  26. out('#include "trace/generated-tracers-dtrace.h"',
  27. '')
  28. for e in events:
  29. out('static inline void %(api)s(%(args)s) {',
  30. ' QEMU_%(uppername)s(%(argnames)s);',
  31. '}',
  32. api = e.api(),
  33. args = e.args,
  34. uppername = e.name.upper(),
  35. argnames = ", ".join(e.args.names()),
  36. )
  37. def d(events):
  38. out('provider qemu {')
  39. for e in events:
  40. args = str(e.args)
  41. # DTrace provider syntax expects foo() for empty
  42. # params, not foo(void)
  43. if args == 'void':
  44. args = ''
  45. # Define prototype for probe arguments
  46. out('',
  47. 'probe %(name)s(%(args)s);',
  48. name = e.name,
  49. args = args,
  50. )
  51. out('',
  52. '};')
  53. # Technically 'self' is not used by systemtap yet, but
  54. # they recommended we keep it in the reserved list anyway
  55. RESERVED_WORDS = (
  56. 'break', 'catch', 'continue', 'delete', 'else', 'for',
  57. 'foreach', 'function', 'global', 'if', 'in', 'limit',
  58. 'long', 'next', 'probe', 'return', 'self', 'string',
  59. 'try', 'while'
  60. )
  61. def stap(events):
  62. for e in events:
  63. # Define prototype for probe arguments
  64. out('probe %(probeprefix)s.%(name)s = process("%(binary)s").mark("%(name)s")',
  65. '{',
  66. probeprefix = _probeprefix(),
  67. name = e.name,
  68. binary = _binary(),
  69. )
  70. i = 1
  71. if len(e.args) > 0:
  72. for name in e.args.names():
  73. # Append underscore to reserved keywords
  74. if name in RESERVED_WORDS:
  75. name += '_'
  76. out(' %s = $arg%d;' % (name, i))
  77. i += 1
  78. out('}')
  79. out()