Linux Audio

Check our new training course

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