* Factor out common code for manipulating the RSE backing store.

* Implement a fairly simplistic parser for unwinding stack frames.
* Use unwind records for DDB's 'trace' command. Also add support for
  tracing past exceptions to the context which generated the exception.

The stack unwind code requires a toolchain based on binutils-2.11.2 or
later and gcc-3.0.1 or later.
This commit is contained in:
Doug Rabson 2001-10-29 12:04:23 +00:00
parent c3338474b9
commit d57b94ba65
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=85685
7 changed files with 1689 additions and 136 deletions

View file

@ -51,6 +51,7 @@
#include <vm/vm.h>
#include <machine/inst.h>
#include <machine/rse.h>
#include <machine/db_machdep.h>
#include <machine/mutex.h>
@ -230,49 +231,6 @@ struct db_variable db_regs[] = {
};
struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
static int
rse_slot(u_int64_t *bsp)
{
return ((u_int64_t) bsp >> 3) & 0x3f;
}
/*
* Return the address of register regno (regno >= 32) given that bsp
* points at the base of the register stack frame.
*/
u_int64_t *
db_rse_register_address(u_int64_t *bsp, int regno)
{
int off = regno - 32;
u_int64_t rnats = (rse_slot(bsp) + off) / 63;
u_int64_t *p = bsp + off + rnats;
return p;
}
u_int64_t *
db_rse_current_frame()
{
int sof = ddb_regs.tf_cr_ifs & 0x7f;
u_int64_t *bsp = (u_int64_t *)
(ddb_regs.tf_ar_bspstore + ddb_regs.tf_ndirty);
return db_rse_previous_frame(bsp, sof);
}
u_int64_t *
db_rse_previous_frame(u_int64_t *bsp, int sof)
{
int slot = rse_slot(bsp);
int rnats = 0;
int count = sof;
while (count > slot) {
count -= 63;
rnats++;
slot = 63;
}
return bsp - sof - rnats;
}
static int
db_get_rse_reg(struct db_variable *vp, db_expr_t *valuep, int op)
{
@ -285,8 +243,8 @@ db_get_rse_reg(struct db_variable *vp, db_expr_t *valuep, int op)
if (op == DB_VAR_GET)
*valuep = 0xdeadbeefdeadbeef;
} else {
bsp = db_rse_previous_frame(bsp, sof);
reg = db_rse_register_address(bsp, regno);
bsp = ia64_rse_previous_frame(bsp, sof);
reg = ia64_rse_register_address(bsp, regno);
if (op == DB_VAR_GET)
*valuep = *reg;
else
@ -471,8 +429,8 @@ db_register_value(regs, regno)
if (regno - 32 >= sof) {
return 0xdeadbeefdeadbeef;
} else {
bsp = db_rse_previous_frame(bsp, sof);
reg = db_rse_register_address(bsp, regno);
bsp = ia64_rse_previous_frame(bsp, sof);
reg = ia64_rse_register_address(bsp, regno);
return *reg;
}
}

View file

@ -30,6 +30,8 @@
#include <sys/proc.h>
#include <machine/inst.h>
#include <machine/db_machdep.h>
#include <machine/unwind.h>
#include <machine/rse.h>
#include <ddb/ddb.h>
#include <ddb/db_sym.h>
@ -42,76 +44,94 @@ int db_md_set_watchpoint __P((db_expr_t addr, db_expr_t size));
int db_md_clr_watchpoint __P((db_expr_t addr, db_expr_t size));
void db_md_list_watchpoints __P((void));
extern char ia64_vector_table[], do_syscall[], do_syscall_end[];
void
db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, char *modif)
{
db_addr_t callpc;
u_int64_t *bsp;
int sof, sol;
struct ia64_unwind_state *us;
if (count == -1)
count = 65535;
if (!have_addr) {
callpc = (db_addr_t)ddb_regs.tf_cr_iip;
bsp = db_rse_current_frame();
sof = ddb_regs.tf_cr_ifs & 0x7f;
sol = (ddb_regs.tf_cr_ifs >> 7) & 0x7f;
us = ia64_create_unwind_state(&ddb_regs);
} else {
callpc = 0; /* XXX */
bsp = 0; /* XXX */
sof = 0; /* XXX */
sol = 0; /* XXX */
return; /* XXX */
}
if (!us) {
db_printf("db_stack_trace_cmd: can't create unwind state\n");
return;
}
while (count--) {
const char * name;
db_expr_t ip;
db_expr_t offset;
c_db_sym_t sym;
u_int64_t ar_pfs;
u_int64_t newpc;
int newsof, newsol, nargs, i;
int cfm, sof, sol, nargs, i;
u_int64_t *bsp;
u_int64_t *p, reg;
/*
* XXX this assumes the simplistic stack frames used
* by the old toolchain.
*/
ar_pfs = *db_rse_register_address(bsp, 32 + sol - 1);
newpc = *db_rse_register_address(bsp, 32 + sol - 2);
newsof = ar_pfs & 0x7f;
newsol = (ar_pfs >> 7) & 0x7f;
ip = ia64_unwind_state_get_ip(us);
cfm = ia64_unwind_state_get_cfm(us);
bsp = ia64_unwind_state_get_bsp(us);
sof = cfm & 0x7f;
sol = (cfm >> 7) & 0x7f;
sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
sym = db_search_symbol(ip, DB_STGY_ANY, &offset);
db_symbol_values(sym, &name, NULL);
db_printf("%s(", name);
nargs = newsof - newsol;
nargs = sof - sol;
if (nargs > 8)
nargs = 8;
for (i = 0; i < nargs; i++) {
p = ia64_rse_register_address(bsp, 32 + i);
db_read_bytes((vm_offset_t) p, sizeof(reg),
(caddr_t) &reg);
if (i > 0)
db_printf(", ");
db_printf("0x%lx",
*db_rse_register_address(bsp, 32 + i));
db_printf("0x%lx", reg);
}
db_printf(") at ");
db_printsym(callpc, DB_STGY_PROC);
db_printsym(ip, DB_STGY_PROC);
db_printf("\n");
bsp = db_rse_previous_frame(bsp, newsol);
callpc = newpc;
sol = newsol;
sof = newsof;
if ((callpc >> 61) != 7)
/*
* Was this an exception? If so, we can keep unwinding
* based on the interrupted trapframe. We could do
* this by constructing funky unwind records in
* exception.s but this is easier.
*/
if (ip >= (u_int64_t) &ia64_vector_table[0]
&& ip < (u_int64_t) &ia64_vector_table[32768]) {
u_int64_t sp = ia64_unwind_state_get_sp(us);
ia64_free_unwind_state(us);
us = ia64_create_unwind_state((struct trapframe *)
(sp + 16));
} else if (ip >= (u_int64_t) &do_syscall[0]
&& ip < (u_int64_t) &do_syscall_end[0]) {
u_int64_t sp = ia64_unwind_state_get_sp(us);
ia64_free_unwind_state(us);
us = ia64_create_unwind_state((struct trapframe *)
(sp + 16 + 8*8));
} else {
if (ia64_unwind_state_previous_frame(us))
break;
}
ip = ia64_unwind_state_get_ip(us);
if (!ip)
break;
}
ia64_free_unwind_state(us);
}
int
db_md_set_watchpoint(addr, size)
db_expr_t addr;

View file

@ -78,6 +78,8 @@
#include <machine/sigframe.h>
#include <machine/efi.h>
#include <machine/inst.h>
#include <machine/rse.h>
#include <machine/unwind.h>
#ifdef SKI
extern void ia64_ski_init(void);
@ -96,6 +98,10 @@ extern char kstack[];
struct user *proc0uarea;
vm_offset_t proc0kstack;
extern u_int64_t kernel_text[], _end[];
extern u_int64_t _ia64_unwind_start[];
extern u_int64_t _ia64_unwind_end[];
u_int64_t ia64_port_base;
char machine[] = "ia64";
@ -395,7 +401,17 @@ identifycpu(void)
"\001LB");
}
extern char kernel_text[], _end[];
static void
add_kernel_unwind_tables(void *arg)
{
/*
* Register the kernel's unwind table.
*/
ia64_add_unwind_table(kernel_text,
_ia64_unwind_start,
_ia64_unwind_end);
}
SYSINIT(unwind, SI_SUB_KMEM, SI_ORDER_ANY, add_kernel_unwind_tables, 0);
static void
map_pal_code(void)
@ -1095,6 +1111,10 @@ sigreturn(struct thread *td,
(caddr_t)&uc, sizeof(ucontext_t)))
return (EFAULT);
if (frame->tf_ndirty != 0) {
printf("sigreturn: dirty user stacked registers\n");
}
/*
* Restore the user-supplied information
*/
@ -1504,3 +1524,40 @@ ia64_pack_bundle(u_int64_t *lowp, u_int64_t *highp,
*highp = high;
}
static int
rse_slot(u_int64_t *bsp)
{
return ((u_int64_t) bsp >> 3) & 0x3f;
}
/*
* Return the address of register regno (regno >= 32) given that bsp
* points at the base of the register stack frame.
*/
u_int64_t *
ia64_rse_register_address(u_int64_t *bsp, int regno)
{
int off = regno - 32;
u_int64_t rnats = (rse_slot(bsp) + off) / 63;
return bsp + off + rnats;
}
/*
* Calculate the base address of the previous frame given that the
* current frame's locals area is 'size'.
*/
u_int64_t *
ia64_rse_previous_frame(u_int64_t *bsp, int size)
{
int slot = rse_slot(bsp);
int rnats = 0;
int count = size;
while (count > slot) {
count -= 63;
rnats++;
slot = 63;
}
return bsp - size - rnats;
}

View file

@ -34,6 +34,7 @@
#include <vm/vm_extern.h>
#include <machine/frame.h>
#include <machine/inst.h>
#include <machine/rse.h>
#define sign_extend(imm, w) (((int64_t)(imm) << (64 - (w))) >> (64 - (w)))
@ -148,56 +149,6 @@ unaligned_decode_M5(union ia64_instruction ins, struct decoding *d)
return 1;
}
static int
rse_slot(u_int64_t *bsp)
{
return ((u_int64_t) bsp >> 3) & 0x3f;
}
/*
* Return the address of register regno (regno >= 32) given that bsp
* points at the base of the register stack frame.
*/
static u_int64_t *
rse_register_address(u_int64_t *bsp, int regno)
{
int off = regno - 32;
u_int64_t rnats = (rse_slot(bsp) + off) / 63;
u_int64_t *p = bsp + off + rnats;
/*
* We only really need this if the current bspstore
* hasn't advanced past the user's register frame. Its
* hardly worth trying to optimise though.
*/
__asm __volatile("flushrs");
return p;
}
static u_int64_t *
rse_previous_frame(u_int64_t *bsp, int sof)
{
int slot = rse_slot(bsp);
int rnats = 0;
int count = sof;
while (count > slot) {
count -= 63;
rnats++;
slot = 63;
}
return bsp - sof - rnats;
}
static u_int64_t *
rse_current_frame(struct trapframe *framep, struct thread *td)
{
int sof = framep->tf_cr_ifs & 0x7f;
u_int64_t *bsp = (u_int64_t *) (td->td_kstack + framep->tf_ndirty);
return rse_previous_frame(bsp, sof);
}
static int
read_register(struct trapframe *framep, struct thread *td,
int reg, u_int64_t *valuep)
@ -210,10 +161,16 @@ read_register(struct trapframe *framep, struct thread *td,
return 0;
} else {
u_int64_t cfm = framep->tf_cr_ifs;
u_int64_t *bsp = (u_int64_t *) (td->td_kstack
+ framep->tf_ndirty);
int sof = cfm & 0x7f;
int sor = 8*((cfm >> 14) & 15);
int rrb_gr = (cfm >> 18) & 0x7f;
u_int64_t *bsp = rse_current_frame(framep, td);
/*
* Skip back to the start of the interrupted frame.
*/
bsp = ia64_rse_previous_frame(bsp, sof);
if (reg - 32 > sof)
return EINVAL;
@ -224,7 +181,7 @@ read_register(struct trapframe *framep, struct thread *td,
reg = reg + rrb_gr;
}
*valuep = *rse_register_address(bsp, reg);
*valuep = *ia64_rse_register_address(bsp, reg);
return 0;
}
@ -242,10 +199,16 @@ write_register(struct trapframe *framep, struct thread *td,
return 0;
} else {
u_int64_t cfm = framep->tf_cr_ifs;
u_int64_t *bsp = (u_int64_t *) (td->td_kstack
+ framep->tf_ndirty);
int sof = cfm & 0x7f;
int sor = 8*((cfm >> 14) & 15);
int rrb_gr = (cfm >> 18) & 0x7f;
u_int64_t *bsp = rse_current_frame(framep, td);
/*
* Skip back to the start of the interrupted frame.
*/
bsp = ia64_rse_previous_frame(bsp, sof);
if (reg - 32 > sof)
return EINVAL;
@ -256,7 +219,7 @@ write_register(struct trapframe *framep, struct thread *td,
reg = reg + rrb_gr;
}
*rse_register_address(bsp, reg) = value;
*ia64_rse_register_address(bsp, reg) = value;
return 0;
}
@ -530,6 +493,13 @@ unaligned_fixup(struct trapframe *framep, struct thread *td)
u_int64_t addr, update, value, isr;
int error = 0;
/*
* We only really need this if the current bspstore
* hasn't advanced past the user's register frame. Its
* hardly worth trying to optimise though.
*/
__asm __volatile("flushrs");
isr = framep->tf_cr_isr;
error = read_register(framep, td, dec.basereg, &addr);
if (error) {

1472
sys/ia64/ia64/unwind.c Normal file

File diff suppressed because it is too large Load diff

35
sys/ia64/include/rse.h Normal file
View file

@ -0,0 +1,35 @@
/*-
* Copyright (c) 2001 Doug Rabson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _MACHINE_RSE_H_
#define _MACHINE_RSE_H_
u_int64_t *ia64_rse_register_address(u_int64_t *bsp, int regno);
u_int64_t *ia64_rse_previous_frame(u_int64_t *bsp, int size);
#endif /* _MACHINE_RSE_H_ */

41
sys/ia64/include/unwind.h Normal file
View file

@ -0,0 +1,41 @@
/*-
* Copyright (c) 2001 Doug Rabson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
struct ia64_unwind_table;
struct ia64_unwind_table *ia64_add_unwind_table(u_int64_t *base,
u_int64_t *start,
u_int64_t *end);
void ia64_free_unwind_table(struct ia64_unwind_table *ut);
struct ia64_unwind_state *ia64_create_unwind_state(struct trapframe *framep);
void ia64_free_unwind_state(struct ia64_unwind_state *us);
u_int64_t ia64_unwind_state_get_ip(struct ia64_unwind_state *us);
u_int64_t ia64_unwind_state_get_sp(struct ia64_unwind_state *us);
u_int64_t ia64_unwind_state_get_cfm(struct ia64_unwind_state *us);
u_int64_t *ia64_unwind_state_get_bsp(struct ia64_unwind_state *us);
int ia64_unwind_state_previous_frame(struct ia64_unwind_state *us);