/*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/string.h>26#include <linux/ctype.h>27#include "kdb_private.h"2829/*30* kdbgetsymval - Return the address of the given symbol.31*32* Parameters:33* symname Character string containing symbol name34* symtab Structure to receive results35* Returns:36* 0 Symbol not found, symtab zero filled37* 1 Symbol mapped to module/symbol/section, data in symtab38*/39int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)40{41kdb_dbg_printf(AR, "symname=%s, symtab=%px\n", symname, symtab);42memset(symtab, 0, sizeof(*symtab));43symtab->sym_start = kallsyms_lookup_name(symname);44if (symtab->sym_start) {45kdb_dbg_printf(AR, "returns 1, symtab->sym_start=0x%lx\n",46symtab->sym_start);47return 1;48}49kdb_dbg_printf(AR, "returns 0\n");50return 0;51}52EXPORT_SYMBOL(kdbgetsymval);5354/**55* kdbnearsym() - Return the name of the symbol with the nearest address56* less than @addr.57* @addr: Address to check for near symbol58* @symtab: Structure to receive results59*60* WARNING: This function may return a pointer to a single statically61* allocated buffer (namebuf). kdb's unusual calling context (single62* threaded, all other CPUs halted) provides us sufficient locking for63* this to be safe. The only constraint imposed by the static buffer is64* that the caller must consume any previous reply prior to another call65* to lookup a new symbol.66*67* Note that, strictly speaking, some architectures may re-enter the kdb68* trap if the system turns out to be very badly damaged and this breaks69* the single-threaded assumption above. In these circumstances successful70* continuation and exit from the inner trap is unlikely to work and any71* user attempting this receives a prominent warning before being allowed72* to progress. In these circumstances we remain memory safe because73* namebuf[KSYM_NAME_LEN-1] will never change from '\0' although we do74* tolerate the possibility of garbled symbol display from the outer kdb75* trap.76*77* Return:78* * 0 - No sections contain this address, symtab zero filled79* * 1 - Address mapped to module/symbol/section, data in symtab80*/81int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)82{83int ret = 0;84unsigned long symbolsize = 0;85unsigned long offset = 0;86static char namebuf[KSYM_NAME_LEN];8788kdb_dbg_printf(AR, "addr=0x%lx, symtab=%px\n", addr, symtab);89memset(symtab, 0, sizeof(*symtab));9091if (addr < 4096)92goto out;9394symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset,95(char **)(&symtab->mod_name), namebuf);96if (offset > 8*1024*1024) {97symtab->sym_name = NULL;98addr = offset = symbolsize = 0;99}100symtab->sym_start = addr - offset;101symtab->sym_end = symtab->sym_start + symbolsize;102ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';103104if (symtab->mod_name == NULL)105symtab->mod_name = "kernel";106kdb_dbg_printf(AR, "returns %d symtab->sym_start=0x%lx, symtab->mod_name=%px, symtab->sym_name=%px (%s)\n",107ret, symtab->sym_start, symtab->mod_name, symtab->sym_name, symtab->sym_name);108out:109return ret;110}111112static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];113114/*115* kallsyms_symbol_complete116*117* Parameters:118* prefix_name prefix of a symbol name to lookup119* max_len maximum length that can be returned120* Returns:121* Number of symbols which match the given prefix.122* Notes:123* prefix_name is changed to contain the longest unique prefix that124* starts with this prefix (tab completion).125*/126int kallsyms_symbol_complete(char *prefix_name, int max_len)127{128loff_t pos = 0;129int prefix_len = strlen(prefix_name), prev_len = 0;130int i, number = 0;131const char *name;132133while ((name = kdb_walk_kallsyms(&pos))) {134if (strncmp(name, prefix_name, prefix_len) == 0) {135strscpy(ks_namebuf, name, sizeof(ks_namebuf));136/* Work out the longest name that matches the prefix */137if (++number == 1) {138prev_len = min_t(int, max_len-1,139strlen(ks_namebuf));140memcpy(ks_namebuf_prev, ks_namebuf, prev_len);141ks_namebuf_prev[prev_len] = '\0';142continue;143}144for (i = 0; i < prev_len; i++) {145if (ks_namebuf[i] != ks_namebuf_prev[i]) {146prev_len = i;147ks_namebuf_prev[i] = '\0';148break;149}150}151}152}153if (prev_len > prefix_len)154memcpy(prefix_name, ks_namebuf_prev, prev_len+1);155return number;156}157158/*159* kallsyms_symbol_next160*161* Parameters:162* prefix_name prefix of a symbol name to lookup163* flag 0 means search from the head, 1 means continue search.164* buf_size maximum length that can be written to prefix_name165* buffer166* Returns:167* 1 if a symbol matches the given prefix.168* 0 if no string found169*/170int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)171{172int prefix_len = strlen(prefix_name);173static loff_t pos;174const char *name;175176if (!flag)177pos = 0;178179while ((name = kdb_walk_kallsyms(&pos))) {180if (!strncmp(name, prefix_name, prefix_len))181return strscpy(prefix_name, name, buf_size);182}183return 0;184}185186/*187* kdb_symbol_print - Standard method for printing a symbol name and offset.188* Inputs:189* addr Address to be printed.190* symtab Address of symbol data, if NULL this routine does its191* own lookup.192* punc Punctuation for string, bit field.193* Remarks:194* The string and its punctuation is only printed if the address195* is inside the kernel, except that the value is always printed196* when requested.197*/198void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,199unsigned int punc)200{201kdb_symtab_t symtab, *symtab_p2;202if (symtab_p) {203symtab_p2 = (kdb_symtab_t *)symtab_p;204} else {205symtab_p2 = &symtab;206kdbnearsym(addr, symtab_p2);207}208if (!(symtab_p2->sym_name || (punc & KDB_SP_VALUE)))209return;210if (punc & KDB_SP_SPACEB)211kdb_printf(" ");212if (punc & KDB_SP_VALUE)213kdb_printf(kdb_machreg_fmt0, addr);214if (symtab_p2->sym_name) {215if (punc & KDB_SP_VALUE)216kdb_printf(" ");217if (punc & KDB_SP_PAREN)218kdb_printf("(");219if (strcmp(symtab_p2->mod_name, "kernel"))220kdb_printf("[%s]", symtab_p2->mod_name);221kdb_printf("%s", symtab_p2->sym_name);222if (addr != symtab_p2->sym_start)223kdb_printf("+0x%lx", addr - symtab_p2->sym_start);224if (punc & KDB_SP_SYMSIZE)225kdb_printf("/0x%lx",226symtab_p2->sym_end - symtab_p2->sym_start);227if (punc & KDB_SP_PAREN)228kdb_printf(")");229}230if (punc & KDB_SP_SPACEA)231kdb_printf(" ");232if (punc & KDB_SP_NEWLINE)233kdb_printf("\n");234}235236/*237* kdb_strdup - kdb equivalent of strdup, for disasm code.238* Inputs:239* str The string to duplicate.240* type Flags to kmalloc for the new string.241* Returns:242* Address of the new string, NULL if storage could not be allocated.243* Remarks:244* This is not in lib/string.c because it uses kmalloc which is not245* available when string.o is used in boot loaders.246*/247char *kdb_strdup(const char *str, gfp_t type)248{249size_t n = strlen(str) + 1;250char *s = kmalloc(n, type);251if (!s)252return NULL;253memcpy(s, str, n);254return s;255}256257/*258* kdb_strdup_dequote - same as kdb_strdup(), but trims surrounding quotes from259* the input string if present.260* Remarks:261* Quotes are only removed if there is both a leading and a trailing quote.262*/263char *kdb_strdup_dequote(const char *str, gfp_t type)264{265size_t len = strlen(str);266char *s;267268if (str[0] == '"' && len > 1 && str[len - 1] == '"') {269/* trim both leading and trailing quotes */270str++;271len -= 2;272}273274len++; /* add space for NUL terminator */275276s = kmalloc(len, type);277if (!s)278return NULL;279280memcpy(s, str, len - 1);281s[len - 1] = '\0';282283return s;284}285286/*287* kdb_getarea_size - Read an area of data. The kdb equivalent of288* copy_from_user, with kdb messages for invalid addresses.289* Inputs:290* res Pointer to the area to receive the result.291* addr Address of the area to copy.292* size Size of the area.293* Returns:294* 0 for success, < 0 for error.295*/296int kdb_getarea_size(void *res, unsigned long addr, size_t size)297{298int ret = copy_from_kernel_nofault((char *)res, (char *)addr, size);299if (ret) {300if (!KDB_STATE(SUPPRESS)) {301kdb_func_printf("Bad address 0x%lx\n", addr);302KDB_STATE_SET(SUPPRESS);303}304ret = KDB_BADADDR;305} else {306KDB_STATE_CLEAR(SUPPRESS);307}308return ret;309}310311/*312* kdb_putarea_size - Write an area of data. The kdb equivalent of313* copy_to_user, with kdb messages for invalid addresses.314* Inputs:315* addr Address of the area to write to.316* res Pointer to the area holding the data.317* size Size of the area.318* Returns:319* 0 for success, < 0 for error.320*/321int kdb_putarea_size(unsigned long addr, void *res, size_t size)322{323int ret = copy_to_kernel_nofault((char *)addr, (char *)res, size);324if (ret) {325if (!KDB_STATE(SUPPRESS)) {326kdb_func_printf("Bad address 0x%lx\n", addr);327KDB_STATE_SET(SUPPRESS);328}329ret = KDB_BADADDR;330} else {331KDB_STATE_CLEAR(SUPPRESS);332}333return ret;334}335336/*337* kdb_getphys - Read data from a physical address. Validate the338* address is in range, use kmap_local_page() to get data339* similar to kdb_getarea() - but for phys addresses340* Inputs:341* res Pointer to the word to receive the result342* addr Physical address of the area to copy343* size Size of the area344* Returns:345* 0 for success, < 0 for error.346*/347static int kdb_getphys(void *res, unsigned long addr, size_t size)348{349unsigned long pfn;350void *vaddr;351struct page *page;352353pfn = (addr >> PAGE_SHIFT);354if (!pfn_valid(pfn))355return 1;356page = pfn_to_page(pfn);357vaddr = kmap_local_page(page);358memcpy(res, vaddr + (addr & (PAGE_SIZE - 1)), size);359kunmap_local(vaddr);360361return 0;362}363364/*365* kdb_getphysword366* Inputs:367* word Pointer to the word to receive the result.368* addr Address of the area to copy.369* size Size of the area.370* Returns:371* 0 for success, < 0 for error.372*/373int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size)374{375int diag;376__u8 w1;377__u16 w2;378__u32 w4;379__u64 w8;380*word = 0; /* Default value if addr or size is invalid */381382switch (size) {383case 1:384diag = kdb_getphys(&w1, addr, sizeof(w1));385if (!diag)386*word = w1;387break;388case 2:389diag = kdb_getphys(&w2, addr, sizeof(w2));390if (!diag)391*word = w2;392break;393case 4:394diag = kdb_getphys(&w4, addr, sizeof(w4));395if (!diag)396*word = w4;397break;398case 8:399if (size <= sizeof(*word)) {400diag = kdb_getphys(&w8, addr, sizeof(w8));401if (!diag)402*word = w8;403break;404}405fallthrough;406default:407diag = KDB_BADWIDTH;408kdb_func_printf("bad width %zu\n", size);409}410return diag;411}412413/*414* kdb_getword - Read a binary value. Unlike kdb_getarea, this treats415* data as numbers.416* Inputs:417* word Pointer to the word to receive the result.418* addr Address of the area to copy.419* size Size of the area.420* Returns:421* 0 for success, < 0 for error.422*/423int kdb_getword(unsigned long *word, unsigned long addr, size_t size)424{425int diag;426__u8 w1;427__u16 w2;428__u32 w4;429__u64 w8;430*word = 0; /* Default value if addr or size is invalid */431switch (size) {432case 1:433diag = kdb_getarea(w1, addr);434if (!diag)435*word = w1;436break;437case 2:438diag = kdb_getarea(w2, addr);439if (!diag)440*word = w2;441break;442case 4:443diag = kdb_getarea(w4, addr);444if (!diag)445*word = w4;446break;447case 8:448if (size <= sizeof(*word)) {449diag = kdb_getarea(w8, addr);450if (!diag)451*word = w8;452break;453}454fallthrough;455default:456diag = KDB_BADWIDTH;457kdb_func_printf("bad width %zu\n", size);458}459return diag;460}461462/*463* kdb_putword - Write a binary value. Unlike kdb_putarea, this464* treats data as numbers.465* Inputs:466* addr Address of the area to write to..467* word The value to set.468* size Size of the area.469* Returns:470* 0 for success, < 0 for error.471*/472int kdb_putword(unsigned long addr, unsigned long word, size_t size)473{474int diag;475__u8 w1;476__u16 w2;477__u32 w4;478__u64 w8;479switch (size) {480case 1:481w1 = word;482diag = kdb_putarea(addr, w1);483break;484case 2:485w2 = word;486diag = kdb_putarea(addr, w2);487break;488case 4:489w4 = word;490diag = kdb_putarea(addr, w4);491break;492case 8:493if (size <= sizeof(word)) {494w8 = word;495diag = kdb_putarea(addr, w8);496break;497}498fallthrough;499default:500diag = KDB_BADWIDTH;501kdb_func_printf("bad width %zu\n", size);502}503return diag;504}505506507508/*509* kdb_task_state_char - Return the character that represents the task state.510* Inputs:511* p struct task for the process512* Returns:513* One character to represent the task state.514*/515char kdb_task_state_char (const struct task_struct *p)516{517unsigned long tmp;518char state;519int cpu;520521if (!p ||522copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long)))523return 'E';524525state = task_state_to_char((struct task_struct *) p);526527if (is_idle_task(p)) {528/* Idle task. Is it really idle, apart from the kdb529* interrupt? */530cpu = kdb_process_cpu(p);531if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) {532if (cpu != kdb_initial_cpu)533state = '-'; /* idle task */534}535} else if (!p->mm && strchr("IMS", state)) {536state = tolower(state); /* sleeping system daemon */537}538return state;539}540541/*542* kdb_task_state - Return true if a process has the desired state543* given by the mask.544* Inputs:545* p struct task for the process546* mask set of characters used to select processes; both NULL547* and the empty string mean adopt a default filter, which548* is to suppress sleeping system daemons and the idle tasks549* Returns:550* True if the process matches at least one criteria defined by the mask.551*/552bool kdb_task_state(const struct task_struct *p, const char *mask)553{554char state = kdb_task_state_char(p);555556/* If there is no mask, then we will filter code that runs when the557* scheduler is idling and any system daemons that are currently558* sleeping.559*/560if (!mask || mask[0] == '\0')561return !strchr("-ims", state);562563/* A is a special case that matches all states */564if (strchr(mask, 'A'))565return true;566567return strchr(mask, state);568}569570571