cpus.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. def cpu_list(mask_name):
  51. global cpu_mask
  52. mask = None
  53. if mask_name in cpu_mask:
  54. mask = cpu_mask[mask_name]
  55. if mask is None:
  56. mask = gdb.parse_and_eval(mask_name + ".bits")
  57. if hasattr(gdb, 'events'):
  58. cpu_mask[mask_name] = mask
  59. gdb.events.stop.connect(cpu_mask_invalidate)
  60. if hasattr(gdb.events, 'new_objfile'):
  61. gdb.events.new_objfile.connect(cpu_mask_invalidate)
  62. bits_per_entry = mask[0].type.sizeof * 8
  63. num_entries = mask.type.sizeof * 8 / bits_per_entry
  64. entry = -1
  65. bits = 0
  66. while True:
  67. while bits == 0:
  68. entry += 1
  69. if entry == num_entries:
  70. return
  71. bits = mask[entry]
  72. if bits != 0:
  73. bit = 0
  74. break
  75. while bits & 1 == 0:
  76. bits >>= 1
  77. bit += 1
  78. cpu = entry * bits_per_entry + bit
  79. bits >>= 1
  80. bit += 1
  81. yield int(cpu)
  82. def each_online_cpu():
  83. for cpu in cpu_list("__cpu_online_mask"):
  84. yield cpu
  85. def each_present_cpu():
  86. for cpu in cpu_list("__cpu_present_mask"):
  87. yield cpu
  88. def each_possible_cpu():
  89. for cpu in cpu_list("__cpu_possible_mask"):
  90. yield cpu
  91. def each_active_cpu():
  92. for cpu in cpu_list("__cpu_active_mask"):
  93. yield cpu
  94. class LxCpus(gdb.Command):
  95. """List CPU status arrays
  96. Displays the known state of each CPU based on the kernel masks
  97. and can help identify the state of hotplugged CPUs"""
  98. def __init__(self):
  99. super(LxCpus, self).__init__("lx-cpus", gdb.COMMAND_DATA)
  100. def invoke(self, arg, from_tty):
  101. gdb.write("Possible CPUs : {}\n".format(list(each_possible_cpu())))
  102. gdb.write("Present CPUs : {}\n".format(list(each_present_cpu())))
  103. gdb.write("Online CPUs : {}\n".format(list(each_online_cpu())))
  104. gdb.write("Active CPUs : {}\n".format(list(each_active_cpu())))
  105. LxCpus()
  106. class PerCpu(gdb.Function):
  107. """Return per-cpu variable.
  108. $lx_per_cpu("VAR"[, CPU]): Return the per-cpu variable called VAR for the
  109. given CPU number. If CPU is omitted, the CPU of the current context is used.
  110. Note that VAR has to be quoted as string."""
  111. def __init__(self):
  112. super(PerCpu, self).__init__("lx_per_cpu")
  113. def invoke(self, var_name, cpu=-1):
  114. var_ptr = gdb.parse_and_eval("&" + var_name.string())
  115. return per_cpu(var_ptr, cpu)
  116. PerCpu()
  117. class LxCurrentFunc(gdb.Function):
  118. """Return current task.
  119. $lx_current([CPU]): Return the per-cpu task variable for the given CPU
  120. number. If CPU is omitted, the CPU of the current context is used."""
  121. def __init__(self):
  122. super(LxCurrentFunc, self).__init__("lx_current")
  123. def invoke(self, cpu=-1):
  124. var_ptr = gdb.parse_and_eval("&current_task")
  125. return per_cpu(var_ptr, cpu).dereference()
  126. LxCurrentFunc()