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 <[email protected]>
  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. * @task: 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. EXPORT_SYMBOL_GPL(stack_trace_save_regs);
  158. #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
  159. /**
  160. * stack_trace_save_tsk_reliable - Save task stack with verification
  161. * @tsk: Pointer to the task to examine
  162. * @store: Pointer to storage array
  163. * @size: Size of the storage array
  164. *
  165. * Return: An error if it detects any unreliable features of the
  166. * stack. Otherwise it guarantees that the stack trace is
  167. * reliable and returns the number of entries stored.
  168. *
  169. * If the task is not 'current', the caller *must* ensure the task is inactive.
  170. */
  171. int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
  172. unsigned int size)
  173. {
  174. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  175. struct stacktrace_cookie c = {
  176. .store = store,
  177. .size = size,
  178. };
  179. int ret;
  180. /*
  181. * If the task doesn't have a stack (e.g., a zombie), the stack is
  182. * "reliably" empty.
  183. */
  184. if (!try_get_task_stack(tsk))
  185. return 0;
  186. ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
  187. put_task_stack(tsk);
  188. return ret ? ret : c.len;
  189. }
  190. #endif
  191. #ifdef CONFIG_USER_STACKTRACE_SUPPORT
  192. /**
  193. * stack_trace_save_user - Save a user space stack trace into a storage array
  194. * @store: Pointer to storage array
  195. * @size: Size of the storage array
  196. *
  197. * Return: Number of trace entries stored.
  198. */
  199. unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
  200. {
  201. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  202. struct stacktrace_cookie c = {
  203. .store = store,
  204. .size = size,
  205. };
  206. /* Trace user stack if not a kernel thread */
  207. if (current->flags & PF_KTHREAD)
  208. return 0;
  209. arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
  210. return c.len;
  211. }
  212. #endif
  213. #else /* CONFIG_ARCH_STACKWALK */
  214. /*
  215. * Architectures that do not implement save_stack_trace_*()
  216. * get these weak aliases and once-per-bootup warnings
  217. * (whenever this facility is utilized - for example by procfs):
  218. */
  219. __weak void
  220. save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  221. {
  222. WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
  223. }
  224. __weak void
  225. save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  226. {
  227. WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
  228. }
  229. /**
  230. * stack_trace_save - Save a stack trace into a storage array
  231. * @store: Pointer to storage array
  232. * @size: Size of the storage array
  233. * @skipnr: Number of entries to skip at the start of the stack trace
  234. *
  235. * Return: Number of trace entries stored
  236. */
  237. unsigned int stack_trace_save(unsigned long *store, unsigned int size,
  238. unsigned int skipnr)
  239. {
  240. struct stack_trace trace = {
  241. .entries = store,
  242. .max_entries = size,
  243. .skip = skipnr + 1,
  244. };
  245. save_stack_trace(&trace);
  246. return trace.nr_entries;
  247. }
  248. EXPORT_SYMBOL_GPL(stack_trace_save);
  249. /**
  250. * stack_trace_save_tsk - Save a task stack trace into a storage array
  251. * @task: The task to examine
  252. * @store: Pointer to storage array
  253. * @size: Size of the storage array
  254. * @skipnr: Number of entries to skip at the start of the stack trace
  255. *
  256. * Return: Number of trace entries stored
  257. */
  258. unsigned int stack_trace_save_tsk(struct task_struct *task,
  259. unsigned long *store, unsigned int size,
  260. unsigned int skipnr)
  261. {
  262. struct stack_trace trace = {
  263. .entries = store,
  264. .max_entries = size,
  265. /* skip this function if they are tracing us */
  266. .skip = skipnr + (current == task),
  267. };
  268. save_stack_trace_tsk(task, &trace);
  269. return trace.nr_entries;
  270. }
  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);