2005-01-06 01:34:41 +00:00
|
|
|
/*-
|
2017-11-30 15:48:35 +00:00
|
|
|
* SPDX-License-Identifier: MIT-CMU
|
|
|
|
*
|
2004-07-10 23:47:20 +00:00
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1991,1990 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify and distribute this software and its
|
|
|
|
* documentation is hereby granted, provided that both the copyright
|
|
|
|
* notice and this permission notice appear in all copies of the
|
|
|
|
* software, derivative works or modified versions, and any portions
|
|
|
|
* thereof, and that both notices appear in supporting documentation.
|
|
|
|
*
|
|
|
|
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
|
|
|
|
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
|
|
|
|
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* Carnegie Mellon requests users of this software to return to
|
|
|
|
*
|
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
|
|
|
*
|
|
|
|
* any improvements or extensions that they make and grant Carnegie the
|
|
|
|
* rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/cons.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/kdb.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/pcpu.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/reboot.h>
|
Add a new DDB(4) facility, output capture. Input and output from DDB may be
captured to a memory buffer for later inspection using sysctl(8), or in the
future, to a textdump.
A new DDB command, "capture", is added, which accepts arguments "on", "off",
"reset", and "status".
A new DDB sysctl tree, debug.ddb.capture, is added, which can be used to
resize the capture buffer and extract buffer contents.
MFC after: 3 months
2007-12-25 23:06:51 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-07-10 23:47:20 +00:00
|
|
|
|
|
|
|
#include <machine/kdb.h>
|
|
|
|
#include <machine/pcb.h>
|
|
|
|
#include <machine/setjmp.h>
|
|
|
|
|
|
|
|
#include <ddb/ddb.h>
|
|
|
|
#include <ddb/db_command.h>
|
|
|
|
#include <ddb/db_sym.h>
|
|
|
|
|
Add a new DDB(4) facility, output capture. Input and output from DDB may be
captured to a memory buffer for later inspection using sysctl(8), or in the
future, to a textdump.
A new DDB command, "capture", is added, which accepts arguments "on", "off",
"reset", and "status".
A new DDB sysctl tree, debug.ddb.capture, is added, which can be used to
resize the capture buffer and extract buffer contents.
MFC after: 3 months
2007-12-25 23:06:51 +00:00
|
|
|
SYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW, 0, "DDB settings");
|
|
|
|
|
2004-07-10 23:47:20 +00:00
|
|
|
static dbbe_init_f db_init;
|
|
|
|
static dbbe_trap_f db_trap;
|
2006-11-06 11:10:57 +00:00
|
|
|
static dbbe_trace_f db_trace_self_wrapper;
|
2012-04-12 21:34:58 +00:00
|
|
|
static dbbe_trace_thread_f db_trace_thread_wrapper;
|
2004-07-10 23:47:20 +00:00
|
|
|
|
2012-04-12 21:34:58 +00:00
|
|
|
KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trace_thread_wrapper,
|
|
|
|
db_trap);
|
2004-07-10 23:47:20 +00:00
|
|
|
|
2014-09-25 08:28:10 +00:00
|
|
|
/*
|
|
|
|
* Symbols can be loaded by specifying the exact addresses of
|
|
|
|
* the symtab and strtab in memory. This is used when loaded from
|
|
|
|
* boot loaders different than the native one (like Xen).
|
|
|
|
*/
|
|
|
|
vm_offset_t ksymtab, kstrtab, ksymtab_size;
|
2004-07-10 23:47:20 +00:00
|
|
|
|
2015-05-21 15:16:18 +00:00
|
|
|
bool
|
2004-07-10 23:47:20 +00:00
|
|
|
X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
|
|
|
|
db_expr_t off)
|
|
|
|
{
|
2015-05-18 22:27:46 +00:00
|
|
|
return (false);
|
2004-07-10 23:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c_db_sym_t
|
|
|
|
X_db_lookup(db_symtab_t *symtab, const char *symbol)
|
|
|
|
{
|
|
|
|
c_linker_sym_t lsym;
|
|
|
|
Elf_Sym *sym;
|
|
|
|
|
|
|
|
if (symtab->private == NULL) {
|
|
|
|
return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
|
|
|
|
? lsym : NULL));
|
|
|
|
} else {
|
|
|
|
sym = (Elf_Sym *)symtab->start;
|
|
|
|
while ((char *)sym < symtab->end) {
|
|
|
|
if (sym->st_name != 0 &&
|
|
|
|
!strcmp(symtab->private + sym->st_name, symbol))
|
|
|
|
return ((c_db_sym_t)sym);
|
|
|
|
sym++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
c_db_sym_t
|
|
|
|
X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
|
|
|
|
db_expr_t *diffp)
|
|
|
|
{
|
|
|
|
c_linker_sym_t lsym;
|
|
|
|
Elf_Sym *sym, *match;
|
|
|
|
unsigned long diff;
|
|
|
|
|
|
|
|
if (symtab->private == NULL) {
|
|
|
|
if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
|
|
|
|
*diffp = (db_expr_t)diff;
|
|
|
|
return ((c_db_sym_t)lsym);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = ~0UL;
|
|
|
|
match = NULL;
|
|
|
|
for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
|
2015-07-21 23:07:55 +00:00
|
|
|
if (sym->st_name == 0 || sym->st_shndx == SHN_UNDEF)
|
2004-07-10 23:47:20 +00:00
|
|
|
continue;
|
|
|
|
if (off < sym->st_value)
|
|
|
|
continue;
|
|
|
|
if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
|
|
|
|
ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
|
|
|
|
ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
|
|
|
|
continue;
|
|
|
|
if ((off - sym->st_value) > diff)
|
|
|
|
continue;
|
|
|
|
if ((off - sym->st_value) < diff) {
|
|
|
|
diff = off - sym->st_value;
|
|
|
|
match = sym;
|
|
|
|
} else {
|
|
|
|
if (match == NULL)
|
|
|
|
match = sym;
|
|
|
|
else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
|
|
|
|
ELF_ST_BIND(sym->st_info) != STB_LOCAL)
|
|
|
|
match = sym;
|
|
|
|
}
|
|
|
|
if (diff == 0) {
|
|
|
|
if (strat == DB_STGY_PROC &&
|
|
|
|
ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
|
|
|
|
ELF_ST_BIND(sym->st_info) != STB_LOCAL)
|
|
|
|
break;
|
|
|
|
if (strat == DB_STGY_ANY &&
|
|
|
|
ELF_ST_BIND(sym->st_info) != STB_LOCAL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*diffp = (match == NULL) ? off : diff;
|
|
|
|
return ((c_db_sym_t)match);
|
|
|
|
}
|
|
|
|
|
2015-05-21 15:16:18 +00:00
|
|
|
bool
|
2004-07-10 23:47:20 +00:00
|
|
|
X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
|
|
|
|
char **argp)
|
|
|
|
{
|
2015-05-18 22:27:46 +00:00
|
|
|
return (false);
|
2004-07-10 23:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
|
|
|
|
db_expr_t *valp)
|
|
|
|
{
|
|
|
|
linker_symval_t lval;
|
|
|
|
|
|
|
|
if (symtab->private == NULL) {
|
|
|
|
linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
|
|
|
|
if (namep != NULL)
|
|
|
|
*namep = (const char*)lval.name;
|
|
|
|
if (valp != NULL)
|
|
|
|
*valp = (db_expr_t)lval.value;
|
|
|
|
} else {
|
|
|
|
if (namep != NULL)
|
|
|
|
*namep = (const char *)symtab->private +
|
|
|
|
((const Elf_Sym *)sym)->st_name;
|
|
|
|
if (valp != NULL)
|
|
|
|
*valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:28:10 +00:00
|
|
|
int
|
|
|
|
db_fetch_ksymtab(vm_offset_t ksym_start, vm_offset_t ksym_end)
|
|
|
|
{
|
|
|
|
Elf_Size strsz;
|
|
|
|
|
|
|
|
if (ksym_end > ksym_start && ksym_start != 0) {
|
|
|
|
ksymtab = ksym_start;
|
|
|
|
ksymtab_size = *(Elf_Size*)ksymtab;
|
|
|
|
ksymtab += sizeof(Elf_Size);
|
|
|
|
kstrtab = ksymtab + ksymtab_size;
|
|
|
|
strsz = *(Elf_Size*)kstrtab;
|
|
|
|
kstrtab += sizeof(Elf_Size);
|
|
|
|
if (kstrtab + strsz > ksym_end) {
|
|
|
|
/* Sizes doesn't match, unset everything. */
|
|
|
|
ksymtab = ksymtab_size = kstrtab = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ksymtab == 0 || ksymtab_size == 0 || kstrtab == 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-07-10 23:47:20 +00:00
|
|
|
static int
|
|
|
|
db_init(void)
|
|
|
|
{
|
|
|
|
|
2008-09-25 19:50:14 +00:00
|
|
|
db_command_init();
|
2014-09-25 08:28:10 +00:00
|
|
|
|
|
|
|
if (ksymtab != 0 && kstrtab != 0 && ksymtab_size != 0) {
|
|
|
|
db_add_symbol_table((char *)ksymtab,
|
|
|
|
(char *)(ksymtab + ksymtab_size), "elf", (char *)kstrtab);
|
2004-07-10 23:47:20 +00:00
|
|
|
}
|
|
|
|
db_add_symbol_table(NULL, NULL, "kld", NULL);
|
|
|
|
return (1); /* We're the default debugger. */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
db_trap(int type, int code)
|
|
|
|
{
|
|
|
|
jmp_buf jb;
|
|
|
|
void *prev_jb;
|
2015-05-21 15:16:18 +00:00
|
|
|
bool bkpt, watchpt;
|
Add a simple scripting facility to DDB(4), allowing the user to
define a set of named scripts. Each script consists of a list of DDB
commands separated by ";"s that will be executed verbatim. No higher
level language constructs, such as branching, are provided for:
scripts are executed by sequentially injecting commands into the DDB
input buffer.
Four new commands are present in DDB: "run" to run a specific script,
"script" to define or print a script, "scripts" to list currently
defined scripts, and "unscript" to delete a script, modeled on shell
alias commands. Scripts may also be manipulated using sysctls in the
debug.ddb.scripting MIB space, although users will prefer to use the
soon-to-be-added ddb(8) tool for usability reasons.
Scripts with certain names are automatically executed on various DDB
events, such as entering the debugger via a panic, a witness error,
watchdog, breakpoint, sysctl, serial break, etc, allowing customized
handling.
MFC after: 3 months
2007-12-26 09:33:19 +00:00
|
|
|
const char *why;
|
2004-07-10 23:47:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't handle the trap if the console is unavailable (i.e. it
|
|
|
|
* is in graphics mode).
|
|
|
|
*/
|
|
|
|
if (cnunavailable())
|
|
|
|
return (0);
|
|
|
|
|
2016-09-09 15:53:42 +00:00
|
|
|
if (db_stop_at_pc(type, code, &bkpt, &watchpt)) {
|
2004-07-10 23:47:20 +00:00
|
|
|
if (db_inst_count) {
|
|
|
|
db_printf("After %d instructions (%d loads, %d stores),\n",
|
|
|
|
db_inst_count, db_load_count, db_store_count);
|
|
|
|
}
|
|
|
|
prev_jb = kdb_jmpbuf(jb);
|
|
|
|
if (setjmp(jb) == 0) {
|
|
|
|
db_dot = PC_REGS();
|
|
|
|
db_print_thread();
|
|
|
|
if (bkpt)
|
|
|
|
db_printf("Breakpoint at\t");
|
|
|
|
else if (watchpt)
|
|
|
|
db_printf("Watchpoint at\t");
|
|
|
|
else
|
|
|
|
db_printf("Stopped at\t");
|
|
|
|
db_print_loc_and_inst(db_dot);
|
|
|
|
}
|
Add a simple scripting facility to DDB(4), allowing the user to
define a set of named scripts. Each script consists of a list of DDB
commands separated by ";"s that will be executed verbatim. No higher
level language constructs, such as branching, are provided for:
scripts are executed by sequentially injecting commands into the DDB
input buffer.
Four new commands are present in DDB: "run" to run a specific script,
"script" to define or print a script, "scripts" to list currently
defined scripts, and "unscript" to delete a script, modeled on shell
alias commands. Scripts may also be manipulated using sysctls in the
debug.ddb.scripting MIB space, although users will prefer to use the
soon-to-be-added ddb(8) tool for usability reasons.
Scripts with certain names are automatically executed on various DDB
events, such as entering the debugger via a panic, a witness error,
watchdog, breakpoint, sysctl, serial break, etc, allowing customized
handling.
MFC after: 3 months
2007-12-26 09:33:19 +00:00
|
|
|
why = kdb_why;
|
|
|
|
db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown");
|
2004-07-10 23:47:20 +00:00
|
|
|
db_command_loop();
|
|
|
|
(void)kdb_jmpbuf(prev_jb);
|
|
|
|
}
|
|
|
|
|
|
|
|
db_restart_at_pc(watchpt);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
2006-11-06 11:10:57 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
db_trace_self_wrapper(void)
|
|
|
|
{
|
|
|
|
jmp_buf jb;
|
|
|
|
void *prev_jb;
|
|
|
|
|
|
|
|
prev_jb = kdb_jmpbuf(jb);
|
|
|
|
if (setjmp(jb) == 0)
|
|
|
|
db_trace_self();
|
|
|
|
(void)kdb_jmpbuf(prev_jb);
|
|
|
|
}
|
2012-04-12 21:34:58 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
db_trace_thread_wrapper(struct thread *td)
|
|
|
|
{
|
|
|
|
jmp_buf jb;
|
|
|
|
void *prev_jb;
|
|
|
|
|
|
|
|
prev_jb = kdb_jmpbuf(jb);
|
|
|
|
if (setjmp(jb) == 0)
|
|
|
|
db_trace_thread(td, -1);
|
|
|
|
(void)kdb_jmpbuf(prev_jb);
|
|
|
|
}
|