kdb_support.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Kernel Debugger Architecture Independent Support Functions
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
  9. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
  10. * 03/02/13 added new 2.5 kallsyms <[email protected]>
  11. */
  12. #include <linux/types.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/stddef.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/highmem.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/delay.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/kdb.h>
  24. #include <linux/slab.h>
  25. #include <linux/ctype.h>
  26. #include "kdb_private.h"
  27. /*
  28. * kdbgetsymval - Return the address of the given symbol.
  29. *
  30. * Parameters:
  31. * symname Character string containing symbol name
  32. * symtab Structure to receive results
  33. * Returns:
  34. * 0 Symbol not found, symtab zero filled
  35. * 1 Symbol mapped to module/symbol/section, data in symtab
  36. */
  37. int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
  38. {
  39. kdb_dbg_printf(AR, "symname=%s, symtab=%px\n", symname, symtab);
  40. memset(symtab, 0, sizeof(*symtab));
  41. symtab->sym_start = kallsyms_lookup_name(symname);
  42. if (symtab->sym_start) {
  43. kdb_dbg_printf(AR, "returns 1, symtab->sym_start=0x%lx\n",
  44. symtab->sym_start);
  45. return 1;
  46. }
  47. kdb_dbg_printf(AR, "returns 0\n");
  48. return 0;
  49. }
  50. EXPORT_SYMBOL(kdbgetsymval);
  51. /**
  52. * kdbnearsym() - Return the name of the symbol with the nearest address
  53. * less than @addr.
  54. * @addr: Address to check for near symbol
  55. * @symtab: Structure to receive results
  56. *
  57. * WARNING: This function may return a pointer to a single statically
  58. * allocated buffer (namebuf). kdb's unusual calling context (single
  59. * threaded, all other CPUs halted) provides us sufficient locking for
  60. * this to be safe. The only constraint imposed by the static buffer is
  61. * that the caller must consume any previous reply prior to another call
  62. * to lookup a new symbol.
  63. *
  64. * Note that, strictly speaking, some architectures may re-enter the kdb
  65. * trap if the system turns out to be very badly damaged and this breaks
  66. * the single-threaded assumption above. In these circumstances successful
  67. * continuation and exit from the inner trap is unlikely to work and any
  68. * user attempting this receives a prominent warning before being allowed
  69. * to progress. In these circumstances we remain memory safe because
  70. * namebuf[KSYM_NAME_LEN-1] will never change from '\0' although we do
  71. * tolerate the possibility of garbled symbol display from the outer kdb
  72. * trap.
  73. *
  74. * Return:
  75. * * 0 - No sections contain this address, symtab zero filled
  76. * * 1 - Address mapped to module/symbol/section, data in symtab
  77. */
  78. int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
  79. {
  80. int ret = 0;
  81. unsigned long symbolsize = 0;
  82. unsigned long offset = 0;
  83. static char namebuf[KSYM_NAME_LEN];
  84. kdb_dbg_printf(AR, "addr=0x%lx, symtab=%px\n", addr, symtab);
  85. memset(symtab, 0, sizeof(*symtab));
  86. if (addr < 4096)
  87. goto out;
  88. symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset,
  89. (char **)(&symtab->mod_name), namebuf);
  90. if (offset > 8*1024*1024) {
  91. symtab->sym_name = NULL;
  92. addr = offset = symbolsize = 0;
  93. }
  94. symtab->sym_start = addr - offset;
  95. symtab->sym_end = symtab->sym_start + symbolsize;
  96. ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';
  97. if (symtab->mod_name == NULL)
  98. symtab->mod_name = "kernel";
  99. kdb_dbg_printf(AR, "returns %d symtab->sym_start=0x%lx, symtab->mod_name=%px, symtab->sym_name=%px (%s)\n",
  100. ret, symtab->sym_start, symtab->mod_name, symtab->sym_name, symtab->sym_name);
  101. out:
  102. return ret;
  103. }
  104. static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];
  105. /*
  106. * kallsyms_symbol_complete
  107. *
  108. * Parameters:
  109. * prefix_name prefix of a symbol name to lookup
  110. * max_len maximum length that can be returned
  111. * Returns:
  112. * Number of symbols which match the given prefix.
  113. * Notes:
  114. * prefix_name is changed to contain the longest unique prefix that
  115. * starts with this prefix (tab completion).
  116. */
  117. int kallsyms_symbol_complete(char *prefix_name, int max_len)
  118. {
  119. loff_t pos = 0;
  120. int prefix_len = strlen(prefix_name), prev_len = 0;
  121. int i, number = 0;
  122. const char *name;
  123. while ((name = kdb_walk_kallsyms(&pos))) {
  124. if (strncmp(name, prefix_name, prefix_len) == 0) {
  125. strscpy(ks_namebuf, name, sizeof(ks_namebuf));
  126. /* Work out the longest name that matches the prefix */
  127. if (++number == 1) {
  128. prev_len = min_t(int, max_len-1,
  129. strlen(ks_namebuf));
  130. memcpy(ks_namebuf_prev, ks_namebuf, prev_len);
  131. ks_namebuf_prev[prev_len] = '\0';
  132. continue;
  133. }
  134. for (i = 0; i < prev_len; i++) {
  135. if (ks_namebuf[i] != ks_namebuf_prev[i]) {
  136. prev_len = i;
  137. ks_namebuf_prev[i] = '\0';
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. if (prev_len > prefix_len)
  144. memcpy(prefix_name, ks_namebuf_prev, prev_len+1);
  145. return number;
  146. }
  147. /*
  148. * kallsyms_symbol_next
  149. *
  150. * Parameters:
  151. * prefix_name prefix of a symbol name to lookup
  152. * flag 0 means search from the head, 1 means continue search.
  153. * buf_size maximum length that can be written to prefix_name
  154. * buffer
  155. * Returns:
  156. * 1 if a symbol matches the given prefix.
  157. * 0 if no string found
  158. */
  159. int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)
  160. {
  161. int prefix_len = strlen(prefix_name);
  162. static loff_t pos;
  163. const char *name;
  164. if (!flag)
  165. pos = 0;
  166. while ((name = kdb_walk_kallsyms(&pos))) {
  167. if (!strncmp(name, prefix_name, prefix_len))
  168. return strscpy(prefix_name, name, buf_size);
  169. }
  170. return 0;
  171. }
  172. /*
  173. * kdb_symbol_print - Standard method for printing a symbol name and offset.
  174. * Inputs:
  175. * addr Address to be printed.
  176. * symtab Address of symbol data, if NULL this routine does its
  177. * own lookup.
  178. * punc Punctuation for string, bit field.
  179. * Remarks:
  180. * The string and its punctuation is only printed if the address
  181. * is inside the kernel, except that the value is always printed
  182. * when requested.
  183. */
  184. void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,
  185. unsigned int punc)
  186. {
  187. kdb_symtab_t symtab, *symtab_p2;
  188. if (symtab_p) {
  189. symtab_p2 = (kdb_symtab_t *)symtab_p;
  190. } else {
  191. symtab_p2 = &symtab;
  192. kdbnearsym(addr, symtab_p2);
  193. }
  194. if (!(symtab_p2->sym_name || (punc & KDB_SP_VALUE)))
  195. return;
  196. if (punc & KDB_SP_SPACEB)
  197. kdb_printf(" ");
  198. if (punc & KDB_SP_VALUE)
  199. kdb_printf(kdb_machreg_fmt0, addr);
  200. if (symtab_p2->sym_name) {
  201. if (punc & KDB_SP_VALUE)
  202. kdb_printf(" ");
  203. if (punc & KDB_SP_PAREN)
  204. kdb_printf("(");
  205. if (strcmp(symtab_p2->mod_name, "kernel"))
  206. kdb_printf("[%s]", symtab_p2->mod_name);
  207. kdb_printf("%s", symtab_p2->sym_name);
  208. if (addr != symtab_p2->sym_start)
  209. kdb_printf("+0x%lx", addr - symtab_p2->sym_start);
  210. if (punc & KDB_SP_SYMSIZE)
  211. kdb_printf("/0x%lx",
  212. symtab_p2->sym_end - symtab_p2->sym_start);
  213. if (punc & KDB_SP_PAREN)
  214. kdb_printf(")");
  215. }
  216. if (punc & KDB_SP_SPACEA)
  217. kdb_printf(" ");
  218. if (punc & KDB_SP_NEWLINE)
  219. kdb_printf("\n");
  220. }
  221. /*
  222. * kdb_strdup - kdb equivalent of strdup, for disasm code.
  223. * Inputs:
  224. * str The string to duplicate.
  225. * type Flags to kmalloc for the new string.
  226. * Returns:
  227. * Address of the new string, NULL if storage could not be allocated.
  228. * Remarks:
  229. * This is not in lib/string.c because it uses kmalloc which is not
  230. * available when string.o is used in boot loaders.
  231. */
  232. char *kdb_strdup(const char *str, gfp_t type)
  233. {
  234. int n = strlen(str)+1;
  235. char *s = kmalloc(n, type);
  236. if (!s)
  237. return NULL;
  238. return strcpy(s, str);
  239. }
  240. /*
  241. * kdb_getarea_size - Read an area of data. The kdb equivalent of
  242. * copy_from_user, with kdb messages for invalid addresses.
  243. * Inputs:
  244. * res Pointer to the area to receive the result.
  245. * addr Address of the area to copy.
  246. * size Size of the area.
  247. * Returns:
  248. * 0 for success, < 0 for error.
  249. */
  250. int kdb_getarea_size(void *res, unsigned long addr, size_t size)
  251. {
  252. int ret = copy_from_kernel_nofault((char *)res, (char *)addr, size);
  253. if (ret) {
  254. if (!KDB_STATE(SUPPRESS)) {
  255. kdb_func_printf("Bad address 0x%lx\n", addr);
  256. KDB_STATE_SET(SUPPRESS);
  257. }
  258. ret = KDB_BADADDR;
  259. } else {
  260. KDB_STATE_CLEAR(SUPPRESS);
  261. }
  262. return ret;
  263. }
  264. /*
  265. * kdb_putarea_size - Write an area of data. The kdb equivalent of
  266. * copy_to_user, with kdb messages for invalid addresses.
  267. * Inputs:
  268. * addr Address of the area to write to.
  269. * res Pointer to the area holding the data.
  270. * size Size of the area.
  271. * Returns:
  272. * 0 for success, < 0 for error.
  273. */
  274. int kdb_putarea_size(unsigned long addr, void *res, size_t size)
  275. {
  276. int ret = copy_to_kernel_nofault((char *)addr, (char *)res, size);
  277. if (ret) {
  278. if (!KDB_STATE(SUPPRESS)) {
  279. kdb_func_printf("Bad address 0x%lx\n", addr);
  280. KDB_STATE_SET(SUPPRESS);
  281. }
  282. ret = KDB_BADADDR;
  283. } else {
  284. KDB_STATE_CLEAR(SUPPRESS);
  285. }
  286. return ret;
  287. }
  288. /*
  289. * kdb_getphys - Read data from a physical address. Validate the
  290. * address is in range, use kmap_atomic() to get data
  291. * similar to kdb_getarea() - but for phys addresses
  292. * Inputs:
  293. * res Pointer to the word to receive the result
  294. * addr Physical address of the area to copy
  295. * size Size of the area
  296. * Returns:
  297. * 0 for success, < 0 for error.
  298. */
  299. static int kdb_getphys(void *res, unsigned long addr, size_t size)
  300. {
  301. unsigned long pfn;
  302. void *vaddr;
  303. struct page *page;
  304. pfn = (addr >> PAGE_SHIFT);
  305. if (!pfn_valid(pfn))
  306. return 1;
  307. page = pfn_to_page(pfn);
  308. vaddr = kmap_atomic(page);
  309. memcpy(res, vaddr + (addr & (PAGE_SIZE - 1)), size);
  310. kunmap_atomic(vaddr);
  311. return 0;
  312. }
  313. /*
  314. * kdb_getphysword
  315. * Inputs:
  316. * word Pointer to the word to receive the result.
  317. * addr Address of the area to copy.
  318. * size Size of the area.
  319. * Returns:
  320. * 0 for success, < 0 for error.
  321. */
  322. int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size)
  323. {
  324. int diag;
  325. __u8 w1;
  326. __u16 w2;
  327. __u32 w4;
  328. __u64 w8;
  329. *word = 0; /* Default value if addr or size is invalid */
  330. switch (size) {
  331. case 1:
  332. diag = kdb_getphys(&w1, addr, sizeof(w1));
  333. if (!diag)
  334. *word = w1;
  335. break;
  336. case 2:
  337. diag = kdb_getphys(&w2, addr, sizeof(w2));
  338. if (!diag)
  339. *word = w2;
  340. break;
  341. case 4:
  342. diag = kdb_getphys(&w4, addr, sizeof(w4));
  343. if (!diag)
  344. *word = w4;
  345. break;
  346. case 8:
  347. if (size <= sizeof(*word)) {
  348. diag = kdb_getphys(&w8, addr, sizeof(w8));
  349. if (!diag)
  350. *word = w8;
  351. break;
  352. }
  353. fallthrough;
  354. default:
  355. diag = KDB_BADWIDTH;
  356. kdb_func_printf("bad width %zu\n", size);
  357. }
  358. return diag;
  359. }
  360. /*
  361. * kdb_getword - Read a binary value. Unlike kdb_getarea, this treats
  362. * data as numbers.
  363. * Inputs:
  364. * word Pointer to the word to receive the result.
  365. * addr Address of the area to copy.
  366. * size Size of the area.
  367. * Returns:
  368. * 0 for success, < 0 for error.
  369. */
  370. int kdb_getword(unsigned long *word, unsigned long addr, size_t size)
  371. {
  372. int diag;
  373. __u8 w1;
  374. __u16 w2;
  375. __u32 w4;
  376. __u64 w8;
  377. *word = 0; /* Default value if addr or size is invalid */
  378. switch (size) {
  379. case 1:
  380. diag = kdb_getarea(w1, addr);
  381. if (!diag)
  382. *word = w1;
  383. break;
  384. case 2:
  385. diag = kdb_getarea(w2, addr);
  386. if (!diag)
  387. *word = w2;
  388. break;
  389. case 4:
  390. diag = kdb_getarea(w4, addr);
  391. if (!diag)
  392. *word = w4;
  393. break;
  394. case 8:
  395. if (size <= sizeof(*word)) {
  396. diag = kdb_getarea(w8, addr);
  397. if (!diag)
  398. *word = w8;
  399. break;
  400. }
  401. fallthrough;
  402. default:
  403. diag = KDB_BADWIDTH;
  404. kdb_func_printf("bad width %zu\n", size);
  405. }
  406. return diag;
  407. }
  408. /*
  409. * kdb_putword - Write a binary value. Unlike kdb_putarea, this
  410. * treats data as numbers.
  411. * Inputs:
  412. * addr Address of the area to write to..
  413. * word The value to set.
  414. * size Size of the area.
  415. * Returns:
  416. * 0 for success, < 0 for error.
  417. */
  418. int kdb_putword(unsigned long addr, unsigned long word, size_t size)
  419. {
  420. int diag;
  421. __u8 w1;
  422. __u16 w2;
  423. __u32 w4;
  424. __u64 w8;
  425. switch (size) {
  426. case 1:
  427. w1 = word;
  428. diag = kdb_putarea(addr, w1);
  429. break;
  430. case 2:
  431. w2 = word;
  432. diag = kdb_putarea(addr, w2);
  433. break;
  434. case 4:
  435. w4 = word;
  436. diag = kdb_putarea(addr, w4);
  437. break;
  438. case 8:
  439. if (size <= sizeof(word)) {
  440. w8 = word;
  441. diag = kdb_putarea(addr, w8);
  442. break;
  443. }
  444. fallthrough;
  445. default:
  446. diag = KDB_BADWIDTH;
  447. kdb_func_printf("bad width %zu\n", size);
  448. }
  449. return diag;
  450. }
  451. /*
  452. * kdb_task_state_char - Return the character that represents the task state.
  453. * Inputs:
  454. * p struct task for the process
  455. * Returns:
  456. * One character to represent the task state.
  457. */
  458. char kdb_task_state_char (const struct task_struct *p)
  459. {
  460. unsigned long tmp;
  461. char state;
  462. int cpu;
  463. if (!p ||
  464. copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long)))
  465. return 'E';
  466. state = task_state_to_char((struct task_struct *) p);
  467. if (is_idle_task(p)) {
  468. /* Idle task. Is it really idle, apart from the kdb
  469. * interrupt? */
  470. cpu = kdb_process_cpu(p);
  471. if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) {
  472. if (cpu != kdb_initial_cpu)
  473. state = '-'; /* idle task */
  474. }
  475. } else if (!p->mm && strchr("IMS", state)) {
  476. state = tolower(state); /* sleeping system daemon */
  477. }
  478. return state;
  479. }
  480. /*
  481. * kdb_task_state - Return true if a process has the desired state
  482. * given by the mask.
  483. * Inputs:
  484. * p struct task for the process
  485. * mask set of characters used to select processes; both NULL
  486. * and the empty string mean adopt a default filter, which
  487. * is to suppress sleeping system daemons and the idle tasks
  488. * Returns:
  489. * True if the process matches at least one criteria defined by the mask.
  490. */
  491. bool kdb_task_state(const struct task_struct *p, const char *mask)
  492. {
  493. char state = kdb_task_state_char(p);
  494. /* If there is no mask, then we will filter code that runs when the
  495. * scheduler is idling and any system daemons that are currently
  496. * sleeping.
  497. */
  498. if (!mask || mask[0] == '\0')
  499. return !strchr("-ims", state);
  500. /* A is a special case that matches all states */
  501. if (strchr(mask, 'A'))
  502. return true;
  503. return strchr(mask, state);
  504. }
  505. /* Maintain a small stack of kdb_flags to allow recursion without disturbing
  506. * the global kdb state.
  507. */
  508. static int kdb_flags_stack[4], kdb_flags_index;
  509. void kdb_save_flags(void)
  510. {
  511. BUG_ON(kdb_flags_index >= ARRAY_SIZE(kdb_flags_stack));
  512. kdb_flags_stack[kdb_flags_index++] = kdb_flags;
  513. }
  514. void kdb_restore_flags(void)
  515. {
  516. BUG_ON(kdb_flags_index <= 0);
  517. kdb_flags = kdb_flags_stack[--kdb_flags_index];
  518. }