4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2013 by Delphix. All rights reserved.
34 * This file implements the process services declared in <proc_service.h>.
35 * This enables libproc to be used in conjunction with libc_db and
36 * librtld_db. As most of these facilities are already provided by
37 * (more elegant) interfaces in <libproc.h>, we can just call those.
39 * NOTE: We explicitly do *not* implement the functions ps_kill() and
40 * ps_lrolltoaddr() in this library. The very existence of these functions
41 * causes libc_db to create an "agent thread" in the target process.
42 * The only way to turn off this behavior is to omit these functions.
45 #pragma weak ps_pdread = ps_pread
46 #pragma weak ps_ptread = ps_pread
47 #pragma weak ps_pdwrite = ps_pwrite
48 #pragma weak ps_ptwrite = ps_pwrite
51 ps_pdmodel(struct ps_prochandle
*P
, int *modelp
)
53 *modelp
= P
->status
.pr_dmodel
;
58 ps_pread(struct ps_prochandle
*P
, psaddr_t addr
, void *buf
, size_t size
)
60 if (P
->ops
.pop_pread(P
, buf
, size
, addr
, P
->data
) != size
)
66 ps_pwrite(struct ps_prochandle
*P
, psaddr_t addr
, const void *buf
, size_t size
)
68 if (P
->ops
.pop_pwrite(P
, buf
, size
, addr
, P
->data
) != size
)
74 * libc_db calls matched pairs of ps_pstop()/ps_pcontinue()
75 * in the belief that the client may have left the process
76 * running while calling in to the libc_db interfaces.
78 * We interpret the meaning of these functions to be an inquiry
79 * as to whether the process is stopped, not an action to be
80 * performed to make it stopped. For similar reasons, we also
81 * return PS_OK for core files in order to allow libc_db to
82 * operate on these as well.
85 ps_pstop(struct ps_prochandle
*P
)
87 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
93 ps_pcontinue(struct ps_prochandle
*P
)
95 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
101 * ps_lstop() and ps_lcontinue() are not called by any code in libc_db
102 * or librtld_db. We make them behave like ps_pstop() and ps_pcontinue().
106 ps_lstop(struct ps_prochandle
*P
, lwpid_t lwpid
)
108 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
115 ps_lcontinue(struct ps_prochandle
*P
, lwpid_t lwpid
)
117 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
123 ps_lgetregs(struct ps_prochandle
*P
, lwpid_t lwpid
, prgregset_t regs
)
125 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
128 if (Plwp_getregs(P
, lwpid
, regs
) == 0)
135 ps_lsetregs(struct ps_prochandle
*P
, lwpid_t lwpid
, const prgregset_t regs
)
137 if (P
->state
!= PS_STOP
)
140 if (Plwp_setregs(P
, lwpid
, regs
) == 0)
147 ps_lgetfpregs(struct ps_prochandle
*P
, lwpid_t lwpid
, prfpregset_t
*regs
)
149 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
152 if (Plwp_getfpregs(P
, lwpid
, regs
) == 0)
159 ps_lsetfpregs(struct ps_prochandle
*P
, lwpid_t lwpid
, const prfpregset_t
*regs
)
161 if (P
->state
!= PS_STOP
)
164 if (Plwp_setfpregs(P
, lwpid
, regs
) == 0)
170 #if defined(sparc) || defined(__sparc)
173 ps_lgetxregsize(struct ps_prochandle
*P
, lwpid_t lwpid
, int *xrsize
)
175 char fname
[PATH_MAX
];
178 if (P
->state
== PS_DEAD
) {
179 core_info_t
*core
= P
->data
;
180 lwp_info_t
*lwp
= list_next(&core
->core_lwp_head
);
183 for (i
= 0; i
< core
->core_nlwp
; i
++, lwp
= list_next(lwp
)) {
184 if (lwp
->lwp_id
== lwpid
) {
185 if (lwp
->lwp_xregs
!= NULL
)
186 *xrsize
= sizeof (prxregset_t
);
196 (void) snprintf(fname
, sizeof (fname
), "%s/%d/lwp/%d/xregs",
197 procfs_path
, (int)P
->status
.pr_pid
, (int)lwpid
);
199 if (stat(fname
, &statb
) != 0)
202 *xrsize
= (int)statb
.st_size
;
207 ps_lgetxregs(struct ps_prochandle
*P
, lwpid_t lwpid
, caddr_t xregs
)
209 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
212 /* LINTED - alignment */
213 if (Plwp_getxregs(P
, lwpid
, (prxregset_t
*)xregs
) == 0)
220 ps_lsetxregs(struct ps_prochandle
*P
, lwpid_t lwpid
, caddr_t xregs
)
222 if (P
->state
!= PS_STOP
)
225 /* LINTED - alignment */
226 if (Plwp_setxregs(P
, lwpid
, (prxregset_t
*)xregs
) == 0)
234 #if defined(__i386) || defined(__amd64)
237 ps_lgetLDT(struct ps_prochandle
*P
, lwpid_t lwpid
, struct ssd
*ldt
)
239 #if defined(__amd64) && defined(_LP64)
240 if (P
->status
.pr_dmodel
!= PR_MODEL_NATIVE
) {
243 struct ssd
*ldtarray
;
249 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
)
253 * We need to get the ldt entry that matches the
254 * value in the lwp's GS register.
256 if ((error
= ps_lgetregs(P
, lwpid
, regs
)) != PS_OK
)
261 if ((nldt
= Pldt(P
, NULL
, 0)) <= 0 ||
262 (ldtarray
= malloc(nldt
* sizeof (struct ssd
))) == NULL
)
264 if ((nldt
= Pldt(P
, ldtarray
, nldt
)) <= 0) {
269 for (i
= 0; i
< nldt
; i
++) {
270 if (gs
== ldtarray
[i
].sel
) {
279 #if defined(__amd64) && defined(_LP64)
286 #endif /* __i386 || __amd64 */
289 * Libthread_db doesn't use this function currently, but librtld_db uses
290 * it for its debugging output. We turn this on via rd_log if our debugging
291 * switch is on, and then echo the messages sent to ps_plog to stderr.
294 ps_plog(const char *fmt
, ...)
298 if (_libproc_debug
&& fmt
!= NULL
&& *fmt
!= '\0') {
300 (void) vfprintf(stderr
, fmt
, ap
);
302 if (fmt
[strlen(fmt
) - 1] != '\n')
303 (void) fputc('\n', stderr
);
308 * Store a pointer to our internal copy of the aux vector at the address
309 * specified by the caller. It should not hold on to this data for too long.
312 ps_pauxv(struct ps_prochandle
*P
, const auxv_t
**aux
)
320 *aux
= (const auxv_t
*)P
->auxv
;
325 ps_pbrandname(struct ps_prochandle
*P
, char *buf
, size_t len
)
327 return (Pbrandname(P
, buf
, len
) ? PS_OK
: PS_ERR
);
331 * Search for a symbol by name and return the corresponding address.
334 ps_pglobal_lookup(struct ps_prochandle
*P
, const char *object_name
,
335 const char *sym_name
, psaddr_t
*sym_addr
)
339 if (Plookup_by_name(P
, object_name
, sym_name
, &sym
) == 0) {
340 dprintf("pglobal_lookup <%s> -> %p\n",
341 sym_name
, (void *)(uintptr_t)sym
.st_value
);
342 *sym_addr
= (psaddr_t
)sym
.st_value
;
350 * Search for a symbol by name and return the corresponding symbol
351 * information. If we're compiled _LP64, we just call Plookup_by_name
352 * and return because ps_sym_t is defined to be an Elf64_Sym, which
353 * is the same as a GElf_Sym. In the _ILP32 case, we have to convert
354 * Plookup_by_name's result back to a ps_sym_t (which is an Elf32_Sym).
357 ps_pglobal_sym(struct ps_prochandle
*P
, const char *object_name
,
358 const char *sym_name
, ps_sym_t
*symp
)
363 if (Plookup_by_name(P
, object_name
, sym_name
, &sym
) == 0) {
364 symp
->st_name
= (Elf32_Word
)sym
.st_name
;
365 symp
->st_value
= (Elf32_Addr
)sym
.st_value
;
366 symp
->st_size
= (Elf32_Word
)sym
.st_size
;
367 symp
->st_info
= ELF32_ST_INFO(
368 GELF_ST_BIND(sym
.st_info
), GELF_ST_TYPE(sym
.st_info
));
369 symp
->st_other
= sym
.st_other
;
370 symp
->st_shndx
= sym
.st_shndx
;
375 if (Plookup_by_name(P
, object_name
, sym_name
, symp
) == 0)