/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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
*/
/*
*/
/*
* 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 and number detection.
*
* For the benefit of the various ISA-specific implementations of Pstack_iter()
* we define a set of utility routines that operate on a uclist (ucontext
* address list). These routines enable the detection and processing of signal
* frames without any private knowledge of the userland signal implementation.
* This allows us to examine programs that manipulate their underlying kernel
* signal handlers (i.e. use __sigaction) and insulates us from possible future
* library changes.
*
* 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
* +--------------+ | ^
* | gwindows_t | |
* +--------------+ optional
* | siginfo_t |
* +--------------+ | |
* | xregs data | v v
* +--------------+ - low
* | ucontext_t | ^ addrs
* +--------------+ mandatory
* | struct frame | v
* +--------------+ - <- %sp on resume
*
* Intel ia32:
* +--------------+ -
* | siginfo_t | ^
* +--------------+ optional
* | xregs data | v
* +--------------+ -
* | ucontext_t | ^
* +--------------+ |
* | ucontext_t * | |
* +--------------+
* | siginfo_t * | mandatory
* +--------------+
* | int (signo) | |
* +--------------+ |
* | struct frame | v
* +--------------+ - <- %esp on resume
*
* amd64 (64-bit):
* +--------------+ -
* | siginfo_t | ^
* +--------------+ optional
* | xregs data | v
* +--------------+ -
* | 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.
*
* On the x86 architectures the signal number is trivial to obtain since its
* presence in the signal frame is mandated by the corresponding ABI.
* Unfortunately, SPARC enjoys no such convenience: although we know that the
* handler receives the signal number as its first argument, a compiler is
* free to do as it pleases with the function's registers. Not all is lost,
* however. PSARC/1999/024 specifies (amongst other things) that the default
* signal-handling implementation will include a call to __sighndlr(); this
* function is written by hand so that the signal number, which it is passed as
* its first argument, may be recovered reliably.
*/
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);
}