3946 ::gcore (fix sparc build)
[unleashed.git] / usr / src / lib / libproc / common / Pservice.c
blob03d013364f93a3c2af813425e5cc83278ee697b9
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
29 #include <stdarg.h>
30 #include <string.h>
31 #include "Pcontrol.h"
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
50 ps_err_e
51 ps_pdmodel(struct ps_prochandle *P, int *modelp)
53 *modelp = P->status.pr_dmodel;
54 return (PS_OK);
57 ps_err_e
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)
61 return (PS_BADADDR);
62 return (PS_OK);
65 ps_err_e
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)
69 return (PS_BADADDR);
70 return (PS_OK);
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.
84 ps_err_e
85 ps_pstop(struct ps_prochandle *P)
87 if (P->state != PS_STOP && P->state != PS_DEAD)
88 return (PS_ERR);
89 return (PS_OK);
92 ps_err_e
93 ps_pcontinue(struct ps_prochandle *P)
95 if (P->state != PS_STOP && P->state != PS_DEAD)
96 return (PS_ERR);
97 return (PS_OK);
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().
104 /* ARGSUSED1 */
105 ps_err_e
106 ps_lstop(struct ps_prochandle *P, lwpid_t lwpid)
108 if (P->state != PS_STOP && P->state != PS_DEAD)
109 return (PS_ERR);
110 return (PS_OK);
113 /* ARGSUSED1 */
114 ps_err_e
115 ps_lcontinue(struct ps_prochandle *P, lwpid_t lwpid)
117 if (P->state != PS_STOP && P->state != PS_DEAD)
118 return (PS_ERR);
119 return (PS_OK);
122 ps_err_e
123 ps_lgetregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t regs)
125 if (P->state != PS_STOP && P->state != PS_DEAD)
126 return (PS_ERR);
128 if (Plwp_getregs(P, lwpid, regs) == 0)
129 return (PS_OK);
131 return (PS_BADLID);
134 ps_err_e
135 ps_lsetregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t regs)
137 if (P->state != PS_STOP)
138 return (PS_ERR);
140 if (Plwp_setregs(P, lwpid, regs) == 0)
141 return (PS_OK);
143 return (PS_BADLID);
146 ps_err_e
147 ps_lgetfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *regs)
149 if (P->state != PS_STOP && P->state != PS_DEAD)
150 return (PS_ERR);
152 if (Plwp_getfpregs(P, lwpid, regs) == 0)
153 return (PS_OK);
155 return (PS_BADLID);
158 ps_err_e
159 ps_lsetfpregs(struct ps_prochandle *P, lwpid_t lwpid, const prfpregset_t *regs)
161 if (P->state != PS_STOP)
162 return (PS_ERR);
164 if (Plwp_setfpregs(P, lwpid, regs) == 0)
165 return (PS_OK);
167 return (PS_BADLID);
170 #if defined(sparc) || defined(__sparc)
172 ps_err_e
173 ps_lgetxregsize(struct ps_prochandle *P, lwpid_t lwpid, int *xrsize)
175 char fname[PATH_MAX];
176 struct stat statb;
178 if (P->state == PS_DEAD) {
179 core_info_t *core = P->data;
180 lwp_info_t *lwp = list_next(&core->core_lwp_head);
181 uint_t i;
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);
187 else
188 *xrsize = 0;
189 return (PS_OK);
193 return (PS_BADLID);
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)
200 return (PS_BADLID);
202 *xrsize = (int)statb.st_size;
203 return (PS_OK);
206 ps_err_e
207 ps_lgetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
209 if (P->state != PS_STOP && P->state != PS_DEAD)
210 return (PS_ERR);
212 /* LINTED - alignment */
213 if (Plwp_getxregs(P, lwpid, (prxregset_t *)xregs) == 0)
214 return (PS_OK);
216 return (PS_BADLID);
219 ps_err_e
220 ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
222 if (P->state != PS_STOP)
223 return (PS_ERR);
225 /* LINTED - alignment */
226 if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs) == 0)
227 return (PS_OK);
229 return (PS_BADLID);
232 #endif /* sparc */
234 #if defined(__i386) || defined(__amd64)
236 ps_err_e
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) {
241 #endif
242 prgregset_t regs;
243 struct ssd *ldtarray;
244 ps_err_e error;
245 uint_t gs;
246 int nldt;
247 int i;
249 if (P->state != PS_STOP && P->state != PS_DEAD)
250 return (PS_ERR);
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)
257 return (error);
259 gs = regs[GS];
261 if ((nldt = Pldt(P, NULL, 0)) <= 0 ||
262 (ldtarray = malloc(nldt * sizeof (struct ssd))) == NULL)
263 return (PS_ERR);
264 if ((nldt = Pldt(P, ldtarray, nldt)) <= 0) {
265 free(ldtarray);
266 return (PS_ERR);
269 for (i = 0; i < nldt; i++) {
270 if (gs == ldtarray[i].sel) {
271 *ldt = ldtarray[i];
272 break;
275 free(ldtarray);
277 if (i < nldt)
278 return (PS_OK);
279 #if defined(__amd64) && defined(_LP64)
281 #endif
283 return (PS_ERR);
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.
293 void
294 ps_plog(const char *fmt, ...)
296 va_list ap;
298 if (_libproc_debug && fmt != NULL && *fmt != '\0') {
299 va_start(ap, fmt);
300 (void) vfprintf(stderr, fmt, ap);
301 va_end(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.
311 ps_err_e
312 ps_pauxv(struct ps_prochandle *P, const auxv_t **aux)
314 if (P->auxv == NULL)
315 Preadauxvec(P);
317 if (P->auxv == NULL)
318 return (PS_ERR);
320 *aux = (const auxv_t *)P->auxv;
321 return (PS_OK);
324 ps_err_e
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.
333 ps_err_e
334 ps_pglobal_lookup(struct ps_prochandle *P, const char *object_name,
335 const char *sym_name, psaddr_t *sym_addr)
337 GElf_Sym sym;
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;
343 return (PS_OK);
346 return (PS_NOSYM);
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).
356 ps_err_e
357 ps_pglobal_sym(struct ps_prochandle *P, const char *object_name,
358 const char *sym_name, ps_sym_t *symp)
360 #if defined(_ILP32)
361 GElf_Sym sym;
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;
371 return (PS_OK);
374 #elif defined(_LP64)
375 if (Plookup_by_name(P, object_name, sym_name, symp) == 0)
376 return (PS_OK);
377 #endif
378 return (PS_NOSYM);