/*1* Kernel Debugger Architecture Independent Support Functions2*3* This file is subject to the terms and conditions of the GNU General Public4* License. See the file "COPYING" in the main directory of this archive5* for more details.6*7* Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.8* Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.9* 03/02/13 added new 2.5 kallsyms <[email protected]>10*/1112#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"2728/*29* kdbgetsymval - Return the address of the given symbol.30*31* Parameters:32* symname Character string containing symbol name33* symtab Structure to receive results34* Returns:35* 0 Symbol not found, symtab zero filled36* 1 Symbol mapped to module/symbol/section, data in symtab37*/38int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)39{40kdb_dbg_printf(AR, "symname=%s, symtab=%px\n", symname, symtab);41memset(symtab, 0, sizeof(*symtab));42symtab->sym_start = kallsyms_lookup_name(symname);43if (symtab->sym_start) {44kdb_dbg_printf(AR, "returns 1, symtab->sym_start=0x%lx\n",45symtab->sym_start);46return 1;47}48kdb_dbg_printf(AR, "returns 0\n");49return 0;50}51EXPORT_SYMBOL(kdbgetsymval);5253/**54* kdbnearsym() - Return the name of the symbol with the nearest address55* less than @addr.56* @addr: Address to check for near symbol57* @symtab: Structure to receive results58*59* WARNING: This function may return a pointer to a single statically60* allocated buffer (namebuf). kdb's unusual calling context (single61* threaded, all other CPUs halted) provides us sufficient locking for62* this to be safe. The only constraint imposed by the static buffer is63* that the caller must consume any previous reply prior to another call64* to lookup a new symbol.65*66* Note that, strictly speaking, some architectures may re-enter the kdb67* trap if the system turns out to be very badly damaged and this breaks68* the single-threaded assumption above. In these circumstances successful69* continuation and exit from the inner trap is unlikely to work and any70* user attempting this receives a prominent warning before being allowed71* to progress. In these circumstances we remain memory safe because72* namebuf[KSYM_NAME_LEN-1] will never change from '\0' although we do73* tolerate the possibility of garbled symbol display from the outer kdb74* trap.75*76* Return:77* * 0 - No sections contain this address, symtab zero filled78* * 1 - Address mapped to module/symbol/section, data in symtab79*/80int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)81{82int ret = 0;83unsigned long symbolsize = 0;84unsigned long offset = 0;85static char namebuf[KSYM_NAME_LEN];8687kdb_dbg_printf(AR, "addr=0x%lx, symtab=%px\n", addr, symtab);88memset(symtab, 0, sizeof(*symtab));8990if (addr < 4096)91goto out;9293symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset,94(char **)(&symtab->mod_name), namebuf);95if (offset > 8*1024*1024) {96symtab->sym_name = NULL;97addr = offset = symbolsize = 0;98}99symtab->sym_start = addr - offset;100symtab->sym_end = symtab->sym_start + symbolsize;101ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';102103if (symtab->mod_name == NULL)104symtab->mod_name = "kernel";105kdb_dbg_printf(AR, "returns %d symtab->sym_start=0x%lx, symtab->mod_name=%px, symtab->sym_name=%px (%s)\n",106ret, symtab->sym_start, symtab->mod_name, symtab->sym_name, symtab->sym_name);107out:108return ret;109}110111static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];112113/*114* kallsyms_symbol_complete115*116* Parameters:117* prefix_name prefix of a symbol name to lookup118* max_len maximum length that can be returned119* Returns:120* Number of symbols which match the given prefix.121* Notes:122* prefix_name is changed to contain the longest unique prefix that123* starts with this prefix (tab completion).124*/125int kallsyms_symbol_complete(char *prefix_name, int max_len)126{127loff_t pos = 0;128int prefix_len = strlen(prefix_name), prev_len = 0;129int i, number = 0;130const char *name;131132while ((name = kdb_walk_kallsyms(&pos))) {133if (strncmp(name, prefix_name, prefix_len) == 0) {134strscpy(ks_namebuf, name, sizeof(ks_namebuf));135/* Work out the longest name that matches the prefix */136if (++number == 1) {137prev_len = min_t(int, max_len-1,138strlen(ks_namebuf));139memcpy(ks_namebuf_prev, ks_namebuf, prev_len);140ks_namebuf_prev[prev_len] = '\0';141continue;142}143for (i = 0; i < prev_len; i++) {144if (ks_namebuf[i] != ks_namebuf_prev[i]) {145prev_len = i;146ks_namebuf_prev[i] = '\0';147break;148}149}150}151}152if (prev_len > prefix_len)153memcpy(prefix_name, ks_namebuf_prev, prev_len+1);154return number;155}156157/*158* kallsyms_symbol_next159*160* Parameters:161* prefix_name prefix of a symbol name to lookup162* flag 0 means search from the head, 1 means continue search.163* buf_size maximum length that can be written to prefix_name164* buffer165* Returns:166* 1 if a symbol matches the given prefix.167* 0 if no string found168*/169int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)170{171int prefix_len = strlen(prefix_name);172static loff_t pos;173const char *name;174175if (!flag)176pos = 0;177178while ((name = kdb_walk_kallsyms(&pos))) {179if (!strncmp(name, prefix_name, prefix_len))180return strscpy(prefix_name, name, buf_size);181}182return 0;183}184185/*186* kdb_symbol_print - Standard method for printing a symbol name and offset.187* Inputs:188* addr Address to be printed.189* symtab Address of symbol data, if NULL this routine does its190* own lookup.191* punc Punctuation for string, bit field.192* Remarks:193* The string and its punctuation is only printed if the address194* is inside the kernel, except that the value is always printed195* when requested.196*/197void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,198unsigned int punc)199{200kdb_symtab_t symtab, *symtab_p2;201if (symtab_p) {202symtab_p2 = (kdb_symtab_t *)symtab_p;203} else {204symtab_p2 = &symtab;205kdbnearsym(addr, symtab_p2);206}207if (!(symtab_p2->sym_name || (punc & KDB_SP_VALUE)))208return;209if (punc & KDB_SP_SPACEB)210kdb_printf(" ");211if (punc & KDB_SP_VALUE)212kdb_printf(kdb_machreg_fmt0, addr);213if (symtab_p2->sym_name) {214if (punc & KDB_SP_VALUE)215kdb_printf(" ");216if (punc & KDB_SP_PAREN)217kdb_printf("(");218if (strcmp(symtab_p2->mod_name, "kernel"))219kdb_printf("[%s]", symtab_p2->mod_name);220kdb_printf("%s", symtab_p2->sym_name);221if (addr != symtab_p2->sym_start)222kdb_printf("+0x%lx", addr - symtab_p2->sym_start);223if (punc & KDB_SP_SYMSIZE)224kdb_printf("/0x%lx",225symtab_p2->sym_end - symtab_p2->sym_start);226if (punc & KDB_SP_PAREN)227kdb_printf(")");228}229if (punc & KDB_SP_SPACEA)230kdb_printf(" ");231if (punc & KDB_SP_NEWLINE)232kdb_printf("\n");233}234235/*236* kdb_strdup - kdb equivalent of strdup, for disasm code.237* Inputs:238* str The string to duplicate.239* type Flags to kmalloc for the new string.240* Returns:241* Address of the new string, NULL if storage could not be allocated.242* Remarks:243* This is not in lib/string.c because it uses kmalloc which is not244* available when string.o is used in boot loaders.245*/246char *kdb_strdup(const char *str, gfp_t type)247{248int n = strlen(str)+1;249char *s = kmalloc(n, type);250if (!s)251return NULL;252return strcpy(s, str);253}254255/*256* kdb_getarea_size - Read an area of data. The kdb equivalent of257* copy_from_user, with kdb messages for invalid addresses.258* Inputs:259* res Pointer to the area to receive the result.260* addr Address of the area to copy.261* size Size of the area.262* Returns:263* 0 for success, < 0 for error.264*/265int kdb_getarea_size(void *res, unsigned long addr, size_t size)266{267int ret = copy_from_kernel_nofault((char *)res, (char *)addr, size);268if (ret) {269if (!KDB_STATE(SUPPRESS)) {270kdb_func_printf("Bad address 0x%lx\n", addr);271KDB_STATE_SET(SUPPRESS);272}273ret = KDB_BADADDR;274} else {275KDB_STATE_CLEAR(SUPPRESS);276}277return ret;278}279280/*281* kdb_putarea_size - Write an area of data. The kdb equivalent of282* copy_to_user, with kdb messages for invalid addresses.283* Inputs:284* addr Address of the area to write to.285* res Pointer to the area holding the data.286* size Size of the area.287* Returns:288* 0 for success, < 0 for error.289*/290int kdb_putarea_size(unsigned long addr, void *res, size_t size)291{292int ret = copy_to_kernel_nofault((char *)addr, (char *)res, size);293if (ret) {294if (!KDB_STATE(SUPPRESS)) {295kdb_func_printf("Bad address 0x%lx\n", addr);296KDB_STATE_SET(SUPPRESS);297}298ret = KDB_BADADDR;299} else {300KDB_STATE_CLEAR(SUPPRESS);301}302return ret;303}304305/*306* kdb_getphys - Read data from a physical address. Validate the307* address is in range, use kmap_local_page() to get data308* similar to kdb_getarea() - but for phys addresses309* Inputs:310* res Pointer to the word to receive the result311* addr Physical address of the area to copy312* size Size of the area313* Returns:314* 0 for success, < 0 for error.315*/316static int kdb_getphys(void *res, unsigned long addr, size_t size)317{318unsigned long pfn;319void *vaddr;320struct page *page;321322pfn = (addr >> PAGE_SHIFT);323if (!pfn_valid(pfn))324return 1;325page = pfn_to_page(pfn);326vaddr = kmap_local_page(page);327memcpy(res, vaddr + (addr & (PAGE_SIZE - 1)), size);328kunmap_local(vaddr);329330return 0;331}332333/*334* kdb_getphysword335* Inputs:336* word Pointer to the word to receive the result.337* addr Address of the area to copy.338* size Size of the area.339* Returns:340* 0 for success, < 0 for error.341*/342int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size)343{344int diag;345__u8 w1;346__u16 w2;347__u32 w4;348__u64 w8;349*word = 0; /* Default value if addr or size is invalid */350351switch (size) {352case 1:353diag = kdb_getphys(&w1, addr, sizeof(w1));354if (!diag)355*word = w1;356break;357case 2:358diag = kdb_getphys(&w2, addr, sizeof(w2));359if (!diag)360*word = w2;361break;362case 4:363diag = kdb_getphys(&w4, addr, sizeof(w4));364if (!diag)365*word = w4;366break;367case 8:368if (size <= sizeof(*word)) {369diag = kdb_getphys(&w8, addr, sizeof(w8));370if (!diag)371*word = w8;372break;373}374fallthrough;375default:376diag = KDB_BADWIDTH;377kdb_func_printf("bad width %zu\n", size);378}379return diag;380}381382/*383* kdb_getword - Read a binary value. Unlike kdb_getarea, this treats384* data as numbers.385* Inputs:386* word Pointer to the word to receive the result.387* addr Address of the area to copy.388* size Size of the area.389* Returns:390* 0 for success, < 0 for error.391*/392int kdb_getword(unsigned long *word, unsigned long addr, size_t size)393{394int diag;395__u8 w1;396__u16 w2;397__u32 w4;398__u64 w8;399*word = 0; /* Default value if addr or size is invalid */400switch (size) {401case 1:402diag = kdb_getarea(w1, addr);403if (!diag)404*word = w1;405break;406case 2:407diag = kdb_getarea(w2, addr);408if (!diag)409*word = w2;410break;411case 4:412diag = kdb_getarea(w4, addr);413if (!diag)414*word = w4;415break;416case 8:417if (size <= sizeof(*word)) {418diag = kdb_getarea(w8, addr);419if (!diag)420*word = w8;421break;422}423fallthrough;424default:425diag = KDB_BADWIDTH;426kdb_func_printf("bad width %zu\n", size);427}428return diag;429}430431/*432* kdb_putword - Write a binary value. Unlike kdb_putarea, this433* treats data as numbers.434* Inputs:435* addr Address of the area to write to..436* word The value to set.437* size Size of the area.438* Returns:439* 0 for success, < 0 for error.440*/441int kdb_putword(unsigned long addr, unsigned long word, size_t size)442{443int diag;444__u8 w1;445__u16 w2;446__u32 w4;447__u64 w8;448switch (size) {449case 1:450w1 = word;451diag = kdb_putarea(addr, w1);452break;453case 2:454w2 = word;455diag = kdb_putarea(addr, w2);456break;457case 4:458w4 = word;459diag = kdb_putarea(addr, w4);460break;461case 8:462if (size <= sizeof(word)) {463w8 = word;464diag = kdb_putarea(addr, w8);465break;466}467fallthrough;468default:469diag = KDB_BADWIDTH;470kdb_func_printf("bad width %zu\n", size);471}472return diag;473}474475476477/*478* kdb_task_state_char - Return the character that represents the task state.479* Inputs:480* p struct task for the process481* Returns:482* One character to represent the task state.483*/484char kdb_task_state_char (const struct task_struct *p)485{486unsigned long tmp;487char state;488int cpu;489490if (!p ||491copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long)))492return 'E';493494state = task_state_to_char((struct task_struct *) p);495496if (is_idle_task(p)) {497/* Idle task. Is it really idle, apart from the kdb498* interrupt? */499cpu = kdb_process_cpu(p);500if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) {501if (cpu != kdb_initial_cpu)502state = '-'; /* idle task */503}504} else if (!p->mm && strchr("IMS", state)) {505state = tolower(state); /* sleeping system daemon */506}507return state;508}509510/*511* kdb_task_state - Return true if a process has the desired state512* given by the mask.513* Inputs:514* p struct task for the process515* mask set of characters used to select processes; both NULL516* and the empty string mean adopt a default filter, which517* is to suppress sleeping system daemons and the idle tasks518* Returns:519* True if the process matches at least one criteria defined by the mask.520*/521bool kdb_task_state(const struct task_struct *p, const char *mask)522{523char state = kdb_task_state_char(p);524525/* If there is no mask, then we will filter code that runs when the526* scheduler is idling and any system daemons that are currently527* sleeping.528*/529if (!mask || mask[0] == '\0')530return !strchr("-ims", state);531532/* A is a special case that matches all states */533if (strchr(mask, 'A'))534return true;535536return strchr(mask, state);537}538539540