interrupts.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # SPDX-License-Identifier: GPL-2.0
  2. #
  3. # Copyright 2023 Broadcom
  4. import gdb
  5. from linux import constants
  6. from linux import cpus
  7. from linux import utils
  8. from linux import mapletree
  9. irq_desc_type = utils.CachedType("struct irq_desc")
  10. def irq_settings_is_hidden(desc):
  11. return desc['status_use_accessors'] & constants.LX_IRQ_HIDDEN
  12. def irq_desc_is_chained(desc):
  13. return desc['action'] and desc['action'] == gdb.parse_and_eval("&chained_action")
  14. def irqd_is_level(desc):
  15. return desc['irq_data']['common']['state_use_accessors'] & constants.LX_IRQD_LEVEL
  16. def show_irq_desc(prec, irq):
  17. text = ""
  18. desc = mapletree.mtree_load(gdb.parse_and_eval("&sparse_irqs"), irq)
  19. if desc is None:
  20. return text
  21. desc = desc.cast(irq_desc_type.get_type().pointer())
  22. if desc == 0:
  23. return text
  24. if irq_settings_is_hidden(desc):
  25. return text
  26. any_count = 0
  27. if desc['kstat_irqs']:
  28. for cpu in cpus.each_online_cpu():
  29. any_count += cpus.per_cpu(desc['kstat_irqs'], cpu)['cnt']
  30. if (desc['action'] == 0 or irq_desc_is_chained(desc)) and any_count == 0:
  31. return text;
  32. text += "%*d: " % (prec, irq)
  33. for cpu in cpus.each_online_cpu():
  34. if desc['kstat_irqs']:
  35. count = cpus.per_cpu(desc['kstat_irqs'], cpu)['cnt']
  36. else:
  37. count = 0
  38. text += "%10u" % (count)
  39. name = "None"
  40. if desc['irq_data']['chip']:
  41. chip = desc['irq_data']['chip']
  42. if chip['name']:
  43. name = chip['name'].string()
  44. else:
  45. name = "-"
  46. text += " %8s" % (name)
  47. if desc['irq_data']['domain']:
  48. text += " %*lu" % (prec, desc['irq_data']['hwirq'])
  49. else:
  50. text += " %*s" % (prec, "")
  51. if constants.LX_CONFIG_GENERIC_IRQ_SHOW_LEVEL:
  52. text += " %-8s" % ("Level" if irqd_is_level(desc) else "Edge")
  53. if desc['name']:
  54. text += "-%-8s" % (desc['name'].string())
  55. """ Some toolchains may not be able to provide information about irqaction """
  56. try:
  57. gdb.lookup_type("struct irqaction")
  58. action = desc['action']
  59. if action is not None:
  60. text += " %s" % (action['name'].string())
  61. while True:
  62. action = action['next']
  63. if action is not None:
  64. break
  65. if action['name']:
  66. text += ", %s" % (action['name'].string())
  67. except:
  68. pass
  69. text += "\n"
  70. return text
  71. def show_irq_err_count(prec):
  72. cnt = utils.gdb_eval_or_none("irq_err_count")
  73. text = ""
  74. if cnt is not None:
  75. text += "%*s: %10u\n" % (prec, "ERR", cnt['counter'])
  76. return text
  77. def x86_show_irqstat(prec, pfx, field, desc):
  78. irq_stat = gdb.parse_and_eval("&irq_stat")
  79. text = "%*s: " % (prec, pfx)
  80. for cpu in cpus.each_online_cpu():
  81. stat = cpus.per_cpu(irq_stat, cpu)
  82. text += "%10u " % (stat[field])
  83. text += " %s\n" % (desc)
  84. return text
  85. def x86_show_mce(prec, var, pfx, desc):
  86. pvar = gdb.parse_and_eval(var)
  87. text = "%*s: " % (prec, pfx)
  88. for cpu in cpus.each_online_cpu():
  89. text += "%10u " % (cpus.per_cpu(pvar, cpu).dereference())
  90. text += " %s\n" % (desc)
  91. return text
  92. def x86_show_interupts(prec):
  93. text = x86_show_irqstat(prec, "NMI", '__nmi_count', 'Non-maskable interrupts')
  94. if constants.LX_CONFIG_X86_LOCAL_APIC:
  95. text += x86_show_irqstat(prec, "LOC", 'apic_timer_irqs', "Local timer interrupts")
  96. text += x86_show_irqstat(prec, "SPU", 'irq_spurious_count', "Spurious interrupts")
  97. text += x86_show_irqstat(prec, "PMI", 'apic_perf_irqs', "Performance monitoring interrupts")
  98. text += x86_show_irqstat(prec, "IWI", 'apic_irq_work_irqs', "IRQ work interrupts")
  99. text += x86_show_irqstat(prec, "RTR", 'icr_read_retry_count', "APIC ICR read retries")
  100. if utils.gdb_eval_or_none("x86_platform_ipi_callback") is not None:
  101. text += x86_show_irqstat(prec, "PLT", 'x86_platform_ipis', "Platform interrupts")
  102. if constants.LX_CONFIG_SMP:
  103. text += x86_show_irqstat(prec, "RES", 'irq_resched_count', "Rescheduling interrupts")
  104. text += x86_show_irqstat(prec, "CAL", 'irq_call_count', "Function call interrupts")
  105. text += x86_show_irqstat(prec, "TLB", 'irq_tlb_count', "TLB shootdowns")
  106. if constants.LX_CONFIG_X86_THERMAL_VECTOR:
  107. text += x86_show_irqstat(prec, "TRM", 'irq_thermal_count', "Thermal events interrupts")
  108. if constants.LX_CONFIG_X86_MCE_THRESHOLD:
  109. text += x86_show_irqstat(prec, "THR", 'irq_threshold_count', "Threshold APIC interrupts")
  110. if constants.LX_CONFIG_X86_MCE_AMD:
  111. text += x86_show_irqstat(prec, "DFR", 'irq_deferred_error_count', "Deferred Error APIC interrupts")
  112. if constants.LX_CONFIG_X86_MCE:
  113. text += x86_show_mce(prec, "&mce_exception_count", "MCE", "Machine check exceptions")
  114. text += x86_show_mce(prec, "&mce_poll_count", "MCP", "Machine check polls")
  115. text += show_irq_err_count(prec)
  116. if constants.LX_CONFIG_X86_IO_APIC:
  117. cnt = utils.gdb_eval_or_none("irq_mis_count")
  118. if cnt is not None:
  119. text += "%*s: %10u\n" % (prec, "MIS", cnt['counter'])
  120. if constants.LX_CONFIG_KVM:
  121. text += x86_show_irqstat(prec, "PIN", 'kvm_posted_intr_ipis', 'Posted-interrupt notification event')
  122. text += x86_show_irqstat(prec, "NPI", 'kvm_posted_intr_nested_ipis', 'Nested posted-interrupt event')
  123. text += x86_show_irqstat(prec, "PIW", 'kvm_posted_intr_wakeup_ipis', 'Posted-interrupt wakeup event')
  124. return text
  125. def arm_common_show_interrupts(prec):
  126. text = ""
  127. nr_ipi = utils.gdb_eval_or_none("nr_ipi")
  128. ipi_desc = utils.gdb_eval_or_none("ipi_desc")
  129. ipi_types = utils.gdb_eval_or_none("ipi_types")
  130. if nr_ipi is None or ipi_desc is None or ipi_types is None:
  131. return text
  132. if prec >= 4:
  133. sep = " "
  134. else:
  135. sep = ""
  136. for ipi in range(nr_ipi):
  137. text += "%*s%u:%s" % (prec - 1, "IPI", ipi, sep)
  138. desc = ipi_desc[ipi].cast(irq_desc_type.get_type().pointer())
  139. if desc == 0:
  140. continue
  141. for cpu in cpus.each_online_cpu():
  142. text += "%10u" % (cpus.per_cpu(desc['kstat_irqs'], cpu)['cnt'])
  143. text += " %s" % (ipi_types[ipi].string())
  144. text += "\n"
  145. return text
  146. def aarch64_show_interrupts(prec):
  147. text = arm_common_show_interrupts(prec)
  148. text += "%*s: %10lu\n" % (prec, "ERR", gdb.parse_and_eval("irq_err_count"))
  149. return text
  150. def arch_show_interrupts(prec):
  151. text = ""
  152. if utils.is_target_arch("x86"):
  153. text += x86_show_interupts(prec)
  154. elif utils.is_target_arch("aarch64"):
  155. text += aarch64_show_interrupts(prec)
  156. elif utils.is_target_arch("arm"):
  157. text += arm_common_show_interrupts(prec)
  158. elif utils.is_target_arch("mips"):
  159. text += show_irq_err_count(prec)
  160. else:
  161. raise gdb.GdbError("Unsupported architecture: {}".format(target_arch))
  162. return text
  163. class LxInterruptList(gdb.Command):
  164. """Print /proc/interrupts"""
  165. def __init__(self):
  166. super(LxInterruptList, self).__init__("lx-interruptlist", gdb.COMMAND_DATA)
  167. def invoke(self, arg, from_tty):
  168. nr_irqs = gdb.parse_and_eval("nr_irqs")
  169. prec = 3
  170. j = 1000
  171. while prec < 10 and j <= nr_irqs:
  172. prec += 1
  173. j *= 10
  174. gdb.write("%*s" % (prec + 8, ""))
  175. for cpu in cpus.each_online_cpu():
  176. gdb.write("CPU%-8d" % cpu)
  177. gdb.write("\n")
  178. if utils.gdb_eval_or_none("&sparse_irqs") is None:
  179. raise gdb.GdbError("Unable to find the sparse IRQ tree, is CONFIG_SPARSE_IRQ enabled?")
  180. for irq in range(nr_irqs):
  181. gdb.write(show_irq_desc(prec, irq))
  182. gdb.write(arch_show_interrupts(prec))
  183. LxInterruptList()