kdb_support.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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 <xavier.bru@bull.net>
  11. */
  12. #include <stdarg.h>
  13. #include <linux/types.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/stddef.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/module.h>
  21. #include <linux/highmem.h>
  22. #include <linux/hardirq.h>
  23. #include <linux/delay.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/kdb.h>
  26. #include <linux/slab.h>
  27. #include "kdb_private.h"
  28. /*
  29. * kdbgetsymval - Return the address of the given symbol.
  30. *
  31. * Parameters:
  32. * symname Character string containing symbol name
  33. * symtab Structure to receive results
  34. * Returns:
  35. * 0 Symbol not found, symtab zero filled
  36. * 1 Symbol mapped to module/symbol/section, data in symtab
  37. */
  38. int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
  39. {
  40. if (KDB_DEBUG(AR))
  41. kdb_printf("kdbgetsymval: symname=%s, symtab=%p\n", symname,
  42. symtab);
  43. memset(symtab, 0, sizeof(*symtab));
  44. symtab->sym_start = kallsyms_lookup_name(symname);
  45. if (symtab->sym_start) {
  46. if (KDB_DEBUG(AR))
  47. kdb_printf("kdbgetsymval: returns 1, "
  48. "symtab->sym_start=0x%lx\n",
  49. symtab->sym_start);
  50. return 1;
  51. }
  52. if (KDB_DEBUG(AR))
  53. kdb_printf("kdbgetsymval: returns 0\n");
  54. return 0;
  55. }
  56. EXPORT_SYMBOL(kdbgetsymval);
  57. static char *kdb_name_table[100]; /* arbitrary size */
  58. /*
  59. * kdbnearsym - Return the name of the symbol with the nearest address
  60. * less than 'addr'.
  61. *
  62. * Parameters:
  63. * addr Address to check for symbol near
  64. * symtab Structure to receive results
  65. * Returns:
  66. * 0 No sections contain this address, symtab zero filled
  67. * 1 Address mapped to module/symbol/section, data in symtab
  68. * Remarks:
  69. * 2.6 kallsyms has a "feature" where it unpacks the name into a
  70. * string. If that string is reused before the caller expects it
  71. * then the caller sees its string change without warning. To
  72. * avoid cluttering up the main kdb code with lots of kdb_strdup,
  73. * tests and kfree calls, kdbnearsym maintains an LRU list of the
  74. * last few unique strings. The list is sized large enough to
  75. * hold active strings, no kdb caller of kdbnearsym makes more
  76. * than ~20 later calls before using a saved value.
  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. #define knt1_size 128 /* must be >= kallsyms table size */
  84. char *knt1 = NULL;
  85. if (KDB_DEBUG(AR))
  86. kdb_printf("kdbnearsym: addr=0x%lx, symtab=%p\n", addr, symtab);
  87. memset(symtab, 0, sizeof(*symtab));
  88. if (addr < 4096)
  89. goto out;
  90. knt1 = debug_kmalloc(knt1_size, GFP_ATOMIC);
  91. if (!knt1) {
  92. kdb_printf("kdbnearsym: addr=0x%lx cannot kmalloc knt1\n",
  93. addr);
  94. goto out;
  95. }
  96. symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset,
  97. (char **)(&symtab->mod_name), knt1);
  98. if (offset > 8*1024*1024) {
  99. symtab->sym_name = NULL;
  100. addr = offset = symbolsize = 0;
  101. }
  102. symtab->sym_start = addr - offset;
  103. symtab->sym_end = symtab->sym_start + symbolsize;
  104. ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';
  105. if (ret) {
  106. int i;
  107. /* Another 2.6 kallsyms "feature". Sometimes the sym_name is
  108. * set but the buffer passed into kallsyms_lookup is not used,
  109. * so it contains garbage. The caller has to work out which
  110. * buffer needs to be saved.
  111. *
  112. * What was Rusty smoking when he wrote that code?
  113. */
  114. if (symtab->sym_name != knt1) {
  115. strncpy(knt1, symtab->sym_name, knt1_size);
  116. knt1[knt1_size-1] = '\0';
  117. }
  118. for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
  119. if (kdb_name_table[i] &&
  120. strcmp(kdb_name_table[i], knt1) == 0)
  121. break;
  122. }
  123. if (i >= ARRAY_SIZE(kdb_name_table)) {
  124. debug_kfree(kdb_name_table[0]);
  125. memmove(kdb_name_table, kdb_name_table+1,
  126. sizeof(kdb_name_table[0]) *
  127. (ARRAY_SIZE(kdb_name_table)-1));
  128. } else {
  129. debug_kfree(knt1);
  130. knt1 = kdb_name_table[i];
  131. memmove(kdb_name_table+i, kdb_name_table+i+1,
  132. sizeof(kdb_name_table[0]) *
  133. (ARRAY_SIZE(kdb_name_table)-i-1));
  134. }
  135. i = ARRAY_SIZE(kdb_name_table) - 1;
  136. kdb_name_table[i] = knt1;
  137. symtab->sym_name = kdb_name_table[i];
  138. knt1 = NULL;
  139. }
  140. if (symtab->mod_name == NULL)
  141. symtab->mod_name = "kernel";
  142. if (KDB_DEBUG(AR))
  143. kdb_printf("kdbnearsym: returns %d symtab->sym_start=0x%lx, "
  144. "symtab->mod_name=%p, symtab->sym_name=%p (%s)\n", ret,
  145. symtab->sym_start, symtab->mod_name, symtab->sym_name,
  146. symtab->sym_name);
  147. out:
  148. debug_kfree(knt1);
  149. return ret;
  150. }
  151. void kdbnearsym_cleanup(void)
  152. {
  153. int i;
  154. for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
  155. if (kdb_name_table[i]) {
  156. debug_kfree(kdb_name_table[i]);
  157. kdb_name_table[i] = NULL;
  158. }
  159. }
  160. }
  161. static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];
  162. /*
  163. * kallsyms_symbol_complete
  164. *
  165. * Parameters:
  166. * prefix_name prefix of a symbol name to lookup
  167. * max_len maximum length that can be returned
  168. * Returns:
  169. * Number of symbols which match the given prefix.
  170. * Notes:
  171. * prefix_name is changed to contain the longest unique prefix that
  172. * starts with this prefix (tab completion).
  173. */
  174. int kallsyms_symbol_complete(char *prefix_name, int max_len)
  175. {
  176. loff_t pos = 0;
  177. int prefix_len = strlen(prefix_name), prev_len = 0;
  178. int i, number = 0;
  179. const char *name;
  180. while ((name = kdb_walk_kallsyms(&pos))) {
  181. if (strncmp(name, prefix_name, prefix_len) == 0) {
  182. strcpy(ks_namebuf, name);
  183. /* Work out the longest name that matches the prefix */
  184. if (++number == 1) {
  185. prev_len = min_t(int, max_len-1,
  186. strlen(ks_namebuf));
  187. memcpy(ks_namebuf_prev, ks_namebuf, prev_len);
  188. ks_namebuf_prev[prev_len] = '\0';
  189. continue;
  190. }
  191. for (i = 0; i < prev_len; i++) {
  192. if (ks_namebuf[i] != ks_namebuf_prev[i]) {
  193. prev_len = i;
  194. ks_namebuf_prev[i] = '\0';
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. if (prev_len > prefix_len)
  201. memcpy(prefix_name, ks_namebuf_prev, prev_len+1);
  202. return number;
  203. }
  204. /*
  205. * kallsyms_symbol_next
  206. *
  207. * Parameters:
  208. * prefix_name prefix of a symbol name to lookup
  209. * flag 0 means search from the head, 1 means continue search.
  210. * buf_size maximum length that can be written to prefix_name
  211. * buffer
  212. * Returns:
  213. * 1 if a symbol matches the given prefix.
  214. * 0 if no string found
  215. */
  216. int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)
  217. {
  218. int prefix_len = strlen(prefix_name);
  219. static loff_t pos;
  220. const char *name;
  221. if (!flag)
  222. pos = 0;
  223. while ((name = kdb_walk_kallsyms(&pos))) {
  224. if (!strncmp(name, prefix_name, prefix_len))
  225. return strscpy(prefix_name, name, buf_size);
  226. }
  227. return 0;
  228. }
  229. /*
  230. * kdb_symbol_print - Standard method for printing a symbol name and offset.
  231. * Inputs:
  232. * addr Address to be printed.
  233. * symtab Address of symbol data, if NULL this routine does its
  234. * own lookup.
  235. * punc Punctuation for string, bit field.
  236. * Remarks:
  237. * The string and its punctuation is only printed if the address
  238. * is inside the kernel, except that the value is always printed
  239. * when requested.
  240. */
  241. void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,
  242. unsigned int punc)
  243. {
  244. kdb_symtab_t symtab, *symtab_p2;
  245. if (symtab_p) {
  246. symtab_p2 = (kdb_symtab_t *)symtab_p;
  247. } else {
  248. symtab_p2 = &symtab;
  249. kdbnearsym(addr, symtab_p2);
  250. }
  251. if (!(symtab_p2->sym_name || (punc & KDB_SP_VALUE)))
  252. return;
  253. if (punc & KDB_SP_SPACEB)
  254. kdb_printf(" ");
  255. if (punc & KDB_SP_VALUE)
  256. kdb_printf(kdb_machreg_fmt0, addr);
  257. if (symtab_p2->sym_name) {
  258. if (punc & KDB_SP_VALUE)
  259. kdb_printf(" ");
  260. if (punc & KDB_SP_PAREN)
  261. kdb_printf("(");
  262. if (strcmp(symtab_p2->mod_name, "kernel"))
  263. kdb_printf("[%s]", symtab_p2->mod_name);
  264. kdb_printf("%s", symtab_p2->sym_name);
  265. if (addr != symtab_p2->sym_start)
  266. kdb_printf("+0x%lx", addr - symtab_p2->sym_start);
  267. if (punc & KDB_SP_SYMSIZE)
  268. kdb_printf("/0x%lx",
  269. symtab_p2->sym_end - symtab_p2->sym_start);
  270. if (punc & KDB_SP_PAREN)
  271. kdb_printf(")");
  272. }
  273. if (punc & KDB_SP_SPACEA)
  274. kdb_printf(" ");
  275. if (punc & KDB_SP_NEWLINE)
  276. kdb_printf("\n");
  277. }
  278. /*
  279. * kdb_strdup - kdb equivalent of strdup, for disasm code.
  280. * Inputs:
  281. * str The string to duplicate.
  282. * type Flags to kmalloc for the new string.
  283. * Returns:
  284. * Address of the new string, NULL if storage could not be allocated.
  285. * Remarks:
  286. * This is not in lib/string.c because it uses kmalloc which is not
  287. * available when string.o is used in boot loaders.
  288. */
  289. char *kdb_strdup(const char *str, gfp_t type)
  290. {
  291. int n = strlen(str)+1;
  292. char *s = kmalloc(n, type);
  293. if (!s)
  294. return NULL;
  295. return strcpy(s, str);
  296. }
  297. /*
  298. * kdb_getarea_size - Read an area of data. The kdb equivalent of
  299. * copy_from_user, with kdb messages for invalid addresses.
  300. * Inputs:
  301. * res Pointer to the area to receive the result.
  302. * addr Address of the area to copy.
  303. * size Size of the area.
  304. * Returns:
  305. * 0 for success, < 0 for error.
  306. */
  307. int kdb_getarea_size(void *res, unsigned long addr, size_t size)
  308. {
  309. int ret = probe_kernel_read((char *)res, (char *)addr, size);
  310. if (ret) {
  311. if (!KDB_STATE(SUPPRESS)) {
  312. kdb_printf("kdb_getarea: Bad address 0x%lx\n", addr);
  313. KDB_STATE_SET(SUPPRESS);
  314. }
  315. ret = KDB_BADADDR;
  316. } else {
  317. KDB_STATE_CLEAR(SUPPRESS);
  318. }
  319. return ret;
  320. }
  321. /*
  322. * kdb_putarea_size - Write an area of data. The kdb equivalent of
  323. * copy_to_user, with kdb messages for invalid addresses.
  324. * Inputs:
  325. * addr Address of the area to write to.
  326. * res Pointer to the area holding the data.
  327. * size Size of the area.
  328. * Returns:
  329. * 0 for success, < 0 for error.
  330. */
  331. int kdb_putarea_size(unsigned long addr, void *res, size_t size)
  332. {
  333. int ret = probe_kernel_read((char *)addr, (char *)res, size);
  334. if (ret) {
  335. if (!KDB_STATE(SUPPRESS)) {
  336. kdb_printf("kdb_putarea: Bad address 0x%lx\n", addr);
  337. KDB_STATE_SET(SUPPRESS);
  338. }
  339. ret = KDB_BADADDR;
  340. } else {
  341. KDB_STATE_CLEAR(SUPPRESS);
  342. }
  343. return ret;
  344. }
  345. /*
  346. * kdb_getphys - Read data from a physical address. Validate the
  347. * address is in range, use kmap_atomic() to get data
  348. * similar to kdb_getarea() - but for phys addresses
  349. * Inputs:
  350. * res Pointer to the word to receive the result
  351. * addr Physical address of the area to copy
  352. * size Size of the area
  353. * Returns:
  354. * 0 for success, < 0 for error.
  355. */
  356. static int kdb_getphys(void *res, unsigned long addr, size_t size)
  357. {
  358. unsigned long pfn;
  359. void *vaddr;
  360. struct page *page;
  361. pfn = (addr >> PAGE_SHIFT);
  362. if (!pfn_valid(pfn))
  363. return 1;
  364. page = pfn_to_page(pfn);
  365. vaddr = kmap_atomic(page);
  366. memcpy(res, vaddr + (addr & (PAGE_SIZE - 1)), size);
  367. kunmap_atomic(vaddr);
  368. return 0;
  369. }
  370. /*
  371. * kdb_getphysword
  372. * Inputs:
  373. * word Pointer to the word to receive the result.
  374. * addr Address of the area to copy.
  375. * size Size of the area.
  376. * Returns:
  377. * 0 for success, < 0 for error.
  378. */
  379. int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size)
  380. {
  381. int diag;
  382. __u8 w1;
  383. __u16 w2;
  384. __u32 w4;
  385. __u64 w8;
  386. *word = 0; /* Default value if addr or size is invalid */
  387. switch (size) {
  388. case 1:
  389. diag = kdb_getphys(&w1, addr, sizeof(w1));
  390. if (!diag)
  391. *word = w1;
  392. break;
  393. case 2:
  394. diag = kdb_getphys(&w2, addr, sizeof(w2));
  395. if (!diag)
  396. *word = w2;
  397. break;
  398. case 4:
  399. diag = kdb_getphys(&w4, addr, sizeof(w4));
  400. if (!diag)
  401. *word = w4;
  402. break;
  403. case 8:
  404. if (size <= sizeof(*word)) {
  405. diag = kdb_getphys(&w8, addr, sizeof(w8));
  406. if (!diag)
  407. *word = w8;
  408. break;
  409. }
  410. /* drop through */
  411. default:
  412. diag = KDB_BADWIDTH;
  413. kdb_printf("kdb_getphysword: bad width %ld\n", (long) size);
  414. }
  415. return diag;
  416. }
  417. /*
  418. * kdb_getword - Read a binary value. Unlike kdb_getarea, this treats
  419. * data as numbers.
  420. * Inputs:
  421. * word Pointer to the word to receive the result.
  422. * addr Address of the area to copy.
  423. * size Size of the area.
  424. * Returns:
  425. * 0 for success, < 0 for error.
  426. */
  427. int kdb_getword(unsigned long *word, unsigned long addr, size_t size)
  428. {
  429. int diag;
  430. __u8 w1;
  431. __u16 w2;
  432. __u32 w4;
  433. __u64 w8;
  434. *word = 0; /* Default value if addr or size is invalid */
  435. switch (size) {
  436. case 1:
  437. diag = kdb_getarea(w1, addr);
  438. if (!diag)
  439. *word = w1;
  440. break;
  441. case 2:
  442. diag = kdb_getarea(w2, addr);
  443. if (!diag)
  444. *word = w2;
  445. break;
  446. case 4:
  447. diag = kdb_getarea(w4, addr);
  448. if (!diag)
  449. *word = w4;
  450. break;
  451. case 8:
  452. if (size <= sizeof(*word)) {
  453. diag = kdb_getarea(w8, addr);
  454. if (!diag)
  455. *word = w8;
  456. break;
  457. }
  458. /* drop through */
  459. default:
  460. diag = KDB_BADWIDTH;
  461. kdb_printf("kdb_getword: bad width %ld\n", (long) size);
  462. }
  463. return diag;
  464. }
  465. /*
  466. * kdb_putword - Write a binary value. Unlike kdb_putarea, this
  467. * treats data as numbers.
  468. * Inputs:
  469. * addr Address of the area to write to..
  470. * word The value to set.
  471. * size Size of the area.
  472. * Returns:
  473. * 0 for success, < 0 for error.
  474. */
  475. int kdb_putword(unsigned long addr, unsigned long word, size_t size)
  476. {
  477. int diag;
  478. __u8 w1;
  479. __u16 w2;
  480. __u32 w4;
  481. __u64 w8;
  482. switch (size) {
  483. case 1:
  484. w1 = word;
  485. diag = kdb_putarea(addr, w1);
  486. break;
  487. case 2:
  488. w2 = word;
  489. diag = kdb_putarea(addr, w2);
  490. break;
  491. case 4:
  492. w4 = word;
  493. diag = kdb_putarea(addr, w4);
  494. break;
  495. case 8:
  496. if (size <= sizeof(word)) {
  497. w8 = word;
  498. diag = kdb_putarea(addr, w8);
  499. break;
  500. }
  501. /* drop through */
  502. default:
  503. diag = KDB_BADWIDTH;
  504. kdb_printf("kdb_putword: bad width %ld\n", (long) size);
  505. }
  506. return diag;
  507. }
  508. /*
  509. * kdb_task_state_string - Convert a string containing any of the
  510. * letters DRSTCZEUIMA to a mask for the process state field and
  511. * return the value. If no argument is supplied, return the mask
  512. * that corresponds to environment variable PS, DRSTCZEU by
  513. * default.
  514. * Inputs:
  515. * s String to convert
  516. * Returns:
  517. * Mask for process state.
  518. * Notes:
  519. * The mask folds data from several sources into a single long value, so
  520. * be careful not to overlap the bits. TASK_* bits are in the LSB,
  521. * special cases like UNRUNNABLE are in the MSB. As of 2.6.10-rc1 there
  522. * is no overlap between TASK_* and EXIT_* but that may not always be
  523. * true, so EXIT_* bits are shifted left 16 bits before being stored in
  524. * the mask.
  525. */
  526. /* unrunnable is < 0 */
  527. #define UNRUNNABLE (1UL << (8*sizeof(unsigned long) - 1))
  528. #define RUNNING (1UL << (8*sizeof(unsigned long) - 2))
  529. #define IDLE (1UL << (8*sizeof(unsigned long) - 3))
  530. #define DAEMON (1UL << (8*sizeof(unsigned long) - 4))
  531. unsigned long kdb_task_state_string(const char *s)
  532. {
  533. long res = 0;
  534. if (!s) {
  535. s = kdbgetenv("PS");
  536. if (!s)
  537. s = "DRSTCZEU"; /* default value for ps */
  538. }
  539. while (*s) {
  540. switch (*s) {
  541. case 'D':
  542. res |= TASK_UNINTERRUPTIBLE;
  543. break;
  544. case 'R':
  545. res |= RUNNING;
  546. break;
  547. case 'S':
  548. res |= TASK_INTERRUPTIBLE;
  549. break;
  550. case 'T':
  551. res |= TASK_STOPPED;
  552. break;
  553. case 'C':
  554. res |= TASK_TRACED;
  555. break;
  556. case 'Z':
  557. res |= EXIT_ZOMBIE << 16;
  558. break;
  559. case 'E':
  560. res |= EXIT_DEAD << 16;
  561. break;
  562. case 'U':
  563. res |= UNRUNNABLE;
  564. break;
  565. case 'I':
  566. res |= IDLE;
  567. break;
  568. case 'M':
  569. res |= DAEMON;
  570. break;
  571. case 'A':
  572. res = ~0UL;
  573. break;
  574. default:
  575. kdb_printf("%s: unknown flag '%c' ignored\n",
  576. __func__, *s);
  577. break;
  578. }
  579. ++s;
  580. }
  581. return res;
  582. }
  583. /*
  584. * kdb_task_state_char - Return the character that represents the task state.
  585. * Inputs:
  586. * p struct task for the process
  587. * Returns:
  588. * One character to represent the task state.
  589. */
  590. char kdb_task_state_char (const struct task_struct *p)
  591. {
  592. int cpu;
  593. char state;
  594. unsigned long tmp;
  595. if (!p || probe_kernel_read(&tmp, (char *)p, sizeof(unsigned long)))
  596. return 'E';
  597. cpu = kdb_process_cpu(p);
  598. state = (p->state == 0) ? 'R' :
  599. (p->state < 0) ? 'U' :
  600. (p->state & TASK_UNINTERRUPTIBLE) ? 'D' :
  601. (p->state & TASK_STOPPED) ? 'T' :
  602. (p->state & TASK_TRACED) ? 'C' :
  603. (p->exit_state & EXIT_ZOMBIE) ? 'Z' :
  604. (p->exit_state & EXIT_DEAD) ? 'E' :
  605. (p->state & TASK_INTERRUPTIBLE) ? 'S' : '?';
  606. if (is_idle_task(p)) {
  607. /* Idle task. Is it really idle, apart from the kdb
  608. * interrupt? */
  609. if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) {
  610. if (cpu != kdb_initial_cpu)
  611. state = 'I'; /* idle task */
  612. }
  613. } else if (!p->mm && state == 'S') {
  614. state = 'M'; /* sleeping system daemon */
  615. }
  616. return state;
  617. }
  618. /*
  619. * kdb_task_state - Return true if a process has the desired state
  620. * given by the mask.
  621. * Inputs:
  622. * p struct task for the process
  623. * mask mask from kdb_task_state_string to select processes
  624. * Returns:
  625. * True if the process matches at least one criteria defined by the mask.
  626. */
  627. unsigned long kdb_task_state(const struct task_struct *p, unsigned long mask)
  628. {
  629. char state[] = { kdb_task_state_char(p), '\0' };
  630. return (mask & kdb_task_state_string(state)) != 0;
  631. }
  632. /*
  633. * kdb_print_nameval - Print a name and its value, converting the
  634. * value to a symbol lookup if possible.
  635. * Inputs:
  636. * name field name to print
  637. * val value of field
  638. */
  639. void kdb_print_nameval(const char *name, unsigned long val)
  640. {
  641. kdb_symtab_t symtab;
  642. kdb_printf(" %-11.11s ", name);
  643. if (kdbnearsym(val, &symtab))
  644. kdb_symbol_print(val, &symtab,
  645. KDB_SP_VALUE|KDB_SP_SYMSIZE|KDB_SP_NEWLINE);
  646. else
  647. kdb_printf("0x%lx\n", val);
  648. }
  649. /* Last ditch allocator for debugging, so we can still debug even when
  650. * the GFP_ATOMIC pool has been exhausted. The algorithms are tuned
  651. * for space usage, not for speed. One smallish memory pool, the free
  652. * chain is always in ascending address order to allow coalescing,
  653. * allocations are done in brute force best fit.
  654. */
  655. struct debug_alloc_header {
  656. u32 next; /* offset of next header from start of pool */
  657. u32 size;
  658. void *caller;
  659. };
  660. /* The memory returned by this allocator must be aligned, which means
  661. * so must the header size. Do not assume that sizeof(struct
  662. * debug_alloc_header) is a multiple of the alignment, explicitly
  663. * calculate the overhead of this header, including the alignment.
  664. * The rest of this code must not use sizeof() on any header or
  665. * pointer to a header.
  666. */
  667. #define dah_align 8
  668. #define dah_overhead ALIGN(sizeof(struct debug_alloc_header), dah_align)
  669. static u64 debug_alloc_pool_aligned[256*1024/dah_align]; /* 256K pool */
  670. static char *debug_alloc_pool = (char *)debug_alloc_pool_aligned;
  671. static u32 dah_first, dah_first_call = 1, dah_used, dah_used_max;
  672. /* Locking is awkward. The debug code is called from all contexts,
  673. * including non maskable interrupts. A normal spinlock is not safe
  674. * in NMI context. Try to get the debug allocator lock, if it cannot
  675. * be obtained after a second then give up. If the lock could not be
  676. * previously obtained on this cpu then only try once.
  677. *
  678. * sparse has no annotation for "this function _sometimes_ acquires a
  679. * lock", so fudge the acquire/release notation.
  680. */
  681. static DEFINE_SPINLOCK(dap_lock);
  682. static int get_dap_lock(void)
  683. __acquires(dap_lock)
  684. {
  685. static int dap_locked = -1;
  686. int count;
  687. if (dap_locked == smp_processor_id())
  688. count = 1;
  689. else
  690. count = 1000;
  691. while (1) {
  692. if (spin_trylock(&dap_lock)) {
  693. dap_locked = -1;
  694. return 1;
  695. }
  696. if (!count--)
  697. break;
  698. udelay(1000);
  699. }
  700. dap_locked = smp_processor_id();
  701. __acquire(dap_lock);
  702. return 0;
  703. }
  704. void *debug_kmalloc(size_t size, gfp_t flags)
  705. {
  706. unsigned int rem, h_offset;
  707. struct debug_alloc_header *best, *bestprev, *prev, *h;
  708. void *p = NULL;
  709. if (!get_dap_lock()) {
  710. __release(dap_lock); /* we never actually got it */
  711. return NULL;
  712. }
  713. h = (struct debug_alloc_header *)(debug_alloc_pool + dah_first);
  714. if (dah_first_call) {
  715. h->size = sizeof(debug_alloc_pool_aligned) - dah_overhead;
  716. dah_first_call = 0;
  717. }
  718. size = ALIGN(size, dah_align);
  719. prev = best = bestprev = NULL;
  720. while (1) {
  721. if (h->size >= size && (!best || h->size < best->size)) {
  722. best = h;
  723. bestprev = prev;
  724. if (h->size == size)
  725. break;
  726. }
  727. if (!h->next)
  728. break;
  729. prev = h;
  730. h = (struct debug_alloc_header *)(debug_alloc_pool + h->next);
  731. }
  732. if (!best)
  733. goto out;
  734. rem = best->size - size;
  735. /* The pool must always contain at least one header */
  736. if (best->next == 0 && bestprev == NULL && rem < dah_overhead)
  737. goto out;
  738. if (rem >= dah_overhead) {
  739. best->size = size;
  740. h_offset = ((char *)best - debug_alloc_pool) +
  741. dah_overhead + best->size;
  742. h = (struct debug_alloc_header *)(debug_alloc_pool + h_offset);
  743. h->size = rem - dah_overhead;
  744. h->next = best->next;
  745. } else
  746. h_offset = best->next;
  747. best->caller = __builtin_return_address(0);
  748. dah_used += best->size;
  749. dah_used_max = max(dah_used, dah_used_max);
  750. if (bestprev)
  751. bestprev->next = h_offset;
  752. else
  753. dah_first = h_offset;
  754. p = (char *)best + dah_overhead;
  755. memset(p, POISON_INUSE, best->size - 1);
  756. *((char *)p + best->size - 1) = POISON_END;
  757. out:
  758. spin_unlock(&dap_lock);
  759. return p;
  760. }
  761. void debug_kfree(void *p)
  762. {
  763. struct debug_alloc_header *h;
  764. unsigned int h_offset;
  765. if (!p)
  766. return;
  767. if ((char *)p < debug_alloc_pool ||
  768. (char *)p >= debug_alloc_pool + sizeof(debug_alloc_pool_aligned)) {
  769. kfree(p);
  770. return;
  771. }
  772. if (!get_dap_lock()) {
  773. __release(dap_lock); /* we never actually got it */
  774. return; /* memory leak, cannot be helped */
  775. }
  776. h = (struct debug_alloc_header *)((char *)p - dah_overhead);
  777. memset(p, POISON_FREE, h->size - 1);
  778. *((char *)p + h->size - 1) = POISON_END;
  779. h->caller = NULL;
  780. dah_used -= h->size;
  781. h_offset = (char *)h - debug_alloc_pool;
  782. if (h_offset < dah_first) {
  783. h->next = dah_first;
  784. dah_first = h_offset;
  785. } else {
  786. struct debug_alloc_header *prev;
  787. unsigned int prev_offset;
  788. prev = (struct debug_alloc_header *)(debug_alloc_pool +
  789. dah_first);
  790. while (1) {
  791. if (!prev->next || prev->next > h_offset)
  792. break;
  793. prev = (struct debug_alloc_header *)
  794. (debug_alloc_pool + prev->next);
  795. }
  796. prev_offset = (char *)prev - debug_alloc_pool;
  797. if (prev_offset + dah_overhead + prev->size == h_offset) {
  798. prev->size += dah_overhead + h->size;
  799. memset(h, POISON_FREE, dah_overhead - 1);
  800. *((char *)h + dah_overhead - 1) = POISON_END;
  801. h = prev;
  802. h_offset = prev_offset;
  803. } else {
  804. h->next = prev->next;
  805. prev->next = h_offset;
  806. }
  807. }
  808. if (h_offset + dah_overhead + h->size == h->next) {
  809. struct debug_alloc_header *next;
  810. next = (struct debug_alloc_header *)
  811. (debug_alloc_pool + h->next);
  812. h->size += dah_overhead + next->size;
  813. h->next = next->next;
  814. memset(next, POISON_FREE, dah_overhead - 1);
  815. *((char *)next + dah_overhead - 1) = POISON_END;
  816. }
  817. spin_unlock(&dap_lock);
  818. }
  819. void debug_kusage(void)
  820. {
  821. struct debug_alloc_header *h_free, *h_used;
  822. #ifdef CONFIG_IA64
  823. /* FIXME: using dah for ia64 unwind always results in a memory leak.
  824. * Fix that memory leak first, then set debug_kusage_one_time = 1 for
  825. * all architectures.
  826. */
  827. static int debug_kusage_one_time;
  828. #else
  829. static int debug_kusage_one_time = 1;
  830. #endif
  831. if (!get_dap_lock()) {
  832. __release(dap_lock); /* we never actually got it */
  833. return;
  834. }
  835. h_free = (struct debug_alloc_header *)(debug_alloc_pool + dah_first);
  836. if (dah_first == 0 &&
  837. (h_free->size == sizeof(debug_alloc_pool_aligned) - dah_overhead ||
  838. dah_first_call))
  839. goto out;
  840. if (!debug_kusage_one_time)
  841. goto out;
  842. debug_kusage_one_time = 0;
  843. kdb_printf("%s: debug_kmalloc memory leak dah_first %d\n",
  844. __func__, dah_first);
  845. if (dah_first) {
  846. h_used = (struct debug_alloc_header *)debug_alloc_pool;
  847. kdb_printf("%s: h_used %p size %d\n", __func__, h_used,
  848. h_used->size);
  849. }
  850. do {
  851. h_used = (struct debug_alloc_header *)
  852. ((char *)h_free + dah_overhead + h_free->size);
  853. kdb_printf("%s: h_used %p size %d caller %p\n",
  854. __func__, h_used, h_used->size, h_used->caller);
  855. h_free = (struct debug_alloc_header *)
  856. (debug_alloc_pool + h_free->next);
  857. } while (h_free->next);
  858. h_used = (struct debug_alloc_header *)
  859. ((char *)h_free + dah_overhead + h_free->size);
  860. if ((char *)h_used - debug_alloc_pool !=
  861. sizeof(debug_alloc_pool_aligned))
  862. kdb_printf("%s: h_used %p size %d caller %p\n",
  863. __func__, h_used, h_used->size, h_used->caller);
  864. out:
  865. spin_unlock(&dap_lock);
  866. }
  867. /* Maintain a small stack of kdb_flags to allow recursion without disturbing
  868. * the global kdb state.
  869. */
  870. static int kdb_flags_stack[4], kdb_flags_index;
  871. void kdb_save_flags(void)
  872. {
  873. BUG_ON(kdb_flags_index >= ARRAY_SIZE(kdb_flags_stack));
  874. kdb_flags_stack[kdb_flags_index++] = kdb_flags;
  875. }
  876. void kdb_restore_flags(void)
  877. {
  878. BUG_ON(kdb_flags_index <= 0);
  879. kdb_flags = kdb_flags_stack[--kdb_flags_index];
  880. }