#include <sys/cdefs.h>
#include "opt_kstack_pages.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/smp.h>
#include <sys/sysent.h>
#include <net/vnet.h>
#include <ddb/ddb.h>
#include <ddb/db_sym.h>
#include <ddb/db_variables.h>
#include "opt_ddb.h"
#ifndef MAXNOSYMTABS
#define MAXNOSYMTABS 3
#endif
static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
static int db_nsymtab = 0;
static db_symtab_t *db_last_symtab;
static c_db_sym_t db_lookup( const char *symstr);
static char *db_qualify(c_db_sym_t sym, char *symtabname);
static bool db_symbol_is_ambiguous(c_db_sym_t sym);
static bool db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
static int db_cpu = -1;
#ifdef VIMAGE
static void *db_vnet = NULL;
#endif
int
db_var_db_cpu(struct db_variable *vp, db_expr_t *valuep, int op)
{
switch (op) {
case DB_VAR_GET:
*valuep = db_cpu;
return (1);
case DB_VAR_SET:
if (*(int *)valuep < -1 || *(int *)valuep > mp_maxid) {
db_printf("Invalid value: %d\n", *(int*)valuep);
return (0);
}
db_cpu = *(int *)valuep;
return (1);
default:
db_printf("db_var_db_cpu: unknown operation\n");
return (0);
}
}
int
db_var_curcpu(struct db_variable *vp, db_expr_t *valuep, int op)
{
switch (op) {
case DB_VAR_GET:
*valuep = curcpu;
return (1);
case DB_VAR_SET:
db_printf("Read-only variable.\n");
return (0);
default:
db_printf("db_var_curcpu: unknown operation\n");
return (0);
}
}
#ifdef VIMAGE
int
db_var_db_vnet(struct db_variable *vp, db_expr_t *valuep, int op)
{
switch (op) {
case DB_VAR_GET:
*valuep = (db_expr_t)db_vnet;
return (1);
case DB_VAR_SET:
db_vnet = *(void **)valuep;
return (1);
default:
db_printf("db_var_db_vnet: unknown operation\n");
return (0);
}
}
int
db_var_curvnet(struct db_variable *vp, db_expr_t *valuep, int op)
{
switch (op) {
case DB_VAR_GET:
*valuep = (db_expr_t)curvnet;
return (1);
case DB_VAR_SET:
db_printf("Read-only variable.\n");
return (0);
default:
db_printf("db_var_curvnet: unknown operation\n");
return (0);
}
}
#endif
void
db_add_symbol_table(char *start, char *end, char *name, char *ref)
{
if (db_nsymtab >= MAXNOSYMTABS) {
printf ("No slots left for %s symbol table", name);
panic ("db_sym.c: db_add_symbol_table");
}
db_symtabs[db_nsymtab].start = start;
db_symtabs[db_nsymtab].end = end;
db_symtabs[db_nsymtab].name = name;
db_symtabs[db_nsymtab].private = ref;
db_nsymtab++;
}
static char *
db_qualify(c_db_sym_t sym, char *symtabname)
{
const char *symname;
static char tmp[256];
db_symbol_values(sym, &symname, 0);
snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
return tmp;
}
bool
db_eqname(const char *src, const char *dst, int c)
{
if (!strcmp(src, dst))
return (true);
if (src[0] == c)
return (!strcmp(src+1,dst));
return (false);
}
bool
db_value_of_name(const char *name, db_expr_t *valuep)
{
c_db_sym_t sym;
sym = db_lookup(name);
if (sym == C_DB_SYM_NULL)
return (false);
db_symbol_values(sym, &name, valuep);
return (true);
}
bool
db_value_of_name_pcpu(const char *name, db_expr_t *valuep)
{
static char tmp[256];
db_expr_t value;
c_db_sym_t sym;
int cpu;
if (db_cpu != -1)
cpu = db_cpu;
else
cpu = curcpu;
snprintf(tmp, sizeof(tmp), "pcpu_entry_%s", name);
sym = db_lookup(tmp);
if (sym == C_DB_SYM_NULL)
return (false);
db_symbol_values(sym, &name, &value);
if (value < DPCPU_START || value >= DPCPU_STOP)
return (false);
*valuep = (db_expr_t)((uintptr_t)value + dpcpu_off[cpu]);
return (true);
}
bool
db_value_of_name_vnet(const char *name, db_expr_t *valuep)
{
#ifdef VIMAGE
static char tmp[256];
db_expr_t value;
c_db_sym_t sym;
struct vnet *vnet;
if (db_vnet != NULL)
vnet = db_vnet;
else
vnet = curvnet;
snprintf(tmp, sizeof(tmp), "vnet_entry_%s", name);
sym = db_lookup(tmp);
if (sym == C_DB_SYM_NULL)
return (false);
db_symbol_values(sym, &name, &value);
if (value < VNET_START || value >= VNET_STOP)
return (false);
*valuep = (db_expr_t)((uintptr_t)value + vnet->vnet_data_base);
return (true);
#else
return (false);
#endif
}
static c_db_sym_t
db_lookup(const char *symstr)
{
c_db_sym_t sp;
int i;
int symtab_start = 0;
int symtab_end = db_nsymtab;
const char *cp;
for (cp = symstr; *cp; cp++) {
if (*cp == ':') {
for (i = 0; i < db_nsymtab; i++) {
int n = strlen(db_symtabs[i].name);
if (
n == (cp - symstr) &&
strncmp(symstr, db_symtabs[i].name, n) == 0
) {
symtab_start = i;
symtab_end = i + 1;
break;
}
}
if (i == db_nsymtab) {
db_error("invalid symbol table name");
}
symstr = cp+1;
}
}
for (i = symtab_start; i < symtab_end; i++) {
sp = X_db_lookup(&db_symtabs[i], symstr);
if (sp) {
db_last_symtab = &db_symtabs[i];
return sp;
}
}
return 0;
}
static volatile bool db_qualify_ambiguous_names = false;
static bool
db_symbol_is_ambiguous(c_db_sym_t sym)
{
const char *sym_name;
int i;
bool found_once = false;
if (!db_qualify_ambiguous_names)
return (false);
db_symbol_values(sym, &sym_name, 0);
for (i = 0; i < db_nsymtab; i++) {
if (X_db_lookup(&db_symtabs[i], sym_name)) {
if (found_once)
return (true);
found_once = true;
}
}
return (false);
}
c_db_sym_t
db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
{
unsigned int diff;
size_t newdiff;
int i;
c_db_sym_t ret, sym;
if (val < PAGE_SIZE) {
*offp = 0;
return (C_DB_SYM_NULL);
}
ret = C_DB_SYM_NULL;
newdiff = diff = val;
for (i = 0; i < db_nsymtab; i++) {
sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
if ((uintmax_t)newdiff < (uintmax_t)diff) {
db_last_symtab = &db_symtabs[i];
diff = newdiff;
ret = sym;
}
}
*offp = diff;
return ret;
}
void
db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
{
db_expr_t value;
if (sym == DB_SYM_NULL) {
*namep = NULL;
return;
}
X_db_symbol_values(db_last_symtab, sym, namep, &value);
if (db_symbol_is_ambiguous(sym))
*namep = db_qualify(sym, db_last_symtab->name);
if (valuep)
*valuep = value;
}
db_expr_t db_maxoff = 0x10000;
void
db_printsym(db_expr_t off, db_strategy_t strategy)
{
db_expr_t d;
char *filename;
const char *name;
int linenum;
c_db_sym_t cursym;
if (off < 0 && off >= -db_maxoff) {
db_printf("%+#lr", (long)off);
return;
}
cursym = db_search_symbol(off, strategy, &d);
db_symbol_values(cursym, &name, NULL);
if (name == NULL || d >= (db_addr_t)db_maxoff) {
db_printf("%#lr", (unsigned long)off);
return;
}
#ifdef DDB_NUMSYM
db_printf("%#lr = %s", (unsigned long)off, name);
#else
db_printf("%s", name);
#endif
if (d)
db_printf("+%+#lr", (long)d);
if (strategy == DB_STGY_PROC) {
if (db_line_at_pc(cursym, &filename, &linenum, off))
db_printf(" [%s:%d]", filename, linenum);
}
}
static bool
db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
{
return (X_db_line_at_pc(db_last_symtab, sym, filename, linenum, pc));
}
bool
db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
{
return (X_db_sym_numargs(db_last_symtab, sym, nargp, argnames));
}
void
db_decode_syscall(struct thread *td, u_int number)
{
struct proc *p;
db_printf(" (%u", number);
p = (td != NULL) ? td->td_proc : NULL;
if (p != NULL) {
db_printf(", %s, %s", p->p_sysent->sv_name,
syscallname(p, number));
}
db_printf(")");
}