xref: /illumos-gate/usr/src/lib/libproc/common/Pservice.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229acbbeafSnn35248  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
252a12f85aSJeremy Jones /*
262a12f85aSJeremy Jones  * Copyright (c) 2013 by Delphix. All rights reserved.
27*ed093b41SRobert Mustacchi  * Copyright 2023 Oxide Computer Company
282a12f85aSJeremy Jones  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdarg.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
32*ed093b41SRobert Mustacchi #include <errno.h>
337c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * This file implements the process services declared in <proc_service.h>.
377c478bd9Sstevel@tonic-gate  * This enables libproc to be used in conjunction with libc_db and
387c478bd9Sstevel@tonic-gate  * librtld_db.  As most of these facilities are already provided by
397c478bd9Sstevel@tonic-gate  * (more elegant) interfaces in <libproc.h>, we can just call those.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * NOTE: We explicitly do *not* implement the functions ps_kill() and
427c478bd9Sstevel@tonic-gate  * ps_lrolltoaddr() in this library.  The very existence of these functions
437c478bd9Sstevel@tonic-gate  * causes libc_db to create an "agent thread" in the target process.
447c478bd9Sstevel@tonic-gate  * The only way to turn off this behavior is to omit these functions.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #pragma weak ps_pdread = ps_pread
487c478bd9Sstevel@tonic-gate #pragma weak ps_ptread = ps_pread
497c478bd9Sstevel@tonic-gate #pragma weak ps_pdwrite = ps_pwrite
507c478bd9Sstevel@tonic-gate #pragma weak ps_ptwrite = ps_pwrite
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate ps_err_e
ps_pdmodel(struct ps_prochandle * P,int * modelp)537c478bd9Sstevel@tonic-gate ps_pdmodel(struct ps_prochandle *P, int *modelp)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	*modelp = P->status.pr_dmodel;
567c478bd9Sstevel@tonic-gate 	return (PS_OK);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate ps_err_e
ps_pread(struct ps_prochandle * P,psaddr_t addr,void * buf,size_t size)607c478bd9Sstevel@tonic-gate ps_pread(struct ps_prochandle *P, psaddr_t addr, void *buf, size_t size)
617c478bd9Sstevel@tonic-gate {
622a12f85aSJeremy Jones 	if (P->ops.pop_pread(P, buf, size, addr, P->data) != size)
637c478bd9Sstevel@tonic-gate 		return (PS_BADADDR);
647c478bd9Sstevel@tonic-gate 	return (PS_OK);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate ps_err_e
ps_pwrite(struct ps_prochandle * P,psaddr_t addr,const void * buf,size_t size)687c478bd9Sstevel@tonic-gate ps_pwrite(struct ps_prochandle *P, psaddr_t addr, const void *buf, size_t size)
697c478bd9Sstevel@tonic-gate {
702a12f85aSJeremy Jones 	if (P->ops.pop_pwrite(P, buf, size, addr, P->data) != size)
717c478bd9Sstevel@tonic-gate 		return (PS_BADADDR);
727c478bd9Sstevel@tonic-gate 	return (PS_OK);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * libc_db calls matched pairs of ps_pstop()/ps_pcontinue()
777c478bd9Sstevel@tonic-gate  * in the belief that the client may have left the process
787c478bd9Sstevel@tonic-gate  * running while calling in to the libc_db interfaces.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * We interpret the meaning of these functions to be an inquiry
817c478bd9Sstevel@tonic-gate  * as to whether the process is stopped, not an action to be
827c478bd9Sstevel@tonic-gate  * performed to make it stopped.  For similar reasons, we also
837c478bd9Sstevel@tonic-gate  * return PS_OK for core files in order to allow libc_db to
847c478bd9Sstevel@tonic-gate  * operate on these as well.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate ps_err_e
ps_pstop(struct ps_prochandle * P)877c478bd9Sstevel@tonic-gate ps_pstop(struct ps_prochandle *P)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
907c478bd9Sstevel@tonic-gate 		return (PS_ERR);
917c478bd9Sstevel@tonic-gate 	return (PS_OK);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate ps_err_e
ps_pcontinue(struct ps_prochandle * P)957c478bd9Sstevel@tonic-gate ps_pcontinue(struct ps_prochandle *P)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
987c478bd9Sstevel@tonic-gate 		return (PS_ERR);
997c478bd9Sstevel@tonic-gate 	return (PS_OK);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * ps_lstop() and ps_lcontinue() are not called by any code in libc_db
1047c478bd9Sstevel@tonic-gate  * or librtld_db.  We make them behave like ps_pstop() and ps_pcontinue().
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1077c478bd9Sstevel@tonic-gate ps_err_e
ps_lstop(struct ps_prochandle * P,lwpid_t lwpid)1087c478bd9Sstevel@tonic-gate ps_lstop(struct ps_prochandle *P, lwpid_t lwpid)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
1117c478bd9Sstevel@tonic-gate 		return (PS_ERR);
1127c478bd9Sstevel@tonic-gate 	return (PS_OK);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1167c478bd9Sstevel@tonic-gate ps_err_e
ps_lcontinue(struct ps_prochandle * P,lwpid_t lwpid)1177c478bd9Sstevel@tonic-gate ps_lcontinue(struct ps_prochandle *P, lwpid_t lwpid)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
1207c478bd9Sstevel@tonic-gate 		return (PS_ERR);
1217c478bd9Sstevel@tonic-gate 	return (PS_OK);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate ps_err_e
ps_lgetregs(struct ps_prochandle * P,lwpid_t lwpid,prgregset_t regs)1257c478bd9Sstevel@tonic-gate ps_lgetregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t regs)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
1287c478bd9Sstevel@tonic-gate 		return (PS_ERR);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (Plwp_getregs(P, lwpid, regs) == 0)
1317c478bd9Sstevel@tonic-gate 		return (PS_OK);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (PS_BADLID);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate ps_err_e
ps_lsetregs(struct ps_prochandle * P,lwpid_t lwpid,const prgregset_t regs)1377c478bd9Sstevel@tonic-gate ps_lsetregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t regs)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP)
1407c478bd9Sstevel@tonic-gate 		return (PS_ERR);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (Plwp_setregs(P, lwpid, regs) == 0)
1437c478bd9Sstevel@tonic-gate 		return (PS_OK);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (PS_BADLID);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate ps_err_e
ps_lgetfpregs(struct ps_prochandle * P,lwpid_t lwpid,prfpregset_t * regs)1497c478bd9Sstevel@tonic-gate ps_lgetfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *regs)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
1527c478bd9Sstevel@tonic-gate 		return (PS_ERR);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (Plwp_getfpregs(P, lwpid, regs) == 0)
1557c478bd9Sstevel@tonic-gate 		return (PS_OK);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	return (PS_BADLID);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate ps_err_e
ps_lsetfpregs(struct ps_prochandle * P,lwpid_t lwpid,const prfpregset_t * regs)1617c478bd9Sstevel@tonic-gate ps_lsetfpregs(struct ps_prochandle *P, lwpid_t lwpid, const prfpregset_t *regs)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP)
1647c478bd9Sstevel@tonic-gate 		return (PS_ERR);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (Plwp_setfpregs(P, lwpid, regs) == 0)
1677c478bd9Sstevel@tonic-gate 		return (PS_OK);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (PS_BADLID);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate ps_err_e
ps_lgetxregsize(struct ps_prochandle * P,lwpid_t lwpid,int * xrsize)1737c478bd9Sstevel@tonic-gate ps_lgetxregsize(struct ps_prochandle *P, lwpid_t lwpid, int *xrsize)
1747c478bd9Sstevel@tonic-gate {
1759acbbeafSnn35248 	char fname[PATH_MAX];
1767c478bd9Sstevel@tonic-gate 	struct stat statb;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
17969a119caSChristopher Siden 		core_info_t *core = P->data;
18050d4d24eSRobert Mustacchi 		lwp_info_t *lwp;
1817c478bd9Sstevel@tonic-gate 
18250d4d24eSRobert Mustacchi 		for (lwp = list_head(&core->core_lwp_head); lwp != NULL;
18350d4d24eSRobert Mustacchi 		    lwp = list_next(&core->core_lwp_head, lwp)) {
1847c478bd9Sstevel@tonic-gate 			if (lwp->lwp_id == lwpid) {
185*ed093b41SRobert Mustacchi 				if (lwp->lwp_xregs != NULL &&
186*ed093b41SRobert Mustacchi 				    lwp->lwp_xregsize > 0) {
187*ed093b41SRobert Mustacchi 					if (lwp->lwp_xregsize >= INT_MAX) {
188*ed093b41SRobert Mustacchi 						return (PS_ERR);
189*ed093b41SRobert Mustacchi 					}
190*ed093b41SRobert Mustacchi 
191*ed093b41SRobert Mustacchi 					*xrsize = (int)lwp->lwp_xregsize;
192*ed093b41SRobert Mustacchi 				} else {
1937c478bd9Sstevel@tonic-gate 					*xrsize = 0;
194*ed093b41SRobert Mustacchi 				}
1957c478bd9Sstevel@tonic-gate 				return (PS_OK);
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		return (PS_BADLID);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2029acbbeafSnn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/xregs",
2039acbbeafSnn35248 	    procfs_path, (int)P->status.pr_pid, (int)lwpid);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (stat(fname, &statb) != 0)
2067c478bd9Sstevel@tonic-gate 		return (PS_BADLID);
2077c478bd9Sstevel@tonic-gate 
208*ed093b41SRobert Mustacchi 	if (statb.st_size > INT_MAX)
209*ed093b41SRobert Mustacchi 		return (PS_ERR);
210*ed093b41SRobert Mustacchi 
2117c478bd9Sstevel@tonic-gate 	*xrsize = (int)statb.st_size;
2127c478bd9Sstevel@tonic-gate 	return (PS_OK);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate ps_err_e
ps_lgetxregs(struct ps_prochandle * P,lwpid_t lwpid,caddr_t xregs)2167c478bd9Sstevel@tonic-gate ps_lgetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
2177c478bd9Sstevel@tonic-gate {
218*ed093b41SRobert Mustacchi 	size_t xregsize;
219*ed093b41SRobert Mustacchi 	prxregset_t *prx;
220*ed093b41SRobert Mustacchi 
2217c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
2227c478bd9Sstevel@tonic-gate 		return (PS_ERR);
2237c478bd9Sstevel@tonic-gate 
224*ed093b41SRobert Mustacchi 	if (Plwp_getxregs(P, lwpid, &prx, &xregsize) == 0) {
225*ed093b41SRobert Mustacchi 		(void) memcpy(xregs, prx, xregsize);
226*ed093b41SRobert Mustacchi 		Plwp_freexregs(P, prx, xregsize);
2277c478bd9Sstevel@tonic-gate 		return (PS_OK);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
230*ed093b41SRobert Mustacchi 	if (errno == ENODATA)
231*ed093b41SRobert Mustacchi 		return (PS_NOXREGS);
232*ed093b41SRobert Mustacchi 	else if (errno == ENOENT)
233*ed093b41SRobert Mustacchi 		return (PS_BADLID);
234*ed093b41SRobert Mustacchi 	return (PS_ERR);
235*ed093b41SRobert Mustacchi }
236*ed093b41SRobert Mustacchi 
237*ed093b41SRobert Mustacchi ps_err_e
ps_lsetxregs(struct ps_prochandle * P,lwpid_t lwpid,caddr_t xregs)238*ed093b41SRobert Mustacchi ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
239*ed093b41SRobert Mustacchi {
240*ed093b41SRobert Mustacchi 	size_t xregsize = 0;
241*ed093b41SRobert Mustacchi 
242*ed093b41SRobert Mustacchi 	if (P->state != PS_STOP)
243*ed093b41SRobert Mustacchi 		return (PS_ERR);
244*ed093b41SRobert Mustacchi 
245*ed093b41SRobert Mustacchi 	/*
246*ed093b41SRobert Mustacchi 	 * libproc asks the caller for the size of the extended register set.
247*ed093b41SRobert Mustacchi 	 * Unfortunately, right now we aren't given the actual size of this
248*ed093b41SRobert Mustacchi 	 * ourselves and we don't want to break the ABI that folks have used
249*ed093b41SRobert Mustacchi 	 * historically. Therefore, we reach in and ask the structure in a
250*ed093b41SRobert Mustacchi 	 * platform-specific way about what this should be. Sorry, this is a bit
251*ed093b41SRobert Mustacchi 	 * unfortunate. This really shouldn't be a platform-specific #ifdef.
252*ed093b41SRobert Mustacchi 	 *
253*ed093b41SRobert Mustacchi 	 * Platforms without xregs can leave xregsize set to zero the kernel
254*ed093b41SRobert Mustacchi 	 * will fail with EINVAL.
255*ed093b41SRobert Mustacchi 	 */
256*ed093b41SRobert Mustacchi #if defined(__i386) || defined(__amd64)
257*ed093b41SRobert Mustacchi 	prxregset_hdr_t *hdr = (prxregset_hdr_t *)xregs;
258*ed093b41SRobert Mustacchi 	xregsize = hdr->pr_size;
259*ed093b41SRobert Mustacchi #endif
260*ed093b41SRobert Mustacchi 
261*ed093b41SRobert Mustacchi 	if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs, xregsize) == 0)
262*ed093b41SRobert Mustacchi 		return (PS_OK);
263*ed093b41SRobert Mustacchi 
264*ed093b41SRobert Mustacchi 	if (errno == ENOENT)
265*ed093b41SRobert Mustacchi 		return (PS_BADLID);
266*ed093b41SRobert Mustacchi 	return (PS_ERR);
267*ed093b41SRobert Mustacchi }
268*ed093b41SRobert Mustacchi 
269*ed093b41SRobert Mustacchi #if defined(sparc) || defined(__sparc)
2707c478bd9Sstevel@tonic-gate ps_err_e
ps_lsetxregs(struct ps_prochandle * P,lwpid_t lwpid,caddr_t xregs)2717c478bd9Sstevel@tonic-gate ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP)
2747c478bd9Sstevel@tonic-gate 		return (PS_ERR);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
2777c478bd9Sstevel@tonic-gate 	if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs) == 0)
2787c478bd9Sstevel@tonic-gate 		return (PS_OK);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (PS_BADLID);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate #endif	/* sparc */
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate ps_err_e
ps_lgetLDT(struct ps_prochandle * P,lwpid_t lwpid,struct ssd * ldt)2887c478bd9Sstevel@tonic-gate ps_lgetLDT(struct ps_prochandle *P, lwpid_t lwpid, struct ssd *ldt)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate #if defined(__amd64) && defined(_LP64)
2917c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel != PR_MODEL_NATIVE) {
2927c478bd9Sstevel@tonic-gate #endif
2937c478bd9Sstevel@tonic-gate 	prgregset_t regs;
2947c478bd9Sstevel@tonic-gate 	struct ssd *ldtarray;
2957c478bd9Sstevel@tonic-gate 	ps_err_e error;
2967c478bd9Sstevel@tonic-gate 	uint_t gs;
2977c478bd9Sstevel@tonic-gate 	int nldt;
2987c478bd9Sstevel@tonic-gate 	int i;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD)
3017c478bd9Sstevel@tonic-gate 		return (PS_ERR);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * We need to get the ldt entry that matches the
3057c478bd9Sstevel@tonic-gate 	 * value in the lwp's GS register.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	if ((error = ps_lgetregs(P, lwpid, regs)) != PS_OK)
3087c478bd9Sstevel@tonic-gate 		return (error);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	gs = regs[GS];
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if ((nldt = Pldt(P, NULL, 0)) <= 0 ||
3137c478bd9Sstevel@tonic-gate 	    (ldtarray = malloc(nldt * sizeof (struct ssd))) == NULL)
3147c478bd9Sstevel@tonic-gate 		return (PS_ERR);
3157c478bd9Sstevel@tonic-gate 	if ((nldt = Pldt(P, ldtarray, nldt)) <= 0) {
3167c478bd9Sstevel@tonic-gate 		free(ldtarray);
3177c478bd9Sstevel@tonic-gate 		return (PS_ERR);
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	for (i = 0; i < nldt; i++) {
3217c478bd9Sstevel@tonic-gate 		if (gs == ldtarray[i].sel) {
3227c478bd9Sstevel@tonic-gate 			*ldt = ldtarray[i];
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	free(ldtarray);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (i < nldt)
3297c478bd9Sstevel@tonic-gate 		return (PS_OK);
3307c478bd9Sstevel@tonic-gate #if defined(__amd64) && defined(_LP64)
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate #endif
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	return (PS_ERR);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate #endif	/* __i386 || __amd64 */
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * Libthread_db doesn't use this function currently, but librtld_db uses
3417c478bd9Sstevel@tonic-gate  * it for its debugging output.  We turn this on via rd_log if our debugging
3427c478bd9Sstevel@tonic-gate  * switch is on, and then echo the messages sent to ps_plog to stderr.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate void
ps_plog(const char * fmt,...)3457c478bd9Sstevel@tonic-gate ps_plog(const char *fmt, ...)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	va_list ap;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (_libproc_debug && fmt != NULL && *fmt != '\0') {
3507c478bd9Sstevel@tonic-gate 		va_start(ap, fmt);
3517c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
3527c478bd9Sstevel@tonic-gate 		va_end(ap);
3537c478bd9Sstevel@tonic-gate 		if (fmt[strlen(fmt) - 1] != '\n')
3547c478bd9Sstevel@tonic-gate 			(void) fputc('\n', stderr);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * Store a pointer to our internal copy of the aux vector at the address
3607c478bd9Sstevel@tonic-gate  * specified by the caller.  It should not hold on to this data for too long.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate ps_err_e
ps_pauxv(struct ps_prochandle * P,const auxv_t ** aux)3637c478bd9Sstevel@tonic-gate ps_pauxv(struct ps_prochandle *P, const auxv_t **aux)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
3667c478bd9Sstevel@tonic-gate 		Preadauxvec(P);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
3697c478bd9Sstevel@tonic-gate 		return (PS_ERR);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	*aux = (const auxv_t *)P->auxv;
3727c478bd9Sstevel@tonic-gate 	return (PS_OK);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3759acbbeafSnn35248 ps_err_e
ps_pbrandname(struct ps_prochandle * P,char * buf,size_t len)3769acbbeafSnn35248 ps_pbrandname(struct ps_prochandle *P, char *buf, size_t len)
3779acbbeafSnn35248 {
3789acbbeafSnn35248 	return (Pbrandname(P, buf, len) ? PS_OK : PS_ERR);
3799acbbeafSnn35248 }
3809acbbeafSnn35248 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Search for a symbol by name and return the corresponding address.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate ps_err_e
ps_pglobal_lookup(struct ps_prochandle * P,const char * object_name,const char * sym_name,psaddr_t * sym_addr)3857c478bd9Sstevel@tonic-gate ps_pglobal_lookup(struct ps_prochandle *P, const char *object_name,
3867c478bd9Sstevel@tonic-gate     const char *sym_name, psaddr_t *sym_addr)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
3917c478bd9Sstevel@tonic-gate 		dprintf("pglobal_lookup <%s> -> %p\n",
3927c478bd9Sstevel@tonic-gate 		    sym_name, (void *)(uintptr_t)sym.st_value);
3937c478bd9Sstevel@tonic-gate 		*sym_addr = (psaddr_t)sym.st_value;
3947c478bd9Sstevel@tonic-gate 		return (PS_OK);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (PS_NOSYM);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * Search for a symbol by name and return the corresponding symbol
4027c478bd9Sstevel@tonic-gate  * information.  If we're compiled _LP64, we just call Plookup_by_name
4037c478bd9Sstevel@tonic-gate  * and return because ps_sym_t is defined to be an Elf64_Sym, which
4047c478bd9Sstevel@tonic-gate  * is the same as a GElf_Sym.  In the _ILP32 case, we have to convert
4057c478bd9Sstevel@tonic-gate  * Plookup_by_name's result back to a ps_sym_t (which is an Elf32_Sym).
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate ps_err_e
ps_pglobal_sym(struct ps_prochandle * P,const char * object_name,const char * sym_name,ps_sym_t * symp)4087c478bd9Sstevel@tonic-gate ps_pglobal_sym(struct ps_prochandle *P, const char *object_name,
4097c478bd9Sstevel@tonic-gate     const char *sym_name, ps_sym_t *symp)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate #if defined(_ILP32)
4127c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
4157c478bd9Sstevel@tonic-gate 		symp->st_name = (Elf32_Word)sym.st_name;
4167c478bd9Sstevel@tonic-gate 		symp->st_value = (Elf32_Addr)sym.st_value;
4177c478bd9Sstevel@tonic-gate 		symp->st_size = (Elf32_Word)sym.st_size;
4187c478bd9Sstevel@tonic-gate 		symp->st_info = ELF32_ST_INFO(
4197c478bd9Sstevel@tonic-gate 		    GELF_ST_BIND(sym.st_info), GELF_ST_TYPE(sym.st_info));
4207c478bd9Sstevel@tonic-gate 		symp->st_other = sym.st_other;
4217c478bd9Sstevel@tonic-gate 		symp->st_shndx = sym.st_shndx;
4227c478bd9Sstevel@tonic-gate 		return (PS_OK);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate #elif defined(_LP64)
4267c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(P, object_name, sym_name, symp) == 0)
4277c478bd9Sstevel@tonic-gate 		return (PS_OK);
4287c478bd9Sstevel@tonic-gate #endif
4297c478bd9Sstevel@tonic-gate 	return (PS_NOSYM);
4307c478bd9Sstevel@tonic-gate }
431