cpus.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. class PerCpu(gdb.Function):
  44. """Return per-cpu variable.
  45. $lx_per_cpu("VAR"[, CPU]): Return the per-cpu variable called VAR for the
  46. given CPU number. If CPU is omitted, the CPU of the current context is used.
  47. Note that VAR has to be quoted as string."""
  48. def __init__(self):
  49. super(PerCpu, self).__init__("lx_per_cpu")
  50. def invoke(self, var_name, cpu=-1):
  51. var_ptr = gdb.parse_and_eval("&" + var_name.string())
  52. return per_cpu(var_ptr, cpu)
  53. PerCpu()