Skip to content
breakpoint.c 9.61 KiB
Newer Older
ram's avatar
ram committed
/*

 This code was written as part of the CMU Common Lisp project at
 Carnegie Mellon University, and has been placed in the public domain.

*/

wlott's avatar
wlott committed
#include <stdio.h>
#include <signal.h>

#include "lisp.h"
#include "os.h"
#include "internals.h"
ram's avatar
ram committed
#include "interrupt.h"
wlott's avatar
wlott committed
#include "arch.h"
#include "lispregs.h"
#include "globals.h"
#include "alloc.h"
#include "breakpoint.h"
dtc's avatar
dtc committed
#if defined GENCGC
#include "gencgc.h"
#endif
wlott's avatar
wlott committed

 * See MAKE-BOGUS-LRA in code/debug-int.lisp for these values.
 *
rtoy's avatar
rtoy committed
 * Ideally, internals.h should have the correct values.  We leave
 * these defaults here for now.
rtoy's avatar
rtoy committed
#ifndef REAL_LRA_SLOT
wlott's avatar
wlott committed
#define REAL_LRA_SLOT 0
rtoy's avatar
rtoy committed
#endif

#ifndef KNOWN_RETURN_P_SLOT
wlott's avatar
wlott committed
#define KNOWN_RETURN_P_SLOT 1
#else
#define KNOWN_RETURN_P_SLOT 2
rtoy's avatar
rtoy committed
#endif
#endif

#ifndef BOGUS_LRA_CONSTANTS
#ifndef i386
#define BOGUS_LRA_CONSTANTS 2
#else
#define BOGUS_LRA_CONSTANTS 3
#endif
rtoy's avatar
rtoy committed
#endif

wlott's avatar
wlott committed

static void *
compute_pc(lispobj code_obj, int pc_offset)
wlott's avatar
wlott committed
{
    struct code *code;

    code = (struct code *) PTR(code_obj);
    return (void *) ((char *) code + HeaderValue(code->header) * sizeof(lispobj)
		     + pc_offset);
wlott's avatar
wlott committed
}

unsigned long
breakpoint_install(lispobj code_obj, int pc_offset)
wlott's avatar
wlott committed
{
    return arch_install_breakpoint(compute_pc(code_obj, pc_offset));
}

void
breakpoint_remove(lispobj code_obj, int pc_offset, unsigned long orig_inst)
wlott's avatar
wlott committed
{
    arch_remove_breakpoint(compute_pc(code_obj, pc_offset), orig_inst);
}

void
breakpoint_do_displaced_inst(os_context_t * scp, unsigned long orig_inst)
wlott's avatar
wlott committed
{
#if !defined(hpux) && !defined(irix) && !defined(i386)
wlott's avatar
wlott committed
    undo_fake_foreign_function_call(scp);
wlott's avatar
wlott committed
#endif
wlott's avatar
wlott committed
    arch_do_displaced_inst(scp, orig_inst);
}

rtoy's avatar
rtoy committed
#if !defined(i386)
static lispobj
find_code(os_context_t * scp)
wlott's avatar
wlott committed
{
wlott's avatar
wlott committed
#ifdef reg_CODE
    lispobj code = SC_REG(scp, reg_CODE), header;
wlott's avatar
wlott committed

    if (LowtagOf(code) != type_OtherPointer)
	return NIL;

    header = *(lispobj *) (code - type_OtherPointer);
wlott's avatar
wlott committed

    if (TypeOf(header) == type_CodeHeader)
	return code;
    else
	return code - HeaderValue(header) * sizeof(lispobj);
wlott's avatar
wlott committed
#else
    return NIL;
#endif
}
rtoy's avatar
rtoy committed
#if defined(i386)
static lispobj
find_code(os_context_t * scp)
cshapiro's avatar
cshapiro committed
    lispobj *codeptr = component_ptr_from_pc((lispobj *) SC_PC(scp));
    if (codeptr == NULL)
	return NIL;
    else
	return (lispobj) codeptr | type_OtherPointer;
wlott's avatar
wlott committed

#if (defined(DARWIN) && defined(__ppc__)) || (defined(sparc))
/*
 * During a function-end-breakpoint, the pc is sometimes less than the
 * code address, which bypasses the function end stuff.  Then the
 * offset is zero for a function-end-breakpoint, and we can't find the
 * breakpoint data, causing an error during tracing.  But we know this
 * is a function-end breakpoint, because function_end is set to true.
 *
 * (This condition of pc < code address seems to occur only if a GC
 * happens during tracing.  I guess the function-end code object
 * sometimes gets moved to a lower address than the corresponding
 * code.)
 *
 * Hence this replacement looks at the function end flag first
 * to see if it is a function-end breakpoint and does the function-end
 * stuff anyway.  If not, we do the normal stuff.
 */
static int
compute_offset(os_context_t * scp, lispobj code, boolean function_end)
{
    if (code == NIL)
	return 0;
    if (function_end) {
        /*
         * We're in a function end breakpoint.  Compute the
         * offset from the (known) breakpoint location and the
         * beginning of the breakpoint guts.  (See *-assem.S.)
         *
         * Then make the offset negative so the caller knows
         * that the offset is not from the code object.
         */
        extern char function_end_breakpoint_trap;
        extern char function_end_breakpoint_guts;
        int offset;
            
        offset =
            &function_end_breakpoint_trap -
            &function_end_breakpoint_guts;
#if 0
        fprintf(stderr, "compute_offset\n");
        fprintf(stderr, " function end offset  = %d\n", offset);
#endif
        return make_fixnum(-offset);
    } else {
	unsigned long code_start;
	struct code *codeptr = (struct code *) PTR(code);
	unsigned long pc = SC_PC(scp);

	code_start = (unsigned long) codeptr
	    + HeaderValue(codeptr->header) * sizeof(lispobj);
#if 0
        fprintf(stderr, "compute_offset\n");
        fprintf(stderr, " pc = %d\n", pc);
        fprintf(stderr, " code_start = %d\n", code_start);
        fprintf(stderr, " function_end = %d\n", function_end);
#endif
        if (pc < code_start) {
	    return 0;
        } else {
	    int offset = pc - code_start;

#if 0
            fprintf(stderr, " offset = %d\n", offset);
            fprintf(stderr, " codeptr->code_size = %d\n", codeptr->code_size);
            fprintf(stderr, " function_end = %d\n", function_end);
#endif
	    if (offset >= codeptr->code_size) {
                return 0;
	    } else {
                return make_fixnum(offset);
            }
	}
    }
}
#else
static int
compute_offset(os_context_t * scp, lispobj code, boolean function_end)
wlott's avatar
wlott committed
{
    if (code == NIL)
wlott's avatar
wlott committed
	return 0;
wlott's avatar
wlott committed
    else {
	unsigned long code_start;
	struct code *codeptr = (struct code *) PTR(code);

wlott's avatar
wlott committed
#ifdef parisc
	unsigned long pc = SC_PC(scp) & ~3;
#else
	unsigned long pc = SC_PC(scp);
#endif
wlott's avatar
wlott committed

	code_start = (unsigned long) codeptr
	    + HeaderValue(codeptr->header) * sizeof(lispobj);
wlott's avatar
wlott committed
	if (pc < code_start)
wlott's avatar
wlott committed
	    return 0;
wlott's avatar
wlott committed
	else {
wlott's avatar
wlott committed
	    int offset = pc - code_start;
	    if (offset >= codeptr->code_size) {
		return make_fixnum(offset);
	    }
wlott's avatar
wlott committed
	}
    }
wlott's avatar
wlott committed
}
wlott's avatar
wlott committed

void
handle_breakpoint(int signal, int subcode, os_context_t * scp)
{
    lispobj code;

    fake_foreign_function_call(scp);

    code = find_code(scp);

#if 0
    fprintf(stderr, "handle_breakpoint\n");
    fprintf(stderr, " offset = %d\n", compute_offset(scp, code, 0));
#endif    
    funcall3(SymbolFunction(HANDLE_BREAKPOINT),
	     compute_offset(scp, code, 0), code, alloc_sap(scp));

    undo_fake_foreign_function_call(scp);
}
#else
void
handle_breakpoint(int signal, int subcode, os_context_t * scp)
wlott's avatar
wlott committed
{
    lispobj code, scp_sap = alloc_sap(scp);
wlott's avatar
wlott committed

    fake_foreign_function_call(scp);
wlott's avatar
wlott committed

wlott's avatar
wlott committed
    code = find_code(scp);
dtc's avatar
dtc committed
    /*
     * Don't disallow recursive breakpoint traps.  Otherwise, we can't
     * use debugger breakpoints anywhere in here.
     */

    sigprocmask(SIG_SETMASK, &scp->uc_sigmask, NULL);
wlott's avatar
wlott committed
    funcall3(SymbolFunction(HANDLE_BREAKPOINT),
	     compute_offset(scp, code, 0), code, scp_sap);
wlott's avatar
wlott committed

wlott's avatar
wlott committed
    undo_fake_foreign_function_call(scp);
wlott's avatar
wlott committed
}
wlott's avatar
wlott committed

void *
handle_function_end_breakpoint(int signal, int subcode, os_context_t * scp)
wlott's avatar
wlott committed
{
wlott's avatar
wlott committed
    struct code *codeptr;
wlott's avatar
wlott committed
    fake_foreign_function_call(scp);
wlott's avatar
wlott committed

wlott's avatar
wlott committed
    code = find_code(scp);
    codeptr = (struct code *) PTR(code);
    offset = compute_offset(scp, code, 1);
#if 0
    printf("handle_function_end:\n");
    printf(" code    = 0x%08x\n", code);
    printf(" codeptr = %p\n", codeptr);
    printf(" offset  = %d\n", fixnum_value(offset));
    fflush(stdout);
#endif
    if (offset < 0) {
	/*
	 * We were in the function end breakpoint.  Which means we are
	 * in a bogus LRA, so compute where the code-component of this
	 * bogus lra object starts.  Adjust code, and codeptr
	 * appropriately so the breakpoint handler can do the right
	 * thing.
	 */
	unsigned int pc;

	pc = SC_PC(scp);

	offset = -offset;
	/*
	 * Some magic here.  pc points to the trap instruction.  The
	 * offset gives us where the function_end_breakpoint_guts
	 * begins.  But we need to back up some more to get to the
	 * code-component object.  See MAKE-BOGUS-LRA in
	 * debug-int.lisp
	 */
	code = pc - fixnum_value(offset);
	code -= sizeof(struct code) + BOGUS_LRA_CONSTANTS * sizeof(lispobj);

	code += type_OtherPointer;
	codeptr = (struct code *) PTR(code);
#if 0
	printf("  pc   = 0x%08x\n", pc);
	printf("  code    = 0x%08x\n", code);
	printf("  codeptr = %p\n", codeptr);
	fflush(stdout);
#endif
wlott's avatar
wlott committed
    lra = codeptr->constants[REAL_LRA_SLOT];

    known_return_p = codeptr->constants[KNOWN_RETURN_P_SLOT] != NIL;
rtoy's avatar
rtoy committed
    
    {
	lispobj *args = current_control_stack_pointer;

	/*
	 * Because HANDLE_BREAKPOINT can GC, the LRA could move, and
	 * we need to know where it went so we can return to the
	 * correct place.  We do this by saving the LRA on the Lisp
	 * stack.  If GC moves the LRA, the stack entry will get
	 * updated appropriately too.
	 */
	current_control_stack_pointer += 1;
	args[0] = lra;
rtoy's avatar
rtoy committed

        funcall3(SymbolFunction(HANDLE_BREAKPOINT), offset, code, alloc_sap(scp));

	/*
	 * Breakpoint handling done.  Get the (possibly changed) LRA
	 * value off the stack so we know where to return to.
	 */
	lra = args[0];
	current_control_stack_pointer -= 1;

#ifdef reg_CODE	
	/*
	 * With the known-return convention, we definitely do NOT want
	 * to mangle the CODE register because it isn't pointing to
	 * the bogus LRA but to the actual routine.
	 */
	if (!known_return_p) {
	    SC_REG(scp, reg_CODE) = lra;
	}
rtoy's avatar
rtoy committed
#endif
    }

    undo_fake_foreign_function_call(scp);
    return (void *) (lra - type_OtherPointer + sizeof(lispobj));
void *
handle_function_end_breakpoint(int signal, int subcode, os_context_t * scp)
    lispobj code, scp_sap = alloc_sap(scp);
    struct code *codeptr;

    fake_foreign_function_call(scp);

    code = find_code(scp);
dtc's avatar
dtc committed
    codeptr = (struct code *) PTR(code);

    /*
     * Don't disallow recursive breakpoint traps.  Otherwise, we can't
     * use debugger breakpoints anywhere in here.
     */
    sigprocmask(SIG_SETMASK, &scp->uc_sigmask, NULL);
    funcall3(SymbolFunction(HANDLE_BREAKPOINT),
	     compute_offset(scp, code, 1), code, scp_sap);
wlott's avatar
wlott committed

    undo_fake_foreign_function_call(scp);

    return compute_pc(codeptr->constants[REAL_LRA_SLOT],
dtc's avatar
dtc committed
		      fixnum_value(codeptr->constants[REAL_LRA_SLOT + 1]));
wlott's avatar
wlott committed
}