File Index
Files and directories
Sign in
FILE

kdb_support.c

File preview with syntax highlighting.

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