stacktrace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kernel/stacktrace.c
  4. *
  5. * Stack trace management functions
  6. *
  7. * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  8. */
  9. #include <linux/sched/task_stack.h>
  10. #include <linux/sched/debug.h>
  11. #include <linux/sched.h>
  12. #include <linux/kernel.h>
  13. #include <linux/export.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/stacktrace.h>
  16. #include <linux/interrupt.h>
  17. /**
  18. * stack_trace_print - Print the entries in the stack trace
  19. * @entries: Pointer to storage array
  20. * @nr_entries: Number of entries in the storage array
  21. * @spaces: Number of leading spaces to print
  22. */
  23. void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
  24. int spaces)
  25. {
  26. unsigned int i;
  27. if (WARN_ON(!entries))
  28. return;
  29. for (i = 0; i < nr_entries; i++)
  30. printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
  31. }
  32. EXPORT_SYMBOL_GPL(stack_trace_print);
  33. /**
  34. * stack_trace_snprint - Print the entries in the stack trace into a buffer
  35. * @buf: Pointer to the print buffer
  36. * @size: Size of the print buffer
  37. * @entries: Pointer to storage array
  38. * @nr_entries: Number of entries in the storage array
  39. * @spaces: Number of leading spaces to print
  40. *
  41. * Return: Number of bytes printed.
  42. */
  43. int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
  44. unsigned int nr_entries, int spaces)
  45. {
  46. unsigned int generated, i, total = 0;
  47. if (WARN_ON(!entries))
  48. return 0;
  49. for (i = 0; i < nr_entries && size; i++) {
  50. generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
  51. (void *)entries[i]);
  52. total += generated;
  53. if (generated >= size) {
  54. buf += size;
  55. size = 0;
  56. } else {
  57. buf += generated;
  58. size -= generated;
  59. }
  60. }
  61. return total;
  62. }
  63. EXPORT_SYMBOL_GPL(stack_trace_snprint);
  64. #ifdef CONFIG_ARCH_STACKWALK
  65. struct stacktrace_cookie {
  66. unsigned long *store;
  67. unsigned int size;
  68. unsigned int skip;
  69. unsigned int len;
  70. };
  71. static bool stack_trace_consume_entry(void *cookie, unsigned long addr)
  72. {
  73. struct stacktrace_cookie *c = cookie;
  74. if (c->len >= c->size)
  75. return false;
  76. if (c->skip > 0) {
  77. c->skip--;
  78. return true;
  79. }
  80. c->store[c->len++] = addr;
  81. return c->len < c->size;
  82. }
  83. static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr)
  84. {
  85. if (in_sched_functions(addr))
  86. return true;
  87. return stack_trace_consume_entry(cookie, addr);
  88. }
  89. /**
  90. * stack_trace_save - Save a stack trace into a storage array
  91. * @store: Pointer to storage array
  92. * @size: Size of the storage array
  93. * @skipnr: Number of entries to skip at the start of the stack trace
  94. *
  95. * Return: Number of trace entries stored.
  96. */
  97. unsigned int stack_trace_save(unsigned long *store, unsigned int size,
  98. unsigned int skipnr)
  99. {
  100. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  101. struct stacktrace_cookie c = {
  102. .store = store,
  103. .size = size,
  104. .skip = skipnr + 1,
  105. };
  106. arch_stack_walk(consume_entry, &c, current, NULL);
  107. return c.len;
  108. }
  109. EXPORT_SYMBOL_GPL(stack_trace_save);
  110. /**
  111. * stack_trace_save_tsk - Save a task stack trace into a storage array
  112. * @tsk: The task to examine
  113. * @store: Pointer to storage array
  114. * @size: Size of the storage array
  115. * @skipnr: Number of entries to skip at the start of the stack trace
  116. *
  117. * Return: Number of trace entries stored.
  118. */
  119. unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
  120. unsigned int size, unsigned int skipnr)
  121. {
  122. stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
  123. struct stacktrace_cookie c = {
  124. .store = store,
  125. .size = size,
  126. /* skip this function if they are tracing us */
  127. .skip = skipnr + (current == tsk),
  128. };
  129. if (!try_get_task_stack(tsk))
  130. return 0;
  131. arch_stack_walk(consume_entry, &c, tsk, NULL);
  132. put_task_stack(tsk);
  133. return c.len;
  134. }
  135. EXPORT_SYMBOL_GPL(stack_trace_save_tsk);
  136. /**
  137. * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
  138. * @regs: Pointer to pt_regs to examine
  139. * @store: Pointer to storage array
  140. * @size: Size of the storage array
  141. * @skipnr: Number of entries to skip at the start of the stack trace
  142. *
  143. * Return: Number of trace entries stored.
  144. */
  145. unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
  146. unsigned int size, unsigned int skipnr)
  147. {
  148. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  149. struct stacktrace_cookie c = {
  150. .store = store,
  151. .size = size,
  152. .skip = skipnr,
  153. };
  154. arch_stack_walk(consume_entry, &c, current, regs);
  155. return c.len;
  156. }
  157. #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
  158. /**
  159. * stack_trace_save_tsk_reliable - Save task stack with verification
  160. * @tsk: Pointer to the task to examine
  161. * @store: Pointer to storage array
  162. * @size: Size of the storage array
  163. *
  164. * Return: An error if it detects any unreliable features of the
  165. * stack. Otherwise it guarantees that the stack trace is
  166. * reliable and returns the number of entries stored.
  167. *
  168. * If the task is not 'current', the caller *must* ensure the task is inactive.
  169. */
  170. int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
  171. unsigned int size)
  172. {
  173. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  174. struct stacktrace_cookie c = {
  175. .store = store,
  176. .size = size,
  177. };
  178. int ret;
  179. /*
  180. * If the task doesn't have a stack (e.g., a zombie), the stack is
  181. * "reliably" empty.
  182. */
  183. if (!try_get_task_stack(tsk))
  184. return 0;
  185. ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
  186. put_task_stack(tsk);
  187. return ret ? ret : c.len;
  188. }
  189. #endif
  190. #ifdef CONFIG_USER_STACKTRACE_SUPPORT
  191. /**
  192. * stack_trace_save_user - Save a user space stack trace into a storage array
  193. * @store: Pointer to storage array
  194. * @size: Size of the storage array
  195. *
  196. * Return: Number of trace entries stored.
  197. */
  198. unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
  199. {
  200. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  201. struct stacktrace_cookie c = {
  202. .store = store,
  203. .size = size,
  204. };
  205. /* Trace user stack if not a kernel thread */
  206. if (current->flags & PF_KTHREAD)
  207. return 0;
  208. arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
  209. return c.len;
  210. }
  211. #endif
  212. #else /* CONFIG_ARCH_STACKWALK */
  213. /*
  214. * Architectures that do not implement save_stack_trace_*()
  215. * get these weak aliases and once-per-bootup warnings
  216. * (whenever this facility is utilized - for example by procfs):
  217. */
  218. __weak void
  219. save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  220. {
  221. WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
  222. }
  223. __weak void
  224. save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  225. {
  226. WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
  227. }
  228. /**
  229. * stack_trace_save - Save a stack trace into a storage array
  230. * @store: Pointer to storage array
  231. * @size: Size of the storage array
  232. * @skipnr: Number of entries to skip at the start of the stack trace
  233. *
  234. * Return: Number of trace entries stored
  235. */
  236. unsigned int stack_trace_save(unsigned long *store, unsigned int size,
  237. unsigned int skipnr)
  238. {
  239. struct stack_trace trace = {
  240. .entries = store,
  241. .max_entries = size,
  242. .skip = skipnr + 1,
  243. };
  244. save_stack_trace(&trace);
  245. return trace.nr_entries;
  246. }
  247. EXPORT_SYMBOL_GPL(stack_trace_save);
  248. /**
  249. * stack_trace_save_tsk - Save a task stack trace into a storage array
  250. * @task: The task to examine
  251. * @store: Pointer to storage array
  252. * @size: Size of the storage array
  253. * @skipnr: Number of entries to skip at the start of the stack trace
  254. *
  255. * Return: Number of trace entries stored
  256. */
  257. unsigned int stack_trace_save_tsk(struct task_struct *task,
  258. unsigned long *store, unsigned int size,
  259. unsigned int skipnr)
  260. {
  261. struct stack_trace trace = {
  262. .entries = store,
  263. .max_entries = size,
  264. /* skip this function if they are tracing us */
  265. .skip = skipnr + (current == task),
  266. };
  267. save_stack_trace_tsk(task, &trace);
  268. return trace.nr_entries;
  269. }
  270. EXPORT_SYMBOL_GPL(stack_trace_save_tsk);
  271. /**
  272. * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
  273. * @regs: Pointer to pt_regs to examine
  274. * @store: Pointer to storage array
  275. * @size: Size of the storage array
  276. * @skipnr: Number of entries to skip at the start of the stack trace
  277. *
  278. * Return: Number of trace entries stored
  279. */
  280. unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
  281. unsigned int size, unsigned int skipnr)
  282. {
  283. struct stack_trace trace = {
  284. .entries = store,
  285. .max_entries = size,
  286. .skip = skipnr,
  287. };
  288. save_stack_trace_regs(regs, &trace);
  289. return trace.nr_entries;
  290. }
  291. #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
  292. /**
  293. * stack_trace_save_tsk_reliable - Save task stack with verification
  294. * @tsk: Pointer to the task to examine
  295. * @store: Pointer to storage array
  296. * @size: Size of the storage array
  297. *
  298. * Return: An error if it detects any unreliable features of the
  299. * stack. Otherwise it guarantees that the stack trace is
  300. * reliable and returns the number of entries stored.
  301. *
  302. * If the task is not 'current', the caller *must* ensure the task is inactive.
  303. */
  304. int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
  305. unsigned int size)
  306. {
  307. struct stack_trace trace = {
  308. .entries = store,
  309. .max_entries = size,
  310. };
  311. int ret = save_stack_trace_tsk_reliable(tsk, &trace);
  312. return ret ? ret : trace.nr_entries;
  313. }
  314. #endif
  315. #ifdef CONFIG_USER_STACKTRACE_SUPPORT
  316. /**
  317. * stack_trace_save_user - Save a user space stack trace into a storage array
  318. * @store: Pointer to storage array
  319. * @size: Size of the storage array
  320. *
  321. * Return: Number of trace entries stored
  322. */
  323. unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
  324. {
  325. struct stack_trace trace = {
  326. .entries = store,
  327. .max_entries = size,
  328. };
  329. save_stack_trace_user(&trace);
  330. return trace.nr_entries;
  331. }
  332. #endif /* CONFIG_USER_STACKTRACE_SUPPORT */
  333. #endif /* !CONFIG_ARCH_STACKWALK */
  334. static inline bool in_irqentry_text(unsigned long ptr)
  335. {
  336. return (ptr >= (unsigned long)&__irqentry_text_start &&
  337. ptr < (unsigned long)&__irqentry_text_end) ||
  338. (ptr >= (unsigned long)&__softirqentry_text_start &&
  339. ptr < (unsigned long)&__softirqentry_text_end);
  340. }
  341. /**
  342. * filter_irq_stacks - Find first IRQ stack entry in trace
  343. * @entries: Pointer to stack trace array
  344. * @nr_entries: Number of entries in the storage array
  345. *
  346. * Return: Number of trace entries until IRQ stack starts.
  347. */
  348. unsigned int filter_irq_stacks(unsigned long *entries, unsigned int nr_entries)
  349. {
  350. unsigned int i;
  351. for (i = 0; i < nr_entries; i++) {
  352. if (in_irqentry_text(entries[i])) {
  353. /* Include the irqentry function into the stack. */
  354. return i + 1;
  355. }
  356. }
  357. return nr_entries;
  358. }
  359. EXPORT_SYMBOL_GPL(filter_irq_stacks);