/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Pstack.c
*
* Common helper functions for stack walking. The ISA-specific code is found in
* Pstack_iter() in Pisadep.c.
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "libproc.h"
#include "Pcontrol.h"
#include "P32ton.h"
#include "Pstack.h"
/*
* Utility function to prevent stack loops from running on forever by
* detecting when there is a stack loop (the %fp has been seen before).
*/
int
{
int i;
for (i = 0; i < nfp; i++) {
return (1); /* stack loop detected */
}
/*
* Just assume there is no loop in the face of allocation
* failure; the caller still has the original prevfp pointer.
*/
return (0);
}
return (0);
}
/*
* Signal Frame Detection
*
* In order to facilitate detection and processing of signal handler frames
* during a stack backtrace, we define a set of utility routines to operate on
* a uclist (ucontext address list), and then use these routines in the various
* implementations of Pstack_iter below. Certain source-level debuggers and
* virtual machines that shall remain nameless believe that in order to detect
* signal handler frames, one must hard-code checks for symbol names defined
* in libc and libthread and knowledge of their implementation. We make no
* such assumptions, allowing us to operate on programs that manipulate their
* underlying kernel signal handlers (i.e. use __sigaction) and to not require
* changes in the face of future library modifications.
*
* A signal handler frame is essentially a set of data pushed on to the user
* stack by the kernel prior to returning to the user program in one of the
* pre-defined signal handlers. The signal handler itself receives the signal
* number, an optional pointer to a siginfo_t, and a pointer to the interrupted
* ucontext as arguments. When performing a stack backtrace, we would like to
* detect these frames so that we can correctly return the interrupted program
* counter and frame pointer as a separate frame. When a signal handler frame
* is constructed on the stack by the kernel, the signalled LWP has its
* lwp_oldcontext member (exported through /proc as lwpstatus.pr_oldcontext)
* set to the user address at which the ucontext_t was placed on the LWP's
* stack. The ucontext_t's uc_link member is set to the previous value of
* lwp_oldcontext. Thus when signal handlers are active, pr_oldcontext will
* point to the first element of a linked list of ucontext_t addresses.
*
* The stack layout for a signal handler frame is as follows:
*
* +--------------+ - high +--------------+ -
* | struct fq | ^ addrs | siginfo_t | optional
* +--------------+ | ^ +--------------+ -
* | gwindows_t | | | ucontext_t | ^
* +--------------+ optional +--------------+ |
* | siginfo_t | | ucontext_t * | |
* +--------------+ | | +--------------+
* | xregs data | v v | siginfo_t * | mandatory
* +--------------+ - low +--------------+
* | ucontext_t | ^ addrs | int (signo) | |
* +--------------+ mandatory +--------------+ |
* | struct frame | v | struct frame | v
* +--------------+ - <- %sp on resume +--------------+ - <- %esp on resume
*
* amd64 (64-bit):
* +--------------+ -
* | siginfo_t | optional
* +--------------+ -
* | ucontext_t | ^
* +--------------+ |
* | siginfo_t * |
* +--------------+ mandatory
* | int (signo) |
* +--------------+ |
* | struct frame | v
* +--------------+ - <- %rsp on resume
*
* The bottom-most struct frame is actually constructed by the kernel by
* copying the previous stack frame, allowing naive backtrace code to simply
* skip over the interrupted frame. The copied frame is never really used,
* since it is presumed the libc or libthread signal handler wrapper function
* will explicitly setcontext(2) to the interrupted context if the user
* program's handler returns. If we detect a signal handler frame, we simply
* read the interrupted context structure from the stack, use its embedded
* gregs to construct the register set for the interrupted frame, and then
* continue our backtrace. Detecting the frame itself is easy according to
* the diagram ("oldcontext" represents any element in the uc_link chain):
*
* On SPARC v7 or v9:
* %fp + sizeof (struct frame) == oldcontext
*
* On Intel ia32:
* %ebp + sizeof (struct frame) + (3 * regsize) == oldcontext
*
* On amd64:
* %rbp + sizeof (struct frame) + (2 * regsize) == oldcontext
*
* A final complication is that we want libproc to support backtraces from
* arbitrary addresses without the caller passing in an LWP id. To do this,
* we must first determine all the known oldcontexts by iterating over all
* LWPs and following their pr_oldcontext pointers. We optimize our search
* by discarding NULL pointers and pointers whose value is less than that
* of the initial stack pointer (since stacks grow down from high memory),
* and then sort the resulting list by virtual address so we can binary search.
*/
int
{
return (0);
for (;;) {
} else
break; /* abort if allocation failure */
}
#ifdef _LP64
break; /* abort if we fail to read ucontext */
} else
#endif
break; /* abort if we fail to read ucontext */
dprintf("detected lwp %d signal context at %p\n",
/*
* Abort if we find a NULL uc_link pointer or a duplicate
* entry which could indicate a cycle or a very peculiar
* interference pattern between threads.
*/
break;
return (0);
}
}
return (0);
}
int
{
return (-1);
return (+1);
return (0);
}
void
{
return;
}
} else {
}
}
void
{
}
int
{
}
return (0);
}