cpus.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <[email protected]>
  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. tid = gdb.selected_thread().ptid[2]
  22. if tid > (0x100000000 - MAX_CPUS - 2):
  23. return 0x100000000 - tid - 2
  24. else:
  25. return tasks.get_thread_info(tasks.get_task_by_pid(tid))['cpu']
  26. else:
  27. raise gdb.GdbError("Sorry, obtaining the current CPU is not yet "
  28. "supported with this gdb server.")
  29. def per_cpu(var_ptr, cpu):
  30. if cpu == -1:
  31. cpu = get_current_cpu()
  32. if utils.is_target_arch("sparc:v9"):
  33. offset = gdb.parse_and_eval(
  34. "trap_block[{0}].__per_cpu_base".format(str(cpu)))
  35. else:
  36. try:
  37. offset = gdb.parse_and_eval(
  38. "__per_cpu_offset[{0}]".format(str(cpu)))
  39. except gdb.error:
  40. # !CONFIG_SMP case
  41. offset = 0
  42. pointer = var_ptr.cast(utils.get_long_type()) + offset
  43. return pointer.cast(var_ptr.type).dereference()
  44. cpu_mask = {}
  45. def cpu_mask_invalidate(event):
  46. global cpu_mask
  47. cpu_mask = {}
  48. gdb.events.stop.disconnect(cpu_mask_invalidate)
  49. if hasattr(gdb.events, 'new_objfile'):
  50. gdb.events.new_objfile.disconnect(cpu_mask_invalidate)
  51. def cpu_list(mask_name):
  52. global cpu_mask
  53. mask = None
  54. if mask_name in cpu_mask:
  55. mask = cpu_mask[mask_name]
  56. if mask is None:
  57. mask = gdb.parse_and_eval(mask_name + ".bits")
  58. if hasattr(gdb, 'events'):
  59. cpu_mask[mask_name] = 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. bits_per_entry = mask[0].type.sizeof * 8
  64. num_entries = mask.type.sizeof * 8 / bits_per_entry
  65. entry = -1
  66. bits = 0
  67. while True:
  68. while bits == 0:
  69. entry += 1
  70. if entry == num_entries:
  71. return
  72. bits = mask[entry]
  73. if bits != 0:
  74. bit = 0
  75. break
  76. while bits & 1 == 0:
  77. bits >>= 1
  78. bit += 1
  79. cpu = entry * bits_per_entry + bit
  80. bits >>= 1
  81. bit += 1
  82. yield int(cpu)
  83. def each_online_cpu():
  84. for cpu in cpu_list("__cpu_online_mask"):
  85. yield cpu
  86. def each_present_cpu():
  87. for cpu in cpu_list("__cpu_present_mask"):
  88. yield cpu
  89. def each_possible_cpu():
  90. for cpu in cpu_list("__cpu_possible_mask"):
  91. yield cpu
  92. def each_active_cpu():
  93. for cpu in cpu_list("__cpu_active_mask"):
  94. yield cpu
  95. class LxCpus(gdb.Command):
  96. """List CPU status arrays
  97. Displays the known state of each CPU based on the kernel masks
  98. and can help identify the state of hotplugged CPUs"""
  99. def __init__(self):
  100. super(LxCpus, self).__init__("lx-cpus", gdb.COMMAND_DATA)
  101. def invoke(self, arg, from_tty):
  102. gdb.write("Possible CPUs : {}\n".format(list(each_possible_cpu())))
  103. gdb.write("Present CPUs : {}\n".format(list(each_present_cpu())))
  104. gdb.write("Online CPUs : {}\n".format(list(each_online_cpu())))
  105. gdb.write("Active CPUs : {}\n".format(list(each_active_cpu())))
  106. LxCpus()
  107. class PerCpu(gdb.Function):
  108. """Return per-cpu variable.
  109. $lx_per_cpu("VAR"[, CPU]): Return the per-cpu variable called VAR for the
  110. given CPU number. If CPU is omitted, the CPU of the current context is used.
  111. Note that VAR has to be quoted as string."""
  112. def __init__(self):
  113. super(PerCpu, self).__init__("lx_per_cpu")
  114. def invoke(self, var_name, cpu=-1):
  115. var_ptr = gdb.parse_and_eval("&" + var_name.string())
  116. return per_cpu(var_ptr, cpu)
  117. PerCpu()
  118. def get_current_task(cpu):
  119. task_ptr_type = task_type.get_type().pointer()
  120. if utils.is_target_arch("x86"):
  121. var_ptr = gdb.parse_and_eval("&current_task")
  122. return per_cpu(var_ptr, cpu).dereference()
  123. elif utils.is_target_arch("aarch64"):
  124. current_task_addr = gdb.parse_and_eval("$SP_EL0")
  125. if((current_task_addr >> 63) != 0):
  126. current_task = current_task_addr.cast(task_ptr_type)
  127. return current_task.dereference()
  128. else:
  129. raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
  130. "while running in userspace(EL0)")
  131. else:
  132. raise gdb.GdbError("Sorry, obtaining the current task is not yet "
  133. "supported with this arch")
  134. class LxCurrentFunc(gdb.Function):
  135. """Return current task.
  136. $lx_current([CPU]): Return the per-cpu task variable for the given CPU
  137. number. If CPU is omitted, the CPU of the current context is used."""
  138. def __init__(self):
  139. super(LxCurrentFunc, self).__init__("lx_current")
  140. def invoke(self, cpu=-1):
  141. return get_current_task(cpu)
  142. LxCurrentFunc()