dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libproc / common / Plwpregs.c
blobc2b150000fac1eb06d8729c6717c9609368af62a
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.
27 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2013 by Delphix. All rights reserved.
31 #include <sys/types.h>
32 #include <sys/uio.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <limits.h>
37 #include "Pcontrol.h"
38 #include "P32ton.h"
41 * This file implements the routines to read and write per-lwp register
42 * information from either a live process or core file opened with libproc.
43 * We build up a few common routines for reading and writing register
44 * information, and then the public functions are all trivial calls to these.
48 * Utility function to return a pointer to the structure of cached information
49 * about an lwp in the core file, given its lwpid.
51 static lwp_info_t *
52 getlwpcore(struct ps_prochandle *P, lwpid_t lwpid)
54 core_info_t *core = P->data;
55 lwp_info_t *lwp = list_next(&core->core_lwp_head);
56 uint_t i;
58 for (i = 0; i < core->core_nlwp; i++, lwp = list_next(lwp)) {
59 if (lwp->lwp_id == lwpid)
60 return (lwp);
63 errno = EINVAL;
64 return (NULL);
68 * Utility function to open and read the contents of a per-lwp /proc file.
69 * This function is used to slurp in lwpstatus, xregs, and asrs.
71 static int
72 getlwpfile(struct ps_prochandle *P, lwpid_t lwpid,
73 const char *fbase, void *rp, size_t n)
75 char fname[PATH_MAX];
76 int fd;
78 (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/%s",
79 procfs_path, (int)P->status.pr_pid, (int)lwpid, fbase);
81 if ((fd = open(fname, O_RDONLY)) >= 0) {
82 if (read(fd, rp, n) > 0) {
83 (void) close(fd);
84 return (0);
86 (void) close(fd);
88 return (-1);
92 * Get the lwpstatus_t for an lwp from either the live process or our
93 * cached information from the core file. This is used to get the
94 * general-purpose registers or floating point registers.
96 int
97 getlwpstatus(struct ps_prochandle *P, lwpid_t lwpid, lwpstatus_t *lps)
99 lwp_info_t *lwp;
102 * For both live processes and cores, our job is easy if the lwpid
103 * matches that of the representative lwp:
105 if (P->status.pr_lwp.pr_lwpid == lwpid) {
106 (void) memcpy(lps, &P->status.pr_lwp, sizeof (lwpstatus_t));
107 return (0);
111 * If this is a live process, then just read the information out
112 * of the per-lwp status file:
114 if (P->state != PS_DEAD) {
115 return (getlwpfile(P, lwpid, "lwpstatus",
116 lps, sizeof (lwpstatus_t)));
120 * If this is a core file, we need to iterate through our list of
121 * cached lwp information and then copy out the status.
123 if (P->data != NULL && (lwp = getlwpcore(P, lwpid)) != NULL) {
124 (void) memcpy(lps, &lwp->lwp_status, sizeof (lwpstatus_t));
125 return (0);
128 return (-1);
132 * Utility function to modify lwp registers. This is done using either the
133 * process control file or per-lwp control file as necessary.
135 static int
136 setlwpregs(struct ps_prochandle *P, lwpid_t lwpid, long cmd,
137 const void *rp, size_t n)
139 iovec_t iov[2];
140 char fname[PATH_MAX];
141 int fd;
143 if (P->state != PS_STOP) {
144 errno = EBUSY;
145 return (-1);
148 iov[0].iov_base = (caddr_t)&cmd;
149 iov[0].iov_len = sizeof (long);
150 iov[1].iov_base = (caddr_t)rp;
151 iov[1].iov_len = n;
154 * Writing the process control file writes the representative lwp.
155 * Psync before we write to make sure we are consistent with the
156 * primary interfaces. Similarly, make sure to update P->status
157 * afterward if we are modifying one of its register sets.
159 if (P->status.pr_lwp.pr_lwpid == lwpid) {
160 Psync(P);
162 if (writev(P->ctlfd, iov, 2) == -1)
163 return (-1);
165 if (cmd == PCSREG)
166 (void) memcpy(P->status.pr_lwp.pr_reg, rp, n);
167 else if (cmd == PCSFPREG)
168 (void) memcpy(&P->status.pr_lwp.pr_fpreg, rp, n);
170 return (0);
174 * If the lwp we want is not the representative lwp, we need to
175 * open the ctl file for that specific lwp.
177 (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/lwpctl",
178 procfs_path, (int)P->status.pr_pid, (int)lwpid);
180 if ((fd = open(fname, O_WRONLY)) >= 0) {
181 if (writev(fd, iov, 2) > 0) {
182 (void) close(fd);
183 return (0);
185 (void) close(fd);
187 return (-1);
191 Plwp_getregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t gregs)
193 lwpstatus_t lps;
195 if (getlwpstatus(P, lwpid, &lps) == -1)
196 return (-1);
198 (void) memcpy(gregs, lps.pr_reg, sizeof (prgregset_t));
199 return (0);
203 Plwp_setregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t gregs)
205 return (setlwpregs(P, lwpid, PCSREG, gregs, sizeof (prgregset_t)));
209 Plwp_getfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *fpregs)
211 lwpstatus_t lps;
213 if (getlwpstatus(P, lwpid, &lps) == -1)
214 return (-1);
216 (void) memcpy(fpregs, &lps.pr_fpreg, sizeof (prfpregset_t));
217 return (0);
220 int Plwp_setfpregs(struct ps_prochandle *P, lwpid_t lwpid,
221 const prfpregset_t *fpregs)
223 return (setlwpregs(P, lwpid, PCSFPREG, fpregs, sizeof (prfpregset_t)));
226 #if defined(sparc) || defined(__sparc)
228 Plwp_getxregs(struct ps_prochandle *P, lwpid_t lwpid, prxregset_t *xregs)
230 lwp_info_t *lwp;
232 if (P->state == PS_IDLE) {
233 errno = ENODATA;
234 return (-1);
237 if (P->state != PS_DEAD) {
238 if (P->state != PS_STOP) {
239 errno = EBUSY;
240 return (-1);
243 return (getlwpfile(P, lwpid, "xregs",
244 xregs, sizeof (prxregset_t)));
247 if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_xregs != NULL) {
248 (void) memcpy(xregs, lwp->lwp_xregs, sizeof (prxregset_t));
249 return (0);
252 if (lwp != NULL)
253 errno = ENODATA;
254 return (-1);
258 Plwp_setxregs(struct ps_prochandle *P, lwpid_t lwpid, const prxregset_t *xregs)
260 return (setlwpregs(P, lwpid, PCSXREG, xregs, sizeof (prxregset_t)));
264 Plwp_getgwindows(struct ps_prochandle *P, lwpid_t lwpid, gwindows_t *gwins)
266 lwp_info_t *lwp;
268 if (P->state == PS_IDLE) {
269 errno = ENODATA;
270 return (-1);
273 if (P->state != PS_DEAD) {
274 if (P->state != PS_STOP) {
275 errno = EBUSY;
276 return (-1);
279 return (getlwpfile(P, lwpid, "gwindows",
280 gwins, sizeof (gwindows_t)));
283 if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_gwins != NULL) {
284 *gwins = *lwp->lwp_gwins;
285 return (0);
288 if (lwp != NULL)
289 errno = ENODATA;
290 return (-1);
293 #if defined(__sparcv9)
295 Plwp_getasrs(struct ps_prochandle *P, lwpid_t lwpid, asrset_t asrs)
297 lwp_info_t *lwp;
299 if (P->state == PS_IDLE) {
300 errno = ENODATA;
301 return (-1);
304 if (P->state != PS_DEAD) {
305 if (P->state != PS_STOP) {
306 errno = EBUSY;
307 return (-1);
310 return (getlwpfile(P, lwpid, "asrs", asrs, sizeof (asrset_t)));
313 if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_asrs != NULL) {
314 (void) memcpy(asrs, lwp->lwp_asrs, sizeof (asrset_t));
315 return (0);
318 if (lwp != NULL)
319 errno = ENODATA;
320 return (-1);
325 Plwp_setasrs(struct ps_prochandle *P, lwpid_t lwpid, const asrset_t asrs)
327 return (setlwpregs(P, lwpid, PCSASRS, asrs, sizeof (asrset_t)));
329 #endif /* __sparcv9 */
330 #endif /* __sparc */
333 Plwp_getpsinfo(struct ps_prochandle *P, lwpid_t lwpid, lwpsinfo_t *lps)
335 lwp_info_t *lwp;
337 if (P->state == PS_IDLE) {
338 errno = ENODATA;
339 return (-1);
342 if (P->state != PS_DEAD) {
343 return (getlwpfile(P, lwpid, "lwpsinfo",
344 lps, sizeof (lwpsinfo_t)));
347 if ((lwp = getlwpcore(P, lwpid)) != NULL) {
348 (void) memcpy(lps, &lwp->lwp_psinfo, sizeof (lwpsinfo_t));
349 return (0);
352 return (-1);
356 Plwp_getspymaster(struct ps_prochandle *P, lwpid_t lwpid, psinfo_t *ps)
358 lwpstatus_t lps;
360 if (P->state == PS_IDLE) {
361 errno = ENODATA;
362 return (-1);
365 if (getlwpstatus(P, lwpid, &lps) != 0)
366 return (-1);
368 if (!(lps.pr_flags & PR_AGENT)) {
369 errno = EINVAL;
370 return (-1);
373 if (P->state != PS_DEAD) {
374 return (getlwpfile(P, lwpid, "spymaster",
375 ps, sizeof (psinfo_t)));
378 if (P->spymaster.pr_nlwp != 0) {
379 (void) memcpy(ps, &P->spymaster, sizeof (psinfo_t));
380 return (0);
383 errno = ENODATA;
385 return (-1);
389 Plwp_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
391 uintptr_t addr;
393 if (P->state == PS_IDLE) {
394 errno = ENODATA;
395 return (-1);
398 if (P->state != PS_DEAD) {
399 lwpstatus_t ls;
400 if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
401 return (-1);
402 addr = ls.pr_ustack;
403 } else {
404 lwp_info_t *lwp;
405 if ((lwp = getlwpcore(P, lwpid)) == NULL)
406 return (-1);
407 addr = lwp->lwp_status.pr_ustack;
411 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
412 if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
413 return (-1);
414 #ifdef _LP64
415 } else {
416 stack32_t stk32;
418 if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
419 return (-1);
421 stack_32_to_n(&stk32, stkp);
422 #endif
425 return (0);
429 Plwp_main_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
431 uintptr_t addr;
432 lwpstatus_t ls;
434 if (P->state == PS_IDLE) {
435 errno = ENODATA;
436 return (-1);
439 if (P->state != PS_DEAD) {
440 if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
441 return (-1);
442 } else {
443 lwp_info_t *lwp;
444 if ((lwp = getlwpcore(P, lwpid)) == NULL)
445 return (-1);
446 ls = lwp->lwp_status;
449 addr = ls.pr_ustack;
452 * Read out the current stack; if the SS_ONSTACK flag is set then
453 * this LWP is operating on the alternate signal stack. We can
454 * recover the original stack from pr_oldcontext.
456 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
457 if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
458 return (-1);
460 if (stkp->ss_flags & SS_ONSTACK)
461 goto on_altstack;
462 #ifdef _LP64
463 } else {
464 stack32_t stk32;
466 if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
467 return (-1);
469 if (stk32.ss_flags & SS_ONSTACK)
470 goto on_altstack;
472 stack_32_to_n(&stk32, stkp);
473 #endif
476 return (0);
478 on_altstack:
480 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
481 ucontext_t *ctxp = (void *)ls.pr_oldcontext;
483 if (Pread(P, stkp, sizeof (*stkp),
484 (uintptr_t)&ctxp->uc_stack) != sizeof (*stkp))
485 return (-1);
486 #ifdef _LP64
487 } else {
488 ucontext32_t *ctxp = (void *)ls.pr_oldcontext;
489 stack32_t stk32;
491 if (Pread(P, &stk32, sizeof (stk32),
492 (uintptr_t)&ctxp->uc_stack) != sizeof (stk32))
493 return (-1);
495 stack_32_to_n(&stk32, stkp);
496 #endif
499 return (0);
503 Plwp_alt_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
505 if (P->state == PS_IDLE) {
506 errno = ENODATA;
507 return (-1);
510 if (P->state != PS_DEAD) {
511 lwpstatus_t ls;
513 if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
514 return (-1);
516 if (ls.pr_altstack.ss_flags & SS_DISABLE) {
517 errno = ENODATA;
518 return (-1);
521 *stkp = ls.pr_altstack;
522 } else {
523 lwp_info_t *lwp;
525 if ((lwp = getlwpcore(P, lwpid)) == NULL)
526 return (-1);
528 if (lwp->lwp_status.pr_altstack.ss_flags & SS_DISABLE) {
529 errno = ENODATA;
530 return (-1);
533 *stkp = lwp->lwp_status.pr_altstack;
536 return (0);