stacktrace.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /**
  17. * stack_trace_print - Print the entries in the stack trace
  18. * @entries: Pointer to storage array
  19. * @nr_entries: Number of entries in the storage array
  20. * @spaces: Number of leading spaces to print
  21. */
  22. void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
  23. int spaces)
  24. {
  25. unsigned int i;
  26. if (WARN_ON(!entries))
  27. return;
  28. for (i = 0; i < nr_entries; i++)
  29. printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
  30. }
  31. EXPORT_SYMBOL_GPL(stack_trace_print);
  32. /**
  33. * stack_trace_snprint - Print the entries in the stack trace into a buffer
  34. * @buf: Pointer to the print buffer
  35. * @size: Size of the print buffer
  36. * @entries: Pointer to storage array
  37. * @nr_entries: Number of entries in the storage array
  38. * @spaces: Number of leading spaces to print
  39. *
  40. * Return: Number of bytes printed.
  41. */
  42. int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
  43. unsigned int nr_entries, int spaces)
  44. {
  45. unsigned int generated, i, total = 0;
  46. if (WARN_ON(!entries))
  47. return 0;
  48. for (i = 0; i < nr_entries && size; i++) {
  49. generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
  50. (void *)entries[i]);
  51. total += generated;
  52. if (generated >= size) {
  53. buf += size;
  54. size = 0;
  55. } else {
  56. buf += generated;
  57. size -= generated;
  58. }
  59. }
  60. return total;
  61. }
  62. EXPORT_SYMBOL_GPL(stack_trace_snprint);
  63. #ifdef CONFIG_ARCH_STACKWALK
  64. struct stacktrace_cookie {
  65. unsigned long *store;
  66. unsigned int size;
  67. unsigned int skip;
  68. unsigned int len;
  69. };
  70. static bool stack_trace_consume_entry(void *cookie, unsigned long addr)
  71. {
  72. struct stacktrace_cookie *c = cookie;
  73. if (c->len >= c->size)
  74. return false;
  75. if (c->skip > 0) {
  76. c->skip--;
  77. return true;
  78. }
  79. c->store[c->len++] = addr;
  80. return c->len < c->size;
  81. }
  82. static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr)
  83. {
  84. if (in_sched_functions(addr))
  85. return true;
  86. return stack_trace_consume_entry(cookie, addr);
  87. }
  88. /**
  89. * stack_trace_save - Save a stack trace into a storage array
  90. * @store: Pointer to storage array
  91. * @size: Size of the storage array
  92. * @skipnr: Number of entries to skip at the start of the stack trace
  93. *
  94. * Return: Number of trace entries stored.
  95. */
  96. unsigned int stack_trace_save(unsigned long *store, unsigned int size,
  97. unsigned int skipnr)
  98. {
  99. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  100. struct stacktrace_cookie c = {
  101. .store = store,
  102. .size = size,
  103. .skip = skipnr + 1,
  104. };
  105. arch_stack_walk(consume_entry, &c, current, NULL);
  106. return c.len;
  107. }
  108. EXPORT_SYMBOL_GPL(stack_trace_save);
  109. /**
  110. * stack_trace_save_tsk - Save a task stack trace into a storage array
  111. * @task: The task to examine
  112. * @store: Pointer to storage array
  113. * @size: Size of the storage array
  114. * @skipnr: Number of entries to skip at the start of the stack trace
  115. *
  116. * Return: Number of trace entries stored.
  117. */
  118. unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
  119. unsigned int size, unsigned int skipnr)
  120. {
  121. stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
  122. struct stacktrace_cookie c = {
  123. .store = store,
  124. .size = size,
  125. /* skip this function if they are tracing us */
  126. .skip = skipnr + (current == tsk),
  127. };
  128. if (!try_get_task_stack(tsk))
  129. return 0;
  130. arch_stack_walk(consume_entry, &c, tsk, NULL);
  131. put_task_stack(tsk);
  132. return c.len;
  133. }
  134. EXPORT_SYMBOL_GPL(stack_trace_save_tsk);
  135. /**
  136. * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
  137. * @regs: Pointer to pt_regs to examine
  138. * @store: Pointer to storage array
  139. * @size: Size of the storage array
  140. * @skipnr: Number of entries to skip at the start of the stack trace
  141. *
  142. * Return: Number of trace entries stored.
  143. */
  144. unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
  145. unsigned int size, unsigned int skipnr)
  146. {
  147. stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
  148. struct stacktrace_cookie c = {
  149. .store = store,
  150. .size = size,
  151. .skip = skipnr,
  152. };
  153. arch_stack_walk(consume_entry, &c, current, regs);
  154. return c.len;
  155. }
  156. EXPORT_SYMBOL_GPL(stack_trace_save_regs);
  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. mm_segment_t fs;
  206. /* Trace user stack if not a kernel thread */
  207. if (current->flags & PF_KTHREAD)
  208. return 0;
  209. fs = force_uaccess_begin();
  210. arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
  211. force_uaccess_end(fs);
  212. return c.len;
  213. }
  214. #endif
  215. #else /* CONFIG_ARCH_STACKWALK */
  216. /*
  217. * Architectures that do not implement save_stack_trace_*()
  218. * get these weak aliases and once-per-bootup warnings
  219. * (whenever this facility is utilized - for example by procfs):
  220. */
  221. __weak void
  222. save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  223. {
  224. WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
  225. }
  226. __weak void
  227. save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  228. {
  229. WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
  230. }
  231. /**
  232. * stack_trace_save - Save a stack trace into a storage array
  233. * @store: Pointer to storage array
  234. * @size: Size of the storage array
  235. * @skipnr: Number of entries to skip at the start of the stack trace
  236. *
  237. * Return: Number of trace entries stored
  238. */
  239. unsigned int stack_trace_save(unsigned long *store, unsigned int size,
  240. unsigned int skipnr)
  241. {
  242. struct stack_trace trace = {
  243. .entries = store,
  244. .max_entries = size,
  245. .skip = skipnr + 1,
  246. };
  247. save_stack_trace(&trace);
  248. return trace.nr_entries;
  249. }
  250. EXPORT_SYMBOL_GPL(stack_trace_save);
  251. /**
  252. * stack_trace_save_tsk - Save a task stack trace into a storage array
  253. * @task: The task to examine
  254. * @store: Pointer to storage array
  255. * @size: Size of the storage array
  256. * @skipnr: Number of entries to skip at the start of the stack trace
  257. *
  258. * Return: Number of trace entries stored
  259. */
  260. unsigned int stack_trace_save_tsk(struct task_struct *task,
  261. unsigned long *store, unsigned int size,
  262. unsigned int skipnr)
  263. {
  264. struct stack_trace trace = {
  265. .entries = store,
  266. .max_entries = size,
  267. /* skip this function if they are tracing us */
  268. .skip = skipnr + (current == task),
  269. };
  270. save_stack_trace_tsk(task, &trace);
  271. return trace.nr_entries;
  272. }
  273. /**
  274. * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
  275. * @regs: Pointer to pt_regs to examine
  276. * @store: Pointer to storage array
  277. * @size: Size of the storage array
  278. * @skipnr: Number of entries to skip at the start of the stack trace
  279. *
  280. * Return: Number of trace entries stored
  281. */
  282. unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
  283. unsigned int size, unsigned int skipnr)
  284. {
  285. struct stack_trace trace = {
  286. .entries = store,
  287. .max_entries = size,
  288. .skip = skipnr,
  289. };
  290. save_stack_trace_regs(regs, &trace);
  291. return trace.nr_entries;
  292. }
  293. #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
  294. /**
  295. * stack_trace_save_tsk_reliable - Save task stack with verification
  296. * @tsk: Pointer to the task to examine
  297. * @store: Pointer to storage array
  298. * @size: Size of the storage array
  299. *
  300. * Return: An error if it detects any unreliable features of the
  301. * stack. Otherwise it guarantees that the stack trace is
  302. * reliable and returns the number of entries stored.
  303. *
  304. * If the task is not 'current', the caller *must* ensure the task is inactive.
  305. */
  306. int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
  307. unsigned int size)
  308. {
  309. struct stack_trace trace = {
  310. .entries = store,
  311. .max_entries = size,
  312. };
  313. int ret = save_stack_trace_tsk_reliable(tsk, &trace);
  314. return ret ? ret : trace.nr_entries;
  315. }
  316. #endif
  317. #ifdef CONFIG_USER_STACKTRACE_SUPPORT
  318. /**
  319. * stack_trace_save_user - Save a user space stack trace into a storage array
  320. * @store: Pointer to storage array
  321. * @size: Size of the storage array
  322. *
  323. * Return: Number of trace entries stored
  324. */
  325. unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
  326. {
  327. struct stack_trace trace = {
  328. .entries = store,
  329. .max_entries = size,
  330. };
  331. save_stack_trace_user(&trace);
  332. return trace.nr_entries;
  333. }
  334. #endif /* CONFIG_USER_STACKTRACE_SUPPORT */
  335. #endif /* !CONFIG_ARCH_STACKWALK */