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