cpus.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. task_type = utils.CachedType("struct task_struct")
  16. MAX_CPUS = 4096
  17. def get_current_cpu():
  18. if utils.get_gdbserver_type() == utils.GDBSERVER_QEMU:
  19. return gdb.selected_thread().num - 1
  20. elif utils.get_gdbserver_type() == utils.GDBSERVER_KGDB:
  21. return gdb.parse_and_eval("kgdb_active.counter")
  22. else:
  23. raise gdb.GdbError("Sorry, obtaining the current CPU is not yet "
  24. "supported with this gdb server.")
  25. def per_cpu(var_ptr, cpu):
  26. if cpu == -1:
  27. cpu = get_current_cpu()
  28. if utils.is_target_arch("sparc:v9"):
  29. offset = gdb.parse_and_eval(
  30. "trap_block[{0}].__per_cpu_base".format(str(cpu)))
  31. else:
  32. try:
  33. offset = gdb.parse_and_eval(
  34. "__per_cpu_offset[{0}]".format(str(cpu)))
  35. except gdb.error:
  36. # !CONFIG_SMP case
  37. offset = 0
  38. pointer = var_ptr.cast(utils.get_long_type()) + offset
  39. return pointer.cast(var_ptr.type)
  40. cpu_mask = {}
  41. def cpu_mask_invalidate(event):
  42. global cpu_mask
  43. cpu_mask = {}
  44. gdb.events.stop.disconnect(cpu_mask_invalidate)
  45. if hasattr(gdb.events, 'new_objfile'):
  46. gdb.events.new_objfile.disconnect(cpu_mask_invalidate)
  47. def cpu_list(mask_name):
  48. global cpu_mask
  49. mask = None
  50. if mask_name in cpu_mask:
  51. mask = cpu_mask[mask_name]
  52. if mask is None:
  53. mask = gdb.parse_and_eval(mask_name + ".bits")
  54. if hasattr(gdb, 'events'):
  55. cpu_mask[mask_name] = mask
  56. gdb.events.stop.connect(cpu_mask_invalidate)
  57. if hasattr(gdb.events, 'new_objfile'):
  58. gdb.events.new_objfile.connect(cpu_mask_invalidate)
  59. bits_per_entry = mask[0].type.sizeof * 8
  60. num_entries = mask.type.sizeof * 8 / bits_per_entry
  61. entry = -1
  62. bits = 0
  63. while True:
  64. while bits == 0:
  65. entry += 1
  66. if entry == num_entries:
  67. return
  68. bits = mask[entry]
  69. if bits != 0:
  70. bit = 0
  71. break
  72. while bits & 1 == 0:
  73. bits >>= 1
  74. bit += 1
  75. cpu = entry * bits_per_entry + bit
  76. bits >>= 1
  77. bit += 1
  78. yield int(cpu)
  79. def each_online_cpu():
  80. for cpu in cpu_list("__cpu_online_mask"):
  81. yield cpu
  82. def each_present_cpu():
  83. for cpu in cpu_list("__cpu_present_mask"):
  84. yield cpu
  85. def each_possible_cpu():
  86. for cpu in cpu_list("__cpu_possible_mask"):
  87. yield cpu
  88. def each_active_cpu():
  89. for cpu in cpu_list("__cpu_active_mask"):
  90. yield cpu
  91. class LxCpus(gdb.Command):
  92. """List CPU status arrays
  93. Displays the known state of each CPU based on the kernel masks
  94. and can help identify the state of hotplugged CPUs"""
  95. def __init__(self):
  96. super(LxCpus, self).__init__("lx-cpus", gdb.COMMAND_DATA)
  97. def invoke(self, arg, from_tty):
  98. gdb.write("Possible CPUs : {}\n".format(list(each_possible_cpu())))
  99. gdb.write("Present CPUs : {}\n".format(list(each_present_cpu())))
  100. gdb.write("Online CPUs : {}\n".format(list(each_online_cpu())))
  101. gdb.write("Active CPUs : {}\n".format(list(each_active_cpu())))
  102. LxCpus()
  103. class PerCpu(gdb.Function):
  104. """Return per-cpu variable.
  105. $lx_per_cpu(VAR[, CPU]): Return the per-cpu variable called VAR for the
  106. given CPU number. If CPU is omitted, the CPU of the current context is used.
  107. Note that VAR has to be quoted as string."""
  108. def __init__(self):
  109. super(PerCpu, self).__init__("lx_per_cpu")
  110. def invoke(self, var, cpu=-1):
  111. return per_cpu(var.address, cpu).dereference()
  112. PerCpu()
  113. class PerCpuPtr(gdb.Function):
  114. """Return per-cpu pointer.
  115. $lx_per_cpu_ptr(VAR[, CPU]): Return the per-cpu pointer called VAR for the
  116. given CPU number. If CPU is omitted, the CPU of the current context is used.
  117. Note that VAR has to be quoted as string."""
  118. def __init__(self):
  119. super(PerCpuPtr, self).__init__("lx_per_cpu_ptr")
  120. def invoke(self, var, cpu=-1):
  121. return per_cpu(var, cpu)
  122. PerCpuPtr()
  123. def get_current_task(cpu):
  124. task_ptr_type = task_type.get_type().pointer()
  125. if utils.is_target_arch("x86"):
  126. if gdb.lookup_global_symbol("cpu_tasks"):
  127. # This is a UML kernel, which stores the current task
  128. # differently than other x86 sub architectures
  129. var_ptr = gdb.parse_and_eval("(struct task_struct *)cpu_tasks[0].task")
  130. return var_ptr.dereference()
  131. else:
  132. var_ptr = gdb.parse_and_eval("&current_task")
  133. return per_cpu(var_ptr, cpu).dereference()
  134. elif utils.is_target_arch("aarch64"):
  135. current_task_addr = gdb.parse_and_eval("(unsigned long)$SP_EL0")
  136. if (current_task_addr >> 63) != 0:
  137. current_task = current_task_addr.cast(task_ptr_type)
  138. return current_task.dereference()
  139. else:
  140. raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
  141. "while running in userspace(EL0)")
  142. elif utils.is_target_arch("riscv"):
  143. current_tp = gdb.parse_and_eval("$tp")
  144. scratch_reg = gdb.parse_and_eval("$sscratch")
  145. # by default tp points to current task
  146. current_task = current_tp.cast(task_ptr_type)
  147. # scratch register is set 0 in trap handler after entering kernel.
  148. # When hart is in user mode, scratch register is pointing to task_struct.
  149. # and tp is used by user mode. So when scratch register holds larger value
  150. # (negative address as ulong is larger value) than tp, then use scratch register.
  151. if (scratch_reg.cast(utils.get_ulong_type()) > current_tp.cast(utils.get_ulong_type())):
  152. current_task = scratch_reg.cast(task_ptr_type)
  153. return current_task.dereference()
  154. else:
  155. raise gdb.GdbError("Sorry, obtaining the current task is not yet "
  156. "supported with this arch")
  157. class LxCurrentFunc(gdb.Function):
  158. """Return current task.
  159. $lx_current([CPU]): Return the per-cpu task variable for the given CPU
  160. number. If CPU is omitted, the CPU of the current context is used."""
  161. def __init__(self):
  162. super(LxCurrentFunc, self).__init__("lx_current")
  163. def invoke(self, cpu=-1):
  164. return get_current_task(cpu)
  165. LxCurrentFunc()