cpus.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #
  2. # gdb helper commands and functions for Linux kernel debugging
  3. #
  4. # per-cpu tools
  5. #
  6. # Copyright (c) Siemens AG, 2011-2013
  7. #
  8. # Authors:
  9. # Jan Kiszka <jan.kiszka@siemens.com>
  10. #
  11. # This work is licensed under the terms of the GNU GPL version 2.
  12. #
  13. import gdb
  14. from linux import tasks, utils
  15. MAX_CPUS = 4096
  16. def get_current_cpu():
  17. if utils.get_gdbserver_type() == utils.GDBSERVER_QEMU:
  18. return gdb.selected_thread().num - 1
  19. elif utils.get_gdbserver_type() == utils.GDBSERVER_KGDB:
  20. tid = gdb.selected_thread().ptid[2]
  21. if tid > (0x100000000 - MAX_CPUS - 2):
  22. return 0x100000000 - tid - 2
  23. else:
  24. return tasks.get_thread_info(tasks.get_task_by_pid(tid))['cpu']
  25. else:
  26. raise gdb.GdbError("Sorry, obtaining the current CPU is not yet "
  27. "supported with this gdb server.")
  28. def per_cpu(var_ptr, cpu):
  29. if cpu == -1:
  30. cpu = get_current_cpu()
  31. if utils.is_target_arch("sparc:v9"):
  32. offset = gdb.parse_and_eval(
  33. "trap_block[{0}].__per_cpu_base".format(str(cpu)))
  34. else:
  35. try:
  36. offset = gdb.parse_and_eval(
  37. "__per_cpu_offset[{0}]".format(str(cpu)))
  38. except gdb.error:
  39. # !CONFIG_SMP case
  40. offset = 0
  41. pointer = var_ptr.cast(utils.get_long_type()) + offset
  42. return pointer.cast(var_ptr.type).dereference()
  43. cpu_mask = {}
  44. def cpu_mask_invalidate(event):
  45. global cpu_mask
  46. cpu_mask = {}
  47. gdb.events.stop.disconnect(cpu_mask_invalidate)
  48. if hasattr(gdb.events, 'new_objfile'):
  49. gdb.events.new_objfile.disconnect(cpu_mask_invalidate)
  50. class CpuList():
  51. def __init__(self, mask_name):
  52. global cpu_mask
  53. self.mask = None
  54. if mask_name in cpu_mask:
  55. self.mask = cpu_mask[mask_name]
  56. if self.mask is None:
  57. self.mask = gdb.parse_and_eval(mask_name + ".bits")
  58. if hasattr(gdb, 'events'):
  59. cpu_mask[mask_name] = self.mask
  60. gdb.events.stop.connect(cpu_mask_invalidate)
  61. if hasattr(gdb.events, 'new_objfile'):
  62. gdb.events.new_objfile.connect(cpu_mask_invalidate)
  63. self.bits_per_entry = self.mask[0].type.sizeof * 8
  64. self.num_entries = self.mask.type.sizeof * 8 / self.bits_per_entry
  65. self.entry = -1
  66. self.bits = 0
  67. def __iter__(self):
  68. return self
  69. def __next__(self):
  70. while self.bits == 0:
  71. self.entry += 1
  72. if self.entry == self.num_entries:
  73. raise StopIteration
  74. self.bits = self.mask[self.entry]
  75. if self.bits != 0:
  76. self.bit = 0
  77. break
  78. while self.bits & 1 == 0:
  79. self.bits >>= 1
  80. self.bit += 1
  81. cpu = self.entry * self.bits_per_entry + self.bit
  82. self.bits >>= 1
  83. self.bit += 1
  84. return cpu
  85. def next(self):
  86. return self.__next__()
  87. class PerCpu(gdb.Function):
  88. """Return per-cpu variable.
  89. $lx_per_cpu("VAR"[, CPU]): Return the per-cpu variable called VAR for the
  90. given CPU number. If CPU is omitted, the CPU of the current context is used.
  91. Note that VAR has to be quoted as string."""
  92. def __init__(self):
  93. super(PerCpu, self).__init__("lx_per_cpu")
  94. def invoke(self, var_name, cpu=-1):
  95. var_ptr = gdb.parse_and_eval("&" + var_name.string())
  96. return per_cpu(var_ptr, cpu)
  97. PerCpu()
  98. class LxCurrentFunc(gdb.Function):
  99. """Return current task.
  100. $lx_current([CPU]): Return the per-cpu task variable for the given CPU
  101. number. If CPU is omitted, the CPU of the current context is used."""
  102. def __init__(self):
  103. super(LxCurrentFunc, self).__init__("lx_current")
  104. def invoke(self, cpu=-1):
  105. var_ptr = gdb.parse_and_eval("&current_task")
  106. return per_cpu(var_ptr, cpu).dereference()
  107. LxCurrentFunc()