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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Portions Copyright 2007 Chad Mynhier
27 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
28 * Copyright (c) 2013 by Delphix. All rights reserved.
29 * Copyright 2015, Joyent, Inc.
47 #include <sys/types.h>
50 #include <sys/resource.h>
51 #include <sys/param.h>
52 #include <sys/stack.h>
53 #include <sys/fault.h>
54 #include <sys/syscall.h>
55 #include <sys/sysmacros.h>
56 #include <sys/systeminfo.h>
57 #include <sys/secflags.h>
64 int _libproc_debug
; /* set non-zero to enable debugging printfs */
65 int _libproc_no_qsort
; /* set non-zero to inhibit sorting */
66 /* of symbol tables */
67 int _libproc_incore_elf
; /* only use in-core elf data */
69 sigset_t blockable_sigs
; /* signals to block when we need to be safe */
70 static int minfd
; /* minimum file descriptor returned by dupfd(fd, 0) */
71 char procfs_path
[PATH_MAX
] = "/proc";
74 * Function prototypes for static routines in this module.
76 static void deadcheck(struct ps_prochandle
*);
77 static void restore_tracing_flags(struct ps_prochandle
*);
78 static void Lfree_internal(struct ps_prochandle
*, struct ps_lwphandle
*);
79 static prheader_t
*read_lfile(struct ps_prochandle
*, const char *);
82 * Ops vector functions for live processes.
87 Pread_live(struct ps_prochandle
*P
, void *buf
, size_t n
, uintptr_t addr
,
90 return (pread(P
->asfd
, buf
, n
, (off_t
)addr
));
95 Pwrite_live(struct ps_prochandle
*P
, const void *buf
, size_t n
, uintptr_t addr
,
98 return (pwrite(P
->asfd
, buf
, n
, (off_t
)addr
));
103 Pread_maps_live(struct ps_prochandle
*P
, prmap_t
**Pmapp
, ssize_t
*nmapp
,
106 char mapfile
[PATH_MAX
];
110 prmap_t
*Pmap
= NULL
;
112 (void) snprintf(mapfile
, sizeof (mapfile
), "%s/%d/map",
113 procfs_path
, (int)P
->pid
);
114 if ((mapfd
= open(mapfile
, O_RDONLY
)) < 0 ||
115 fstat(mapfd
, &statb
) != 0 ||
116 statb
.st_size
< sizeof (prmap_t
) ||
117 (Pmap
= malloc(statb
.st_size
)) == NULL
||
118 (nmap
= pread(mapfd
, Pmap
, statb
.st_size
, 0L)) <= 0 ||
119 (nmap
/= sizeof (prmap_t
)) == 0) {
124 Preset_maps(P
); /* utter failure; destroy tables */
137 Pread_aux_live(struct ps_prochandle
*P
, auxv_t
**auxvp
, int *nauxp
, void *data
)
145 (void) snprintf(auxfile
, sizeof (auxfile
), "%s/%d/auxv",
146 procfs_path
, (int)P
->pid
);
147 if ((fd
= open(auxfile
, O_RDONLY
)) < 0) {
148 dprintf("%s: failed to open %s: %s\n",
149 __func__
, auxfile
, strerror(errno
));
153 if (fstat(fd
, &statb
) == 0 &&
154 statb
.st_size
>= sizeof (auxv_t
) &&
155 (auxv
= malloc(statb
.st_size
+ sizeof (auxv_t
))) != NULL
) {
156 if ((naux
= read(fd
, auxv
, statb
.st_size
)) < 0 ||
157 (naux
/= sizeof (auxv_t
)) < 1) {
158 dprintf("%s: read failed: %s\n",
159 __func__
, strerror(errno
));
162 auxv
[naux
].a_type
= AT_NULL
;
163 auxv
[naux
].a_un
.a_val
= 0L;
175 Pcred_live(struct ps_prochandle
*P
, prcred_t
*pcrp
, int ngroups
, void *data
)
177 return (proc_get_cred(P
->pid
, pcrp
, ngroups
));
182 Psecflags_live(struct ps_prochandle
*P
, prsecflags_t
**psf
, void *data
)
184 return (proc_get_secflags(P
->pid
, psf
));
189 Ppriv_live(struct ps_prochandle
*P
, prpriv_t
**pprv
, void *data
)
193 pp
= proc_get_priv(P
->pid
);
203 static const psinfo_t
*
204 Ppsinfo_live(struct ps_prochandle
*P
, psinfo_t
*psinfo
, void *data
)
206 if (proc_get_psinfo(P
->pid
, psinfo
) == -1)
214 Plstatus_live(struct ps_prochandle
*P
, void *data
)
216 return (read_lfile(P
, "lstatus"));
221 Plpsinfo_live(struct ps_prochandle
*P
, void *data
)
223 return (read_lfile(P
, "lpsinfo"));
228 Pplatform_live(struct ps_prochandle
*P
, char *s
, size_t n
, void *data
)
230 if (sysinfo(SI_PLATFORM
, s
, n
) == -1)
237 Puname_live(struct ps_prochandle
*P
, struct utsname
*u
, void *data
)
244 Pzonename_live(struct ps_prochandle
*P
, char *s
, size_t n
, void *data
)
246 if (getzonenamebyid(P
->status
.pr_zoneid
, s
, n
) < 0)
253 * Callback function for Pfindexec(). We return a match if we can stat the
254 * suggested pathname and confirm its device and inode number match our
255 * previous information about the /proc/<pid>/object/a.out file.
258 stat_exec(const char *path
, void *arg
)
260 struct stat64
*stp
= arg
;
263 return (stat64(path
, &st
) == 0 && S_ISREG(st
.st_mode
) &&
264 stp
->st_dev
== st
.st_dev
&& stp
->st_ino
== st
.st_ino
);
269 Pexecname_live(struct ps_prochandle
*P
, char *buf
, size_t buflen
, void *data
)
271 char exec_name
[PATH_MAX
];
278 * Try to get the path information first.
280 (void) snprintf(exec_name
, sizeof (exec_name
),
281 "%s/%d/path/a.out", procfs_path
, (int)P
->pid
);
282 if ((ret
= readlink(exec_name
, buf
, buflen
- 1)) > 0) {
284 (void) Pfindobj(P
, buf
, buf
, buflen
);
289 * Stat the executable file so we can compare Pfindexec's
290 * suggestions to the actual device and inode number.
292 (void) snprintf(exec_name
, sizeof (exec_name
),
293 "%s/%d/object/a.out", procfs_path
, (int)P
->pid
);
295 if (stat64(exec_name
, &st
) != 0 || !S_ISREG(st
.st_mode
))
299 * Attempt to figure out the current working directory of the
300 * target process. This only works if the target process has
301 * not changed its current directory since it was exec'd.
303 (void) snprintf(proc_cwd
, sizeof (proc_cwd
),
304 "%s/%d/path/cwd", procfs_path
, (int)P
->pid
);
306 if ((ret
= readlink(proc_cwd
, cwd
, PATH_MAX
- 1)) > 0)
309 (void) Pfindexec(P
, ret
> 0 ? cwd
: NULL
, stat_exec
, &st
);
314 #if defined(__i386) || defined(__amd64)
317 Pldt_live(struct ps_prochandle
*P
, struct ssd
*pldt
, int nldt
, void *data
)
319 return (proc_get_ldt(P
->pid
, pldt
, nldt
));
323 static const ps_ops_t P_live_ops
= {
324 .pop_pread
= Pread_live
,
325 .pop_pwrite
= Pwrite_live
,
326 .pop_read_maps
= Pread_maps_live
,
327 .pop_read_aux
= Pread_aux_live
,
328 .pop_cred
= Pcred_live
,
329 .pop_priv
= Ppriv_live
,
330 .pop_psinfo
= Ppsinfo_live
,
331 .pop_lstatus
= Plstatus_live
,
332 .pop_lpsinfo
= Plpsinfo_live
,
333 .pop_platform
= Pplatform_live
,
334 .pop_uname
= Puname_live
,
335 .pop_zonename
= Pzonename_live
,
336 .pop_execname
= Pexecname_live
,
337 .pop_secflags
= Psecflags_live
,
338 #if defined(__i386) || defined(__amd64)
344 * This is the library's .init handler.
346 #pragma init(_libproc_init)
350 _libproc_debug
= getenv("LIBPROC_DEBUG") != NULL
;
351 _libproc_no_qsort
= getenv("LIBPROC_NO_QSORT") != NULL
;
352 _libproc_incore_elf
= getenv("LIBPROC_INCORE_ELF") != NULL
;
354 (void) sigfillset(&blockable_sigs
);
355 (void) sigdelset(&blockable_sigs
, SIGKILL
);
356 (void) sigdelset(&blockable_sigs
, SIGSTOP
);
360 Pset_procfs_path(const char *path
)
362 (void) snprintf(procfs_path
, sizeof (procfs_path
), "%s", path
);
366 * Call set_minfd() once before calling dupfd() several times.
367 * We assume that the application will not reduce its current file
368 * descriptor limit lower than 512 once it has set at least that value.
373 static mutex_t minfd_lock
= DEFAULTMUTEX
;
377 if ((fd
= minfd
) < 256) {
378 (void) mutex_lock(&minfd_lock
);
379 if ((fd
= minfd
) < 256) {
380 if (getrlimit(RLIMIT_NOFILE
, &rlim
) != 0)
381 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
382 if (rlim
.rlim_cur
>= 512)
384 else if ((fd
= rlim
.rlim_cur
/ 2) < 3)
389 (void) mutex_unlock(&minfd_lock
);
395 dupfd(int fd
, int dfd
)
400 * Make fd be greater than 255 (the 32-bit stdio limit),
401 * or at least make it greater than 2 so that the
402 * program will work when spawned by init(1m).
403 * Also, if dfd is non-zero, dup the fd to be dfd.
405 if ((mfd
= minfd
) == 0)
407 if (dfd
> 0 || (0 <= fd
&& fd
< mfd
)) {
410 dfd
= fcntl(fd
, F_DUPFD
, dfd
);
415 * Mark it close-on-exec so any created process doesn't inherit it.
418 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
423 * Create a new controlled process.
424 * Leave it stopped on successful exit from exec() or execve().
425 * Return an opaque pointer to its process control structure.
426 * Return NULL if process cannot be created (fork()/exec() not successful).
428 struct ps_prochandle
*
429 Pxcreate(const char *file
, /* executable file name */
430 char *const *argv
, /* argument vector */
431 char *const *envp
, /* environment */
432 int *perr
, /* pointer to error return code */
433 char *path
, /* if non-null, holds exec path name on return */
434 size_t len
) /* size of the path buffer */
436 char execpath
[PATH_MAX
];
437 char procname
[PATH_MAX
];
438 struct ps_prochandle
*P
;
445 if (len
== 0) /* zero length, no path */
450 if ((P
= malloc(sizeof (struct ps_prochandle
))) == NULL
) {
455 if ((pid
= fork1()) == -1) {
461 if (pid
== 0) { /* child process */
463 extern char **environ
;
466 * If running setuid or setgid, reset credentials to normal.
468 if ((id
= getgid()) != getegid())
470 if ((id
= getuid()) != geteuid())
473 Pcreate_callback(P
); /* execute callback (see below) */
474 (void) pause(); /* wait for PRSABORT from parent */
477 * This is ugly. There is no execvep() function that takes a
478 * path and an environment. We cheat here by replacing the
479 * global 'environ' variable right before we call this.
482 environ
= (char **)envp
;
484 (void) execvp(file
, argv
); /* execute the program */
489 * Initialize the process structure.
491 (void) memset(P
, 0, sizeof (*P
));
492 (void) mutex_init(&P
->proc_lock
, USYNC_THREAD
, NULL
);
501 Pinit_ops(&P
->ops
, &P_live_ops
);
505 * Open the /proc/pid files.
507 (void) snprintf(procname
, sizeof (procname
), "%s/%d/",
508 procfs_path
, (int)pid
);
509 fname
= procname
+ strlen(procname
);
513 * Exclusive write open advises others not to interfere.
514 * There is no reason for any of these open()s to fail.
516 (void) strcpy(fname
, "as");
517 if ((fd
= open(procname
, (O_RDWR
|O_EXCL
))) < 0 ||
518 (fd
= dupfd(fd
, 0)) < 0) {
519 dprintf("Pcreate: failed to open %s: %s\n",
520 procname
, strerror(errno
));
526 (void) strcpy(fname
, "status");
527 if ((fd
= open(procname
, O_RDONLY
)) < 0 ||
528 (fd
= dupfd(fd
, 0)) < 0) {
529 dprintf("Pcreate: failed to open %s: %s\n",
530 procname
, strerror(errno
));
536 (void) strcpy(fname
, "ctl");
537 if ((fd
= open(procname
, O_WRONLY
)) < 0 ||
538 (fd
= dupfd(fd
, 0)) < 0) {
539 dprintf("Pcreate: failed to open %s: %s\n",
540 procname
, strerror(errno
));
546 (void) Pstop(P
, 0); /* stop the controlled process */
549 * Wait for process to sleep in pause().
550 * If the process has already called pause(), then it should be
551 * stopped (PR_REQUESTED) while asleep in pause and we are done.
552 * Else we set up to catch entry/exit to pause() and set the process
553 * running again, expecting it to stop when it reaches pause().
554 * There is no reason for this to fail other than an interrupt.
556 (void) Psysentry(P
, SYS_pause
, 1);
557 (void) Psysexit(P
, SYS_pause
, 1);
559 if (P
->state
== PS_STOP
&&
560 P
->status
.pr_lwp
.pr_syscall
== SYS_pause
&&
561 (P
->status
.pr_lwp
.pr_why
== PR_REQUESTED
||
562 P
->status
.pr_lwp
.pr_why
== PR_SYSENTRY
||
563 P
->status
.pr_lwp
.pr_why
== PR_SYSEXIT
))
566 if (P
->state
!= PS_STOP
|| /* interrupt or process died */
567 Psetrun(P
, 0, 0) != 0) { /* can't restart */
568 if (errno
== EINTR
|| errno
== ERESTART
)
571 dprintf("Pcreate: Psetrun failed: %s\n",
580 (void) Psysentry(P
, SYS_pause
, 0);
581 (void) Psysexit(P
, SYS_pause
, 0);
584 * Kick the process off the pause() and catch
585 * it again on entry to exec() or exit().
587 (void) Psysentry(P
, SYS_exit
, 1);
588 (void) Psysentry(P
, SYS_execve
, 1);
589 if (Psetrun(P
, 0, PRSABORT
) == -1) {
590 dprintf("Pcreate: Psetrun failed: %s\n", strerror(errno
));
595 if (P
->state
!= PS_STOP
) {
596 dprintf("Pcreate: Pwait failed: %s\n", strerror(errno
));
602 * Move the process through instances of failed exec()s
603 * to reach the point of stopped on successful exec().
605 (void) Psysexit(P
, SYS_execve
, TRUE
);
607 while (P
->state
== PS_STOP
&&
608 P
->status
.pr_lwp
.pr_why
== PR_SYSENTRY
&&
609 P
->status
.pr_lwp
.pr_what
== SYS_execve
) {
611 * Fetch the exec path name now, before we complete
612 * the exec(). We may lose the process and be unable
613 * to get the information later.
615 (void) Pread_string(P
, execpath
, sizeof (execpath
),
616 (off_t
)P
->status
.pr_lwp
.pr_sysarg
[0]);
618 (void) strncpy(path
, execpath
, len
);
620 * Set the process running and wait for
621 * it to stop on exit from the exec().
623 (void) Psetrun(P
, 0, 0);
626 if (P
->state
== PS_LOST
&& /* we lost control */
627 Preopen(P
) != 0) { /* and we can't get it back */
633 * If the exec() failed, continue the loop, expecting
634 * there to be more attempts to exec(), based on PATH.
636 if (P
->state
== PS_STOP
&&
637 P
->status
.pr_lwp
.pr_why
== PR_SYSEXIT
&&
638 P
->status
.pr_lwp
.pr_what
== SYS_execve
&&
639 (lasterrno
= P
->status
.pr_lwp
.pr_errno
) != 0) {
641 * The exec() failed. Set the process running and
642 * wait for it to stop on entry to the next exec().
644 (void) Psetrun(P
, 0, 0);
652 if (P
->state
== PS_STOP
&&
653 P
->status
.pr_lwp
.pr_why
== PR_SYSEXIT
&&
654 P
->status
.pr_lwp
.pr_what
== SYS_execve
&&
655 P
->status
.pr_lwp
.pr_errno
== 0) {
657 * The process is stopped on successful exec() or execve().
658 * Turn off all tracing flags and return success.
660 restore_tracing_flags(P
);
662 /* We must be a 64-bit process to deal with a 64-bit process */
663 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
669 * Set run-on-last-close so the controlled process
670 * runs even if we die on a signal.
672 (void) Psetflags(P
, PR_RLC
);
677 rc
= lasterrno
== ENOENT
? C_NOENT
: C_NOEXEC
;
680 (void) kill(pid
, SIGKILL
);
681 if (path
!= NULL
&& rc
!= C_PERM
&& rc
!= C_LP64
)
688 struct ps_prochandle
*
690 const char *file
, /* executable file name */
691 char *const *argv
, /* argument vector */
692 int *perr
, /* pointer to error return code */
693 char *path
, /* if non-null, holds exec path name on return */
694 size_t len
) /* size of the path buffer */
696 return (Pxcreate(file
, argv
, NULL
, perr
, path
, len
));
700 * Return a printable string corresponding to a Pcreate() error return.
703 Pcreate_error(int error
)
712 str
= "file is set-id or unreadable";
715 str
= "cannot execute file";
718 str
= "operation interrupted";
721 str
= "program is _LP64, self is not";
724 str
= "unanticipated system error";
727 str
= "cannot find executable file";
730 str
= "unknown error";
738 * Callback to execute in each child process created with Pcreate() after fork
739 * but before it execs the new process image. By default, we do nothing, but
740 * by calling this function we allow the client program to define its own
741 * version of the function which will interpose on our empty default. This
742 * may be useful for clients that need to modify signal dispositions, terminal
743 * attributes, or process group and session properties for each new victim.
747 Pcreate_callback(struct ps_prochandle
*P
)
749 /* nothing to do here */
753 * Grab an existing process.
754 * Return an opaque pointer to its process control structure.
756 * pid: UNIX process ID.
758 * PGRAB_RETAIN Retain tracing flags (default clears all tracing flags).
759 * PGRAB_FORCE Grab regardless of whether process is already traced.
760 * PGRAB_RDONLY Open the address space file O_RDONLY instead of O_RDWR,
761 * and do not open the process control file.
762 * PGRAB_NOSTOP Open the process but do not force it to stop.
763 * perr: pointer to error return code.
765 struct ps_prochandle
*
766 Pgrab(pid_t pid
, int flags
, int *perr
)
768 struct ps_prochandle
*P
;
770 char procname
[PATH_MAX
];
775 * PGRAB_RDONLY means that we do not open the /proc/<pid>/control file,
776 * and so it implies RETAIN and NOSTOP since both require control.
778 if (flags
& PGRAB_RDONLY
)
779 flags
|= PGRAB_RETAIN
| PGRAB_NOSTOP
;
781 if ((P
= malloc(sizeof (struct ps_prochandle
))) == NULL
) {
790 again
: /* Come back here if we lose it in the Window of Vulnerability */
792 (void) close(P
->ctlfd
);
794 (void) close(P
->asfd
);
796 (void) close(P
->statfd
);
797 (void) memset(P
, 0, sizeof (*P
));
798 (void) mutex_init(&P
->proc_lock
, USYNC_THREAD
, NULL
);
804 Pinit_ops(&P
->ops
, &P_live_ops
);
808 * Open the /proc/pid files
810 (void) snprintf(procname
, sizeof (procname
), "%s/%d/",
811 procfs_path
, (int)pid
);
812 fname
= procname
+ strlen(procname
);
816 * Request exclusive open to avoid grabbing someone else's
817 * process and to prevent others from interfering afterwards.
818 * If this fails and the 'PGRAB_FORCE' flag is set, attempt to
819 * open non-exclusively.
821 (void) strcpy(fname
, "as");
822 omode
= (flags
& PGRAB_RDONLY
) ? O_RDONLY
: O_RDWR
;
824 if (((fd
= open(procname
, omode
| O_EXCL
)) < 0 &&
825 (fd
= ((flags
& PGRAB_FORCE
)? open(procname
, omode
) : -1)) < 0) ||
826 (fd
= dupfd(fd
, 0)) < 0) {
839 if (!(flags
& PGRAB_FORCE
) || geteuid() != 0) {
845 dprintf("Pgrab: failed to open %s: %s\n",
846 procname
, strerror(errno
));
854 (void) strcpy(fname
, "status");
855 if ((fd
= open(procname
, O_RDONLY
)) < 0 ||
856 (fd
= dupfd(fd
, 0)) < 0) {
865 dprintf("Pgrab: failed to open %s: %s\n",
866 procname
, strerror(errno
));
874 if (!(flags
& PGRAB_RDONLY
)) {
875 (void) strcpy(fname
, "ctl");
876 if ((fd
= open(procname
, O_WRONLY
)) < 0 ||
877 (fd
= dupfd(fd
, 0)) < 0) {
886 dprintf("Pgrab: failed to open %s: %s\n",
887 procname
, strerror(errno
));
900 * We are now in the Window of Vulnerability (WoV). The process may
901 * exec() a setuid/setgid or unreadable object file between the open()
902 * and the PCSTOP. We will get EAGAIN in this case and must start over.
903 * As Pstopstatus will trigger the first read() from a /proc file,
904 * we also need to handle EOVERFLOW here when 32-bit as an indicator
905 * that this process is 64-bit. Finally, if the process has become
906 * a zombie (PS_UNDEAD) while we were trying to grab it, just remain
907 * silent about this and pretend there was no process.
909 if (Pstopstatus(P
, PCNULL
, 0) != 0) {
911 if (errno
== EOVERFLOW
) {
916 if (P
->state
== PS_LOST
) { /* WoV */
917 (void) mutex_destroy(&P
->proc_lock
);
921 if (P
->state
== PS_UNDEAD
)
930 * If the process is a system process, we can't control it even as root
932 if (P
->status
.pr_flags
& PR_ISSYS
) {
938 * We must be a 64-bit process to deal with a 64-bit process
940 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
947 * Remember the status for use by Prelease().
949 P
->orig_status
= P
->status
; /* structure copy */
952 * Before stopping the process, make sure we are not grabbing ourselves.
953 * If we are, make sure we are doing it PGRAB_RDONLY.
955 if (pid
== getpid()) {
957 * Verify that the process is really ourself:
958 * Set a magic number, read it through the
959 * /proc file and see if the results match.
966 if (Pread(P
, &magic2
, sizeof (magic2
), (uintptr_t)&magic1
)
967 == sizeof (magic2
) &&
969 (magic1
= 0xfeedbeef) &&
970 Pread(P
, &magic2
, sizeof (magic2
), (uintptr_t)&magic1
)
971 == sizeof (magic2
) &&
972 magic2
== 0xfeedbeef &&
973 !(flags
& PGRAB_RDONLY
)) {
980 * If the process is already stopped or has been directed
981 * to stop via /proc, do not set run-on-last-close.
983 if (!(P
->status
.pr_lwp
.pr_flags
& (PR_ISTOP
|PR_DSTOP
)) &&
984 !(flags
& PGRAB_RDONLY
)) {
986 * Mark the process run-on-last-close so
987 * it runs even if we die from SIGKILL.
989 if (Psetflags(P
, PR_RLC
) != 0) {
990 if (errno
== EAGAIN
) { /* WoV */
991 (void) mutex_destroy(&P
->proc_lock
);
994 if (errno
== ENOENT
) /* No complaint about zombies */
997 dprintf("Pgrab: failed to set RLC\n");
1005 * If a stop directive is pending and the process has not yet stopped,
1006 * then synchronously wait for the stop directive to take effect.
1007 * Limit the time spent waiting for the process to stop by iterating
1008 * at most 10 times. The time-out of 20 ms corresponds to the time
1009 * between sending the stop directive and the process actually stopped
1010 * as measured by DTrace on a slow, busy system. If the process doesn't
1011 * stop voluntarily, clear the PR_DSTOP flag so that the code below
1012 * forces the process to stop.
1014 if (!(flags
& PGRAB_RDONLY
)) {
1016 while ((P
->status
.pr_lwp
.pr_flags
& (PR_STOPPED
|PR_DSTOP
)) ==
1017 PR_DSTOP
&& niter
< 10 &&
1018 Pstopstatus(P
, PCTWSTOP
, 20) != 0) {
1020 if (flags
& PGRAB_NOSTOP
)
1023 if (niter
== 10 && !(flags
& PGRAB_NOSTOP
)) {
1024 /* Try it harder down below */
1025 P
->status
.pr_lwp
.pr_flags
&= ~PR_DSTOP
;
1030 * If the process is not already stopped or directed to stop
1031 * and PGRAB_NOSTOP was not specified, stop the process now.
1033 if (!(P
->status
.pr_lwp
.pr_flags
& (PR_ISTOP
|PR_DSTOP
)) &&
1034 !(flags
& PGRAB_NOSTOP
)) {
1036 * Stop the process, get its status and signal/syscall masks.
1038 if (((P
->status
.pr_lwp
.pr_flags
& PR_STOPPED
) &&
1039 Pstopstatus(P
, PCDSTOP
, 0) != 0) ||
1040 Pstopstatus(P
, PCSTOP
, 2000) != 0) {
1042 if (errno
== EOVERFLOW
) {
1047 if (P
->state
== PS_LOST
) { /* WoV */
1048 (void) mutex_destroy(&P
->proc_lock
);
1051 if ((errno
!= EINTR
&& errno
!= ERESTART
) ||
1052 (P
->state
!= PS_STOP
&&
1053 !(P
->status
.pr_flags
& PR_DSTOP
))) {
1054 if (P
->state
!= PS_RUN
&& errno
!= ENOENT
) {
1055 dprintf("Pgrab: failed to PCSTOP\n");
1065 * Process should now either be stopped via /proc or there
1066 * should be an outstanding stop directive.
1068 if (!(P
->status
.pr_flags
& (PR_ISTOP
|PR_DSTOP
))) {
1069 dprintf("Pgrab: process is not stopped\n");
1075 * Test this again now because the 32-bit victim process may
1076 * have exec'd a 64-bit process in the meantime.
1078 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
1086 * Cancel all tracing flags unless the PGRAB_RETAIN flag is set.
1088 if (!(flags
& PGRAB_RETAIN
)) {
1089 (void) Psysentry(P
, 0, FALSE
);
1090 (void) Psysexit(P
, 0, FALSE
);
1091 (void) Psignal(P
, 0, FALSE
);
1092 (void) Pfault(P
, 0, FALSE
);
1106 * Return a printable string corresponding to a Pgrab() error return.
1109 Pgrab_error(int error
)
1115 str
= "no such process";
1118 str
= "no such core file";
1120 case G_NOPROCORCORE
:
1121 str
= "no such process or core file";
1124 str
= "cannot find executable file";
1127 str
= "zombie process";
1130 str
= "permission denied";
1133 str
= "process is traced";
1136 str
= "system process";
1139 str
= "attempt to grab self";
1142 str
= "operation interrupted";
1145 str
= "program is _LP64, self is not";
1148 str
= "file is not an ELF core file";
1151 str
= "libelf error";
1154 str
= "core file is corrupt or missing required data";
1157 str
= "unanticipated system error";
1160 str
= "wrong ELF machine type";
1163 str
= "bad lwp specification";
1166 str
= "too many open files";
1169 str
= "unknown error";
1177 * Free a process control structure.
1178 * Close the file descriptors but don't do the Prelease logic.
1181 Pfree(struct ps_prochandle
*P
)
1185 if (P
->ucaddrs
!= NULL
) {
1191 (void) mutex_lock(&P
->proc_lock
);
1192 if (P
->hashtab
!= NULL
) {
1193 struct ps_lwphandle
*L
;
1194 for (i
= 0; i
< HASHSIZE
; i
++) {
1195 while ((L
= P
->hashtab
[i
]) != NULL
)
1196 Lfree_internal(P
, L
);
1201 while (P
->num_fd
> 0) {
1202 fd_info_t
*fip
= list_next(&P
->fd_head
);
1207 (void) mutex_unlock(&P
->proc_lock
);
1208 (void) mutex_destroy(&P
->proc_lock
);
1210 if (P
->agentctlfd
>= 0)
1211 (void) close(P
->agentctlfd
);
1212 if (P
->agentstatfd
>= 0)
1213 (void) close(P
->agentstatfd
);
1215 (void) close(P
->ctlfd
);
1217 (void) close(P
->asfd
);
1219 (void) close(P
->statfd
);
1221 P
->ops
.pop_fini(P
, P
->data
);
1223 /* clear out the structure as a precaution against reuse */
1224 (void) memset(P
, 0, sizeof (*P
));
1229 P
->agentstatfd
= -1;
1235 * Return the state of the process, one of the PS_* values.
1238 Pstate(struct ps_prochandle
*P
)
1244 * Return the open address space file descriptor for the process.
1245 * Clients must not close this file descriptor, not use it
1246 * after the process is freed.
1249 Pasfd(struct ps_prochandle
*P
)
1255 * Return the open control file descriptor for the process.
1256 * Clients must not close this file descriptor, not use it
1257 * after the process is freed.
1260 Pctlfd(struct ps_prochandle
*P
)
1266 * Return a pointer to the process psinfo structure.
1267 * Clients should not hold on to this pointer indefinitely.
1268 * It will become invalid on Prelease().
1271 Ppsinfo(struct ps_prochandle
*P
)
1273 return (P
->ops
.pop_psinfo(P
, &P
->psinfo
, P
->data
));
1277 * Return a pointer to the process status structure.
1278 * Clients should not hold on to this pointer indefinitely.
1279 * It will become invalid on Prelease().
1282 Pstatus(struct ps_prochandle
*P
)
1284 return (&P
->status
);
1288 Pread_status(struct ps_prochandle
*P
)
1290 P
->ops
.pop_status(P
, &P
->status
, P
->data
);
1294 * Fill in a pointer to a process credentials structure. The ngroups parameter
1295 * is the number of supplementary group entries allocated in the caller's cred
1296 * structure. It should equal zero or one unless extra space has been
1297 * allocated for the group list by the caller.
1300 Pcred(struct ps_prochandle
*P
, prcred_t
*pcrp
, int ngroups
)
1302 return (P
->ops
.pop_cred(P
, pcrp
, ngroups
, P
->data
));
1305 /* Return an allocated prsecflags_t */
1307 Psecflags(struct ps_prochandle
*P
, prsecflags_t
**psf
)
1311 if ((ret
= P
->ops
.pop_secflags(P
, psf
, P
->data
)) == 0) {
1312 if ((*psf
)->pr_version
!= PRSECFLAGS_VERSION_1
) {
1322 Psecflags_free(prsecflags_t
*psf
)
1328 Plstatus(struct ps_prochandle
*P
)
1330 return (P
->ops
.pop_lstatus(P
, P
->data
));
1334 Plpsinfo(struct ps_prochandle
*P
)
1336 return (P
->ops
.pop_lpsinfo(P
, P
->data
));
1340 #if defined(__i386) || defined(__amd64)
1342 * Fill in a pointer to a process LDT structure.
1343 * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
1344 * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
1345 * Otherwise we return the actual number of LDT entries fetched (<= nldt).
1348 Pldt(struct ps_prochandle
*P
, struct ssd
*pldt
, int nldt
)
1350 return (P
->ops
.pop_ldt(P
, pldt
, nldt
, P
->data
));
1357 Ppriv_free(struct ps_prochandle
*P
, prpriv_t
*prv
)
1363 * Return a malloced process privilege structure in *pprv.
1366 Ppriv(struct ps_prochandle
*P
, prpriv_t
**pprv
)
1368 return (P
->ops
.pop_priv(P
, pprv
, P
->data
));
1372 Psetpriv(struct ps_prochandle
*P
, prpriv_t
*pprv
)
1378 if (P
->state
== PS_DEAD
) {
1383 sz
= PRIV_PRPRIV_SIZE(pprv
) + sizeof (long);
1385 sz
= ((sz
- 1) / sizeof (long) + 1) * sizeof (long);
1393 (void) memcpy(&ctl
[1], pprv
, PRIV_PRPRIV_SIZE(pprv
));
1395 if (write(P
->ctlfd
, ctl
, sz
) != sz
)
1406 Pprivinfo(struct ps_prochandle
*P
)
1408 core_info_t
*core
= P
->data
;
1410 /* Use default from libc */
1411 if (P
->state
!= PS_DEAD
)
1414 return (core
->core_privinfo
);
1418 * Ensure that all cached state is written to the process.
1419 * The cached state is the LWP's signal mask and registers
1420 * and the process's tracing flags.
1423 Psync(struct ps_prochandle
*P
)
1425 int ctlfd
= (P
->agentctlfd
>= 0)? P
->agentctlfd
: P
->ctlfd
;
1430 if (P
->flags
& SETHOLD
) {
1432 iov
[n
].iov_base
= (caddr_t
)&cmd
[0];
1433 iov
[n
++].iov_len
= sizeof (long);
1434 iov
[n
].iov_base
= (caddr_t
)&P
->status
.pr_lwp
.pr_lwphold
;
1435 iov
[n
++].iov_len
= sizeof (P
->status
.pr_lwp
.pr_lwphold
);
1437 if (P
->flags
& SETREGS
) {
1440 /* XX64 we should probably restore REG_GS after this */
1441 if (ctlfd
== P
->agentctlfd
)
1442 P
->status
.pr_lwp
.pr_reg
[GS
] = 0;
1443 #elif defined(__amd64)
1446 iov
[n
].iov_base
= (caddr_t
)&cmd
[1];
1447 iov
[n
++].iov_len
= sizeof (long);
1448 iov
[n
].iov_base
= (caddr_t
)&P
->status
.pr_lwp
.pr_reg
[0];
1449 iov
[n
++].iov_len
= sizeof (P
->status
.pr_lwp
.pr_reg
);
1451 if (P
->flags
& SETSIG
) {
1453 iov
[n
].iov_base
= (caddr_t
)&cmd
[2];
1454 iov
[n
++].iov_len
= sizeof (long);
1455 iov
[n
].iov_base
= (caddr_t
)&P
->status
.pr_sigtrace
;
1456 iov
[n
++].iov_len
= sizeof (P
->status
.pr_sigtrace
);
1458 if (P
->flags
& SETFAULT
) {
1460 iov
[n
].iov_base
= (caddr_t
)&cmd
[3];
1461 iov
[n
++].iov_len
= sizeof (long);
1462 iov
[n
].iov_base
= (caddr_t
)&P
->status
.pr_flttrace
;
1463 iov
[n
++].iov_len
= sizeof (P
->status
.pr_flttrace
);
1465 if (P
->flags
& SETENTRY
) {
1467 iov
[n
].iov_base
= (caddr_t
)&cmd
[4];
1468 iov
[n
++].iov_len
= sizeof (long);
1469 iov
[n
].iov_base
= (caddr_t
)&P
->status
.pr_sysentry
;
1470 iov
[n
++].iov_len
= sizeof (P
->status
.pr_sysentry
);
1472 if (P
->flags
& SETEXIT
) {
1474 iov
[n
].iov_base
= (caddr_t
)&cmd
[5];
1475 iov
[n
++].iov_len
= sizeof (long);
1476 iov
[n
].iov_base
= (caddr_t
)&P
->status
.pr_sysexit
;
1477 iov
[n
++].iov_len
= sizeof (P
->status
.pr_sysexit
);
1480 if (n
== 0 || writev(ctlfd
, iov
, n
) < 0)
1481 return; /* nothing to do or write failed */
1483 P
->flags
&= ~(SETSIG
|SETFAULT
|SETENTRY
|SETEXIT
|SETHOLD
|SETREGS
);
1487 * Reopen the /proc file (after PS_LOST).
1490 Preopen(struct ps_prochandle
*P
)
1493 char procname
[PATH_MAX
];
1496 if (P
->state
== PS_DEAD
|| P
->state
== PS_IDLE
)
1499 if (P
->agentcnt
> 0) {
1504 (void) snprintf(procname
, sizeof (procname
), "%s/%d/",
1505 procfs_path
, (int)P
->pid
);
1506 fname
= procname
+ strlen(procname
);
1508 (void) strcpy(fname
, "as");
1509 if ((fd
= open(procname
, O_RDWR
)) < 0 ||
1510 close(P
->asfd
) < 0 ||
1511 (fd
= dupfd(fd
, P
->asfd
)) != P
->asfd
) {
1512 dprintf("Preopen: failed to open %s: %s\n",
1513 procname
, strerror(errno
));
1520 (void) strcpy(fname
, "status");
1521 if ((fd
= open(procname
, O_RDONLY
)) < 0 ||
1522 close(P
->statfd
) < 0 ||
1523 (fd
= dupfd(fd
, P
->statfd
)) != P
->statfd
) {
1524 dprintf("Preopen: failed to open %s: %s\n",
1525 procname
, strerror(errno
));
1532 (void) strcpy(fname
, "ctl");
1533 if ((fd
= open(procname
, O_WRONLY
)) < 0 ||
1534 close(P
->ctlfd
) < 0 ||
1535 (fd
= dupfd(fd
, P
->ctlfd
)) != P
->ctlfd
) {
1536 dprintf("Preopen: failed to open %s: %s\n",
1537 procname
, strerror(errno
));
1545 * Set the state to PS_RUN and wait for the process to stop so that
1546 * we re-read the status from the new P->statfd. If this fails, Pwait
1547 * will reset the state to PS_LOST and we fail the reopen. Before
1548 * returning, we also forge a bit of P->status to allow the debugger to
1549 * see that we are PS_LOST following a successful exec.
1552 if (Pwait(P
, 0) == -1) {
1554 if (errno
== EOVERFLOW
)
1555 P
->status
.pr_dmodel
= PR_MODEL_LP64
;
1557 P
->status
.pr_lwp
.pr_why
= PR_SYSEXIT
;
1558 P
->status
.pr_lwp
.pr_what
= SYS_execve
;
1559 P
->status
.pr_lwp
.pr_errno
= 0;
1564 * The process should be stopped on exec (REQUESTED)
1565 * or else should be stopped on exit from exec() (SYSEXIT)
1567 if (P
->state
== PS_STOP
&&
1568 (P
->status
.pr_lwp
.pr_why
== PR_REQUESTED
||
1569 (P
->status
.pr_lwp
.pr_why
== PR_SYSEXIT
&&
1570 P
->status
.pr_lwp
.pr_what
== SYS_execve
))) {
1571 /* fake up stop-on-exit-from-execve */
1572 if (P
->status
.pr_lwp
.pr_why
== PR_REQUESTED
) {
1573 P
->status
.pr_lwp
.pr_why
= PR_SYSEXIT
;
1574 P
->status
.pr_lwp
.pr_what
= SYS_execve
;
1575 P
->status
.pr_lwp
.pr_errno
= 0;
1578 dprintf("Preopen: expected REQUESTED or "
1579 "SYSEXIT(SYS_execve) stop\n");
1586 * Define all settable flags other than the microstate accounting flags.
1588 #define ALL_SETTABLE_FLAGS (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_PTRACE)
1591 * Restore /proc tracing flags to their original values
1592 * in preparation for releasing the process.
1593 * Also called by Pcreate() to clear all tracing flags.
1596 restore_tracing_flags(struct ps_prochandle
*P
)
1602 if (P
->flags
& CREATED
) {
1603 /* we created this process; clear all tracing flags */
1604 premptyset(&P
->status
.pr_sigtrace
);
1605 premptyset(&P
->status
.pr_flttrace
);
1606 premptyset(&P
->status
.pr_sysentry
);
1607 premptyset(&P
->status
.pr_sysexit
);
1608 if ((P
->status
.pr_flags
& ALL_SETTABLE_FLAGS
) != 0)
1609 (void) Punsetflags(P
, ALL_SETTABLE_FLAGS
);
1611 /* we grabbed the process; restore its tracing flags */
1612 P
->status
.pr_sigtrace
= P
->orig_status
.pr_sigtrace
;
1613 P
->status
.pr_flttrace
= P
->orig_status
.pr_flttrace
;
1614 P
->status
.pr_sysentry
= P
->orig_status
.pr_sysentry
;
1615 P
->status
.pr_sysexit
= P
->orig_status
.pr_sysexit
;
1616 if ((P
->status
.pr_flags
& ALL_SETTABLE_FLAGS
) !=
1617 (flags
= (P
->orig_status
.pr_flags
& ALL_SETTABLE_FLAGS
))) {
1618 (void) Punsetflags(P
, ALL_SETTABLE_FLAGS
);
1620 (void) Psetflags(P
, flags
);
1625 iov
[0].iov_base
= (caddr_t
)&cmd
[0];
1626 iov
[0].iov_len
= sizeof (long);
1627 iov
[1].iov_base
= (caddr_t
)&P
->status
.pr_sigtrace
;
1628 iov
[1].iov_len
= sizeof (P
->status
.pr_sigtrace
);
1631 iov
[2].iov_base
= (caddr_t
)&cmd
[1];
1632 iov
[2].iov_len
= sizeof (long);
1633 iov
[3].iov_base
= (caddr_t
)&P
->status
.pr_flttrace
;
1634 iov
[3].iov_len
= sizeof (P
->status
.pr_flttrace
);
1637 iov
[4].iov_base
= (caddr_t
)&cmd
[2];
1638 iov
[4].iov_len
= sizeof (long);
1639 iov
[5].iov_base
= (caddr_t
)&P
->status
.pr_sysentry
;
1640 iov
[5].iov_len
= sizeof (P
->status
.pr_sysentry
);
1643 iov
[6].iov_base
= (caddr_t
)&cmd
[3];
1644 iov
[6].iov_len
= sizeof (long);
1645 iov
[7].iov_base
= (caddr_t
)&P
->status
.pr_sysexit
;
1646 iov
[7].iov_len
= sizeof (P
->status
.pr_sysexit
);
1648 (void) writev(P
->ctlfd
, iov
, 8);
1650 P
->flags
&= ~(SETSIG
|SETFAULT
|SETENTRY
|SETEXIT
);
1654 * Release the process. Frees the process control structure.
1656 * PRELEASE_CLEAR Clear all tracing flags.
1657 * PRELEASE_RETAIN Retain current tracing flags.
1658 * PRELEASE_HANG Leave the process stopped and abandoned.
1659 * PRELEASE_KILL Terminate the process with SIGKILL.
1662 Prelease(struct ps_prochandle
*P
, int flags
)
1664 if (P
->state
== PS_DEAD
) {
1665 dprintf("Prelease: releasing handle %p PS_DEAD of pid %d\n",
1666 (void *)P
, (int)P
->pid
);
1671 if (P
->state
== PS_IDLE
) {
1672 file_info_t
*fptr
= list_next(&P
->file_head
);
1673 dprintf("Prelease: releasing handle %p PS_IDLE of file %s\n",
1674 (void *)P
, fptr
->file_pname
);
1679 dprintf("Prelease: releasing handle %p pid %d\n",
1680 (void *)P
, (int)P
->pid
);
1682 if (P
->ctlfd
== -1) {
1687 if (P
->agentcnt
> 0) {
1693 * Attempt to stop the process.
1696 (void) Pstop(P
, 1000);
1698 if (flags
& PRELEASE_KILL
) {
1699 if (P
->state
== PS_STOP
)
1700 (void) Psetrun(P
, SIGKILL
, 0);
1701 (void) kill(P
->pid
, SIGKILL
);
1707 * If we lost control, all we can do now is close the files.
1708 * In this case, the last close sets the process running.
1710 if (P
->state
!= PS_STOP
&&
1711 (P
->status
.pr_lwp
.pr_flags
& (PR_ISTOP
|PR_DSTOP
)) == 0) {
1717 * We didn't lose control; we do more.
1721 if (flags
& PRELEASE_CLEAR
)
1722 P
->flags
|= CREATED
;
1724 if (!(flags
& PRELEASE_RETAIN
))
1725 restore_tracing_flags(P
);
1727 if (flags
& PRELEASE_HANG
) {
1728 /* Leave the process stopped and abandoned */
1729 (void) Punsetflags(P
, PR_RLC
|PR_KLC
);
1735 * Set the process running if we created it or if it was
1736 * not originally stopped or directed to stop via /proc
1737 * or if we were given the PRELEASE_CLEAR flag.
1739 if ((P
->flags
& CREATED
) ||
1740 (P
->orig_status
.pr_lwp
.pr_flags
& (PR_ISTOP
|PR_DSTOP
)) == 0) {
1741 (void) Psetflags(P
, PR_RLC
);
1743 * We do this repeatedly because the process may have
1744 * more than one LWP stopped on an event of interest.
1745 * This makes sure all of them are set running.
1748 if (Psetrun(P
, 0, 0) == -1 && errno
== EBUSY
)
1749 break; /* Agent LWP may be stuck */
1750 } while (Pstopstatus(P
, PCNULL
, 0) == 0 &&
1751 P
->status
.pr_lwp
.pr_flags
& (PR_ISTOP
|PR_DSTOP
));
1753 if (P
->status
.pr_lwp
.pr_flags
& (PR_ISTOP
|PR_DSTOP
))
1754 dprintf("Prelease: failed to set process running\n");
1762 prldump(const char *caller
, lwpstatus_t
*lsp
)
1767 switch (lsp
->pr_why
) {
1769 dprintf("%s: REQUESTED\n", caller
);
1772 dprintf("%s: SIGNALLED %s\n", caller
,
1773 proc_signame(lsp
->pr_what
, name
, sizeof (name
)));
1776 dprintf("%s: FAULTED %s\n", caller
,
1777 proc_fltname(lsp
->pr_what
, name
, sizeof (name
)));
1780 dprintf("%s: SYSENTRY %s\n", caller
,
1781 proc_sysname(lsp
->pr_what
, name
, sizeof (name
)));
1784 dprintf("%s: SYSEXIT %s\n", caller
,
1785 proc_sysname(lsp
->pr_what
, name
, sizeof (name
)));
1788 dprintf("%s: JOBCONTROL %s\n", caller
,
1789 proc_signame(lsp
->pr_what
, name
, sizeof (name
)));
1792 dprintf("%s: SUSPENDED\n", caller
);
1795 dprintf("%s: Unknown\n", caller
);
1800 dprintf("%s: p_cursig = %d\n", caller
, lsp
->pr_cursig
);
1802 bits
= *((uint32_t *)&lsp
->pr_lwppend
);
1804 dprintf("%s: pr_lwppend = 0x%.8X\n", caller
, bits
);
1809 prdump(struct ps_prochandle
*P
)
1813 prldump("Pstopstatus", &P
->status
.pr_lwp
);
1815 bits
= *((uint32_t *)&P
->status
.pr_sigpend
);
1817 dprintf("Pstopstatus: pr_sigpend = 0x%.8X\n", bits
);
1821 * Wait for the specified process to stop or terminate.
1822 * Or, just get the current status (PCNULL).
1823 * Or, direct it to stop and get the current status (PCDSTOP).
1824 * If the agent LWP exists, do these things to the agent,
1825 * else do these things to the process as a whole.
1828 Pstopstatus(struct ps_prochandle
*P
,
1829 long request
, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
1830 uint_t msec
) /* if non-zero, timeout in milliseconds */
1832 int ctlfd
= (P
->agentctlfd
>= 0)? P
->agentctlfd
: P
->ctlfd
;
1836 int old_state
= P
->state
;
1842 if (request
!= PCNULL
&& request
!= PCDSTOP
)
1846 if (request
!= PCNULL
) {
1854 if (request
!= PCNULL
) {
1859 default: /* corrupted state */
1860 dprintf("Pstopstatus: corrupted state: %d\n", P
->state
);
1867 ctl
[2] = (long)msec
;
1871 rc
= write(ctlfd
, &ctl
[0], 3*sizeof (long));
1874 rc
= write(ctlfd
, &ctl
[1], 2*sizeof (long));
1877 rc
= write(ctlfd
, &ctl
[0], 1*sizeof (long));
1880 if (P
->state
== PS_DEAD
|| P
->state
== PS_IDLE
)
1883 default: /* programming error */
1887 err
= (rc
< 0)? errno
: 0;
1890 if (P
->agentstatfd
< 0) {
1891 if (pread(P
->statfd
, &P
->status
,
1892 sizeof (P
->status
), (off_t
)0) < 0)
1895 if (pread(P
->agentstatfd
, &P
->status
.pr_lwp
,
1896 sizeof (P
->status
.pr_lwp
), (off_t
)0) < 0)
1898 P
->status
.pr_flags
= P
->status
.pr_lwp
.pr_flags
;
1903 case EINTR
: /* user typed ctl-C */
1905 dprintf("Pstopstatus: EINTR\n");
1907 case EAGAIN
: /* we lost control of the the process */
1909 dprintf("Pstopstatus: PS_LOST, errno=%d\n", err
);
1912 default: /* check for dead process */
1913 if (_libproc_debug
) {
1918 errstr
= "Pstopstatus PCNULL"; break;
1920 errstr
= "Pstopstatus PCSTOP"; break;
1922 errstr
= "Pstopstatus PCDSTOP"; break;
1924 errstr
= "Pstopstatus PCWSTOP"; break;
1926 errstr
= "Pstopstatus PC???"; break;
1928 dprintf("%s: %s\n", errstr
, strerror(err
));
1933 if (err
!= EINTR
&& err
!= ERESTART
) {
1939 if (!(P
->status
.pr_flags
& PR_STOPPED
)) {
1941 if (request
== PCNULL
|| request
== PCDSTOP
|| msec
!= 0)
1943 dprintf("Pstopstatus: process is not stopped\n");
1950 if (_libproc_debug
) /* debugging */
1954 * If the process was already stopped coming into Pstopstatus(),
1955 * then don't use its PC to set P->sysaddr since it may have been
1956 * changed since the time the process originally stopped.
1958 if (old_state
== PS_STOP
)
1961 switch (P
->status
.pr_lwp
.pr_why
) {
1964 if (Pissyscall_prev(P
, P
->status
.pr_lwp
.pr_reg
[R_PC
],
1966 P
->sysaddr
= P
->status
.pr_lwp
.pr_reg
[R_PC
];
1983 * Wait for the process to stop for any reason.
1986 Pwait(struct ps_prochandle
*P
, uint_t msec
)
1988 return (Pstopstatus(P
, PCWSTOP
, msec
));
1992 * Direct the process to stop; wait for it to stop.
1995 Pstop(struct ps_prochandle
*P
, uint_t msec
)
1997 return (Pstopstatus(P
, PCSTOP
, msec
));
2001 * Direct the process to stop; don't wait.
2004 Pdstop(struct ps_prochandle
*P
)
2006 return (Pstopstatus(P
, PCDSTOP
, 0));
2010 deadcheck(struct ps_prochandle
*P
)
2017 P
->state
= PS_UNDEAD
;
2019 if (P
->agentstatfd
< 0) {
2022 size
= sizeof (P
->status
);
2024 fd
= P
->agentstatfd
;
2025 buf
= &P
->status
.pr_lwp
;
2026 size
= sizeof (P
->status
.pr_lwp
);
2028 while (pread(fd
, buf
, size
, (off_t
)0) != size
) {
2031 P
->state
= PS_UNDEAD
;
2042 P
->status
.pr_flags
= P
->status
.pr_lwp
.pr_flags
;
2047 * Get the value of one register from stopped process.
2050 Pgetareg(struct ps_prochandle
*P
, int regno
, prgreg_t
*preg
)
2052 if (regno
< 0 || regno
>= NPRGREG
) {
2057 if (P
->state
== PS_IDLE
) {
2062 if (P
->state
!= PS_STOP
&& P
->state
!= PS_DEAD
) {
2067 *preg
= P
->status
.pr_lwp
.pr_reg
[regno
];
2072 * Put value of one register into stopped process.
2075 Pputareg(struct ps_prochandle
*P
, int regno
, prgreg_t reg
)
2077 if (regno
< 0 || regno
>= NPRGREG
) {
2082 if (P
->state
!= PS_STOP
) {
2087 P
->status
.pr_lwp
.pr_reg
[regno
] = reg
;
2088 P
->flags
|= SETREGS
; /* set registers before continuing */
2093 Psetrun(struct ps_prochandle
*P
,
2094 int sig
, /* signal to pass to process */
2095 int flags
) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
2097 int ctlfd
= (P
->agentctlfd
>= 0) ? P
->agentctlfd
: P
->ctlfd
;
2098 int sbits
= (PR_DSTOP
| PR_ISTOP
| PR_ASLEEP
);
2100 long ctl
[1 + /* PCCFAULT */
2101 1 + sizeof (siginfo_t
)/sizeof (long) + /* PCSSIG/PCCSIG */
2107 if (P
->state
!= PS_STOP
&& (P
->status
.pr_lwp
.pr_flags
& sbits
) == 0) {
2112 Psync(P
); /* flush tracing flags and registers */
2114 if (flags
& PRCFAULT
) { /* clear current fault */
2119 if (flags
& PRCSIG
) { /* clear current signal */
2122 } else if (sig
&& sig
!= P
->status
.pr_lwp
.pr_cursig
) {
2123 /* make current signal */
2127 infop
= (siginfo_t
*)ctlp
;
2128 (void) memset(infop
, 0, sizeof (*infop
));
2129 infop
->si_signo
= sig
;
2130 ctlp
+= sizeof (siginfo_t
) / sizeof (long);
2135 size
= (char *)ctlp
- (char *)ctl
;
2137 P
->info_valid
= 0; /* will need to update map and file info */
2140 * If we've cached ucontext-list information while we were stopped,
2143 if (P
->ucaddrs
!= NULL
) {
2149 if (write(ctlfd
, ctl
, size
) != size
) {
2150 /* If it is dead or lost, return the real status, not PS_RUN */
2151 if (errno
== ENOENT
|| errno
== EAGAIN
) {
2152 (void) Pstopstatus(P
, PCNULL
, 0);
2155 /* If it is not in a jobcontrol stop, issue an error message */
2156 if (errno
!= EBUSY
||
2157 P
->status
.pr_lwp
.pr_why
!= PR_JOBCONTROL
) {
2158 dprintf("Psetrun: %s\n", strerror(errno
));
2161 /* Otherwise pretend that the job-stopped process is running */
2169 Pread(struct ps_prochandle
*P
,
2170 void *buf
, /* caller's buffer */
2171 size_t nbyte
, /* number of bytes to read */
2172 uintptr_t address
) /* address in process */
2174 return (P
->ops
.pop_pread(P
, buf
, nbyte
, address
, P
->data
));
2178 Pread_string(struct ps_prochandle
*P
,
2179 char *buf
, /* caller's buffer */
2180 size_t size
, /* upper limit on bytes to read */
2181 uintptr_t addr
) /* address in process */
2183 enum { STRSZ
= 40 };
2184 char string
[STRSZ
+ 1];
2193 size
--; /* ensure trailing null fits in buffer */
2196 string
[STRSZ
] = '\0';
2198 for (nbyte
= STRSZ
; nbyte
== STRSZ
&& leng
< size
; addr
+= STRSZ
) {
2199 if ((nbyte
= P
->ops
.pop_pread(P
, string
, STRSZ
, addr
,
2202 return (leng
? leng
: -1);
2204 if ((nbyte
= strlen(string
)) > 0) {
2205 if (leng
+ nbyte
> size
)
2206 nbyte
= size
- leng
;
2207 (void) strncpy(buf
+ leng
, string
, nbyte
);
2216 Pwrite(struct ps_prochandle
*P
,
2217 const void *buf
, /* caller's buffer */
2218 size_t nbyte
, /* number of bytes to write */
2219 uintptr_t address
) /* address in process */
2221 return (P
->ops
.pop_pwrite(P
, buf
, nbyte
, address
, P
->data
));
2225 Pclearsig(struct ps_prochandle
*P
)
2227 int ctlfd
= (P
->agentctlfd
>= 0)? P
->agentctlfd
: P
->ctlfd
;
2230 if (write(ctlfd
, &ctl
, sizeof (ctl
)) != sizeof (ctl
))
2232 P
->status
.pr_lwp
.pr_cursig
= 0;
2237 Pclearfault(struct ps_prochandle
*P
)
2239 int ctlfd
= (P
->agentctlfd
>= 0)? P
->agentctlfd
: P
->ctlfd
;
2240 long ctl
= PCCFAULT
;
2242 if (write(ctlfd
, &ctl
, sizeof (ctl
)) != sizeof (ctl
))
2248 * Set a breakpoint trap, return original instruction.
2251 Psetbkpt(struct ps_prochandle
*P
, uintptr_t address
, ulong_t
*saved
)
2253 long ctl
[1 + sizeof (priovec_t
) / sizeof (long) + /* PCREAD */
2254 1 + sizeof (priovec_t
) / sizeof (long)]; /* PCWRITE */
2261 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2262 P
->state
== PS_IDLE
) {
2267 /* fetch the old instruction */
2269 iovp
= (priovec_t
*)ctlp
;
2270 iovp
->pio_base
= &old
;
2271 iovp
->pio_len
= sizeof (old
);
2272 iovp
->pio_offset
= address
;
2273 ctlp
+= sizeof (priovec_t
) / sizeof (long);
2275 /* write the BPT instruction */
2277 iovp
= (priovec_t
*)ctlp
;
2278 iovp
->pio_base
= &bpt
;
2279 iovp
->pio_len
= sizeof (bpt
);
2280 iovp
->pio_offset
= address
;
2281 ctlp
+= sizeof (priovec_t
) / sizeof (long);
2283 size
= (char *)ctlp
- (char *)ctl
;
2284 if (write(P
->ctlfd
, ctl
, size
) != size
)
2288 * Fail if there was already a breakpoint there from another debugger
2289 * or DTrace's user-level tracing on x86.
2296 *saved
= (ulong_t
)old
;
2301 * Restore original instruction where a breakpoint was set.
2304 Pdelbkpt(struct ps_prochandle
*P
, uintptr_t address
, ulong_t saved
)
2306 instr_t old
= (instr_t
)saved
;
2309 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2310 P
->state
== PS_IDLE
) {
2316 * If the breakpoint instruction we had placed has been overwritten
2317 * with a new instruction, then don't try to replace it with the
2318 * old instruction. Doing do can cause problems with self-modifying
2319 * code -- PLTs for example. If the Pread() fails, we assume that we
2320 * should proceed though most likely the Pwrite() will also fail.
2322 if (Pread(P
, &cur
, sizeof (cur
), address
) == sizeof (cur
) &&
2326 if (Pwrite(P
, &old
, sizeof (old
), address
) != sizeof (old
))
2333 * Common code for Pxecbkpt() and Lxecbkpt().
2334 * Develop the array of requests that will do the job, then
2335 * write them to the specified control file descriptor.
2336 * Return the non-zero errno if the write fails.
2340 int ctlfd
, /* process or LWP control file descriptor */
2341 const fltset_t
*faultset
, /* current set of traced faults */
2342 const sigset_t
*sigmask
, /* current signal mask */
2343 uintptr_t address
, /* address of breakpint */
2344 ulong_t saved
) /* the saved instruction */
2347 1 + sizeof (sigset_t
) / sizeof (long) + /* PCSHOLD */
2348 1 + sizeof (fltset_t
) / sizeof (long) + /* PCSFAULT */
2349 1 + sizeof (priovec_t
) / sizeof (long) + /* PCWRITE */
2353 1 + sizeof (priovec_t
) / sizeof (long) + /* PCWRITE */
2354 1 + sizeof (fltset_t
) / sizeof (long) + /* PCSFAULT */
2355 1 + sizeof (sigset_t
) / sizeof (long)]; /* PCSHOLD */
2363 instr_t old
= (instr_t
)saved
;
2367 /* block our signals for the duration */
2368 (void) sigprocmask(SIG_BLOCK
, &blockable_sigs
, &unblock
);
2370 /* hold posted signals */
2372 holdp
= (sigset_t
*)ctlp
;
2374 prdelset(holdp
, SIGKILL
);
2375 prdelset(holdp
, SIGSTOP
);
2376 ctlp
+= sizeof (sigset_t
) / sizeof (long);
2378 /* force tracing of FLTTRACE */
2379 if (!(prismember(faultset
, FLTTRACE
))) {
2381 faultp
= (fltset_t
*)ctlp
;
2382 *faultp
= *faultset
;
2383 praddset(faultp
, FLTTRACE
);
2384 ctlp
+= sizeof (fltset_t
) / sizeof (long);
2387 /* restore the old instruction */
2389 iovp
= (priovec_t
*)ctlp
;
2390 iovp
->pio_base
= &old
;
2391 iovp
->pio_len
= sizeof (old
);
2392 iovp
->pio_offset
= address
;
2393 ctlp
+= sizeof (priovec_t
) / sizeof (long);
2395 /* clear current signal and fault; set running w/ single-step */
2397 *ctlp
++ = PRCSIG
| PRCFAULT
| PRSTEP
;
2399 /* wait for stop, cancel the fault */
2403 /* restore the breakpoint trap */
2405 iovp
= (priovec_t
*)ctlp
;
2406 iovp
->pio_base
= &bpt
;
2407 iovp
->pio_len
= sizeof (bpt
);
2408 iovp
->pio_offset
= address
;
2409 ctlp
+= sizeof (priovec_t
) / sizeof (long);
2411 /* restore fault tracing set */
2412 if (!(prismember(faultset
, FLTTRACE
))) {
2414 *(fltset_t
*)ctlp
= *faultset
;
2415 ctlp
+= sizeof (fltset_t
) / sizeof (long);
2418 /* restore the hold mask */
2420 *(sigset_t
*)ctlp
= *sigmask
;
2421 ctlp
+= sizeof (sigset_t
) / sizeof (long);
2423 size
= (char *)ctlp
- (char *)ctl
;
2424 if ((ssize
= write(ctlfd
, ctl
, size
)) != size
)
2425 error
= (ssize
== -1)? errno
: EINTR
;
2426 (void) sigprocmask(SIG_SETMASK
, &unblock
, NULL
);
2431 * Step over a breakpoint, i.e., execute the instruction that
2432 * really belongs at the breakpoint location (the current %pc)
2433 * and leave the process stopped at the next instruction.
2436 Pxecbkpt(struct ps_prochandle
*P
, ulong_t saved
)
2438 int ctlfd
= (P
->agentctlfd
>= 0)? P
->agentctlfd
: P
->ctlfd
;
2441 if (P
->state
!= PS_STOP
) {
2448 error
= execute_bkpt(ctlfd
,
2449 &P
->status
.pr_flttrace
, &P
->status
.pr_lwp
.pr_lwphold
,
2450 P
->status
.pr_lwp
.pr_reg
[R_PC
], saved
);
2451 rv
= Pstopstatus(P
, PCNULL
, 0);
2454 if (P
->status
.pr_lwp
.pr_why
== PR_JOBCONTROL
&&
2455 error
== EBUSY
) { /* jobcontrol stop -- back off */
2459 if (error
== ENOENT
)
2469 * Install the watchpoint described by wp.
2472 Psetwapt(struct ps_prochandle
*P
, const prwatch_t
*wp
)
2474 long ctl
[1 + sizeof (prwatch_t
) / sizeof (long)];
2475 prwatch_t
*cwp
= (prwatch_t
*)&ctl
[1];
2477 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2478 P
->state
== PS_IDLE
) {
2484 cwp
->pr_vaddr
= wp
->pr_vaddr
;
2485 cwp
->pr_size
= wp
->pr_size
;
2486 cwp
->pr_wflags
= wp
->pr_wflags
;
2488 if (write(P
->ctlfd
, ctl
, sizeof (ctl
)) != sizeof (ctl
))
2495 * Remove the watchpoint described by wp.
2498 Pdelwapt(struct ps_prochandle
*P
, const prwatch_t
*wp
)
2500 long ctl
[1 + sizeof (prwatch_t
) / sizeof (long)];
2501 prwatch_t
*cwp
= (prwatch_t
*)&ctl
[1];
2503 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2504 P
->state
== PS_IDLE
) {
2510 cwp
->pr_vaddr
= wp
->pr_vaddr
;
2511 cwp
->pr_size
= wp
->pr_size
;
2514 if (write(P
->ctlfd
, ctl
, sizeof (ctl
)) != sizeof (ctl
))
2521 * Common code for Pxecwapt() and Lxecwapt(). Develop the array of requests
2522 * that will do the job, then write them to the specified control file
2523 * descriptor. Return the non-zero errno if the write fails.
2527 int ctlfd
, /* process or LWP control file descriptor */
2528 const fltset_t
*faultset
, /* current set of traced faults */
2529 const sigset_t
*sigmask
, /* current signal mask */
2530 const prwatch_t
*wp
) /* watchpoint descriptor */
2533 1 + sizeof (sigset_t
) / sizeof (long) + /* PCSHOLD */
2534 1 + sizeof (fltset_t
) / sizeof (long) + /* PCSFAULT */
2535 1 + sizeof (prwatch_t
) / sizeof (long) + /* PCWATCH */
2539 1 + sizeof (prwatch_t
) / sizeof (long) + /* PCWATCH */
2540 1 + sizeof (fltset_t
) / sizeof (long) + /* PCSFAULT */
2541 1 + sizeof (sigset_t
) / sizeof (long)]; /* PCSHOLD */
2553 (void) sigprocmask(SIG_BLOCK
, &blockable_sigs
, &unblock
);
2556 * Hold all posted signals in the victim process prior to stepping.
2559 holdp
= (sigset_t
*)ctlp
;
2561 prdelset(holdp
, SIGKILL
);
2562 prdelset(holdp
, SIGSTOP
);
2563 ctlp
+= sizeof (sigset_t
) / sizeof (long);
2566 * Force tracing of FLTTRACE since we need to single step.
2568 if (!(prismember(faultset
, FLTTRACE
))) {
2570 faultp
= (fltset_t
*)ctlp
;
2571 *faultp
= *faultset
;
2572 praddset(faultp
, FLTTRACE
);
2573 ctlp
+= sizeof (fltset_t
) / sizeof (long);
2577 * Clear only the current watchpoint by setting pr_wflags to zero.
2580 prw
= (prwatch_t
*)ctlp
;
2581 prw
->pr_vaddr
= wp
->pr_vaddr
;
2582 prw
->pr_size
= wp
->pr_size
;
2584 ctlp
+= sizeof (prwatch_t
) / sizeof (long);
2587 * Clear the current signal and fault; set running with single-step.
2588 * Then wait for the victim to stop and cancel the FLTTRACE.
2591 *ctlp
++ = PRCSIG
| PRCFAULT
| PRSTEP
;
2596 * Restore the current watchpoint.
2599 (void) memcpy(ctlp
, wp
, sizeof (prwatch_t
));
2600 ctlp
+= sizeof (prwatch_t
) / sizeof (long);
2603 * Restore fault tracing set if we modified it.
2605 if (!(prismember(faultset
, FLTTRACE
))) {
2607 *(fltset_t
*)ctlp
= *faultset
;
2608 ctlp
+= sizeof (fltset_t
) / sizeof (long);
2612 * Restore the hold mask to the current hold mask (i.e. the one
2613 * before we executed any of the previous operations).
2616 *(sigset_t
*)ctlp
= *sigmask
;
2617 ctlp
+= sizeof (sigset_t
) / sizeof (long);
2619 size
= (char *)ctlp
- (char *)ctl
;
2620 if ((ssize
= write(ctlfd
, ctl
, size
)) != size
)
2621 error
= (ssize
== -1)? errno
: EINTR
;
2622 (void) sigprocmask(SIG_SETMASK
, &unblock
, NULL
);
2627 * Step over a watchpoint, i.e., execute the instruction that was stopped by
2628 * the watchpoint, and then leave the LWP stopped at the next instruction.
2631 Pxecwapt(struct ps_prochandle
*P
, const prwatch_t
*wp
)
2633 int ctlfd
= (P
->agentctlfd
>= 0)? P
->agentctlfd
: P
->ctlfd
;
2636 if (P
->state
!= PS_STOP
) {
2642 error
= execute_wapt(ctlfd
,
2643 &P
->status
.pr_flttrace
, &P
->status
.pr_lwp
.pr_lwphold
, wp
);
2644 rv
= Pstopstatus(P
, PCNULL
, 0);
2647 if (P
->status
.pr_lwp
.pr_why
== PR_JOBCONTROL
&&
2648 error
== EBUSY
) { /* jobcontrol stop -- back off */
2652 if (error
== ENOENT
)
2662 Psetflags(struct ps_prochandle
*P
, long flags
)
2670 if (write(P
->ctlfd
, ctl
, 2*sizeof (long)) != 2*sizeof (long)) {
2673 P
->status
.pr_flags
|= flags
;
2674 P
->status
.pr_lwp
.pr_flags
|= flags
;
2682 Punsetflags(struct ps_prochandle
*P
, long flags
)
2690 if (write(P
->ctlfd
, ctl
, 2*sizeof (long)) != 2*sizeof (long)) {
2693 P
->status
.pr_flags
&= ~flags
;
2694 P
->status
.pr_lwp
.pr_flags
&= ~flags
;
2702 * Common function to allow clients to manipulate the action to be taken
2703 * on receipt of a signal, receipt of machine fault, entry to a system call,
2704 * or exit from a system call. We make use of our private prset_* functions
2705 * in order to make this code be common. The 'which' parameter identifies
2706 * the code for the event of interest (0 means change the entire set), and
2707 * the 'stop' parameter is a boolean indicating whether the process should
2708 * stop when the event of interest occurs. The previous value is returned
2709 * to the caller; -1 is returned if an error occurred.
2712 Psetaction(struct ps_prochandle
*P
, void *sp
, size_t size
,
2713 uint_t flag
, int max
, int which
, int stop
)
2717 if (which
< 0 || which
> max
) {
2722 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2723 P
->state
== PS_IDLE
) {
2728 oldval
= prset_ismember(sp
, size
, which
) ? TRUE
: FALSE
;
2732 prset_fill(sp
, size
);
2734 } else if (!oldval
) {
2735 prset_add(sp
, size
, which
);
2740 prset_empty(sp
, size
);
2742 } else if (oldval
) {
2743 prset_del(sp
, size
, which
);
2748 if (P
->state
== PS_RUN
)
2755 * Set action on specified signal.
2758 Psignal(struct ps_prochandle
*P
, int which
, int stop
)
2762 if (which
== SIGKILL
&& stop
!= 0) {
2767 oldval
= Psetaction(P
, &P
->status
.pr_sigtrace
, sizeof (sigset_t
),
2768 SETSIG
, PRMAXSIG
, which
, stop
);
2770 if (oldval
!= -1 && which
== 0 && stop
!= 0)
2771 prdelset(&P
->status
.pr_sigtrace
, SIGKILL
);
2777 * Set all signal tracing flags.
2780 Psetsignal(struct ps_prochandle
*P
, const sigset_t
*set
)
2782 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2783 P
->state
== PS_IDLE
)
2786 P
->status
.pr_sigtrace
= *set
;
2789 if (P
->state
== PS_RUN
)
2794 * Set action on specified fault.
2797 Pfault(struct ps_prochandle
*P
, int which
, int stop
)
2799 return (Psetaction(P
, &P
->status
.pr_flttrace
, sizeof (fltset_t
),
2800 SETFAULT
, PRMAXFAULT
, which
, stop
));
2804 * Set all machine fault tracing flags.
2807 Psetfault(struct ps_prochandle
*P
, const fltset_t
*set
)
2809 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2810 P
->state
== PS_IDLE
)
2813 P
->status
.pr_flttrace
= *set
;
2814 P
->flags
|= SETFAULT
;
2816 if (P
->state
== PS_RUN
)
2821 * Set action on specified system call entry.
2824 Psysentry(struct ps_prochandle
*P
, int which
, int stop
)
2826 return (Psetaction(P
, &P
->status
.pr_sysentry
, sizeof (sysset_t
),
2827 SETENTRY
, PRMAXSYS
, which
, stop
));
2831 * Set all system call entry tracing flags.
2834 Psetsysentry(struct ps_prochandle
*P
, const sysset_t
*set
)
2836 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2837 P
->state
== PS_IDLE
)
2840 P
->status
.pr_sysentry
= *set
;
2841 P
->flags
|= SETENTRY
;
2843 if (P
->state
== PS_RUN
)
2848 * Set action on specified system call exit.
2851 Psysexit(struct ps_prochandle
*P
, int which
, int stop
)
2853 return (Psetaction(P
, &P
->status
.pr_sysexit
, sizeof (sysset_t
),
2854 SETEXIT
, PRMAXSYS
, which
, stop
));
2858 * Set all system call exit tracing flags.
2861 Psetsysexit(struct ps_prochandle
*P
, const sysset_t
*set
)
2863 if (P
->state
== PS_DEAD
|| P
->state
== PS_UNDEAD
||
2864 P
->state
== PS_IDLE
)
2867 P
->status
.pr_sysexit
= *set
;
2868 P
->flags
|= SETEXIT
;
2870 if (P
->state
== PS_RUN
)
2875 * Utility function to read the contents of a file that contains a
2876 * prheader_t at the start (/proc/pid/lstatus or /proc/pid/lpsinfo).
2877 * Returns a malloc()d buffer or NULL on failure.
2880 read_lfile(struct ps_prochandle
*P
, const char *lname
)
2883 char lpath
[PATH_MAX
];
2884 struct stat64 statb
;
2889 (void) snprintf(lpath
, sizeof (lpath
), "%s/%d/%s", procfs_path
,
2890 (int)P
->status
.pr_pid
, lname
);
2891 if ((fd
= open(lpath
, O_RDONLY
)) < 0 || fstat64(fd
, &statb
) != 0) {
2898 * 'size' is just the initial guess at the buffer size.
2899 * It will have to grow if the number of lwps increases
2900 * while we are looking at the process.
2901 * 'size' must be larger than the actual file size.
2903 size
= statb
.st_size
+ 32;
2906 if ((Lhp
= malloc(size
)) == NULL
)
2908 if ((rval
= pread(fd
, Lhp
, size
, 0)) < 0 ||
2909 rval
<= sizeof (prheader_t
)) {
2916 /* need a bigger buffer */
2926 * LWP iteration interface.
2929 Plwp_iter(struct ps_prochandle
*P
, proc_lwp_f
*func
, void *cd
)
2938 (void) Pstopstatus(P
, PCNULL
, 0);
2951 * For either live processes or cores, the single LWP case is easy:
2952 * the pstatus_t contains the lwpstatus_t for the only LWP.
2954 if (P
->status
.pr_nlwp
<= 1)
2955 return (func(cd
, &P
->status
.pr_lwp
));
2958 * For the core file multi-LWP case, we just iterate through the
2959 * list of LWP structs we read in from the core file.
2961 if (P
->state
== PS_DEAD
) {
2962 core_info_t
*core
= P
->data
;
2963 lwp_info_t
*lwp
= list_prev(&core
->core_lwp_head
);
2966 for (i
= 0; i
< core
->core_nlwp
; i
++, lwp
= list_prev(lwp
)) {
2967 if (lwp
->lwp_psinfo
.pr_sname
!= 'Z' &&
2968 (rv
= func(cd
, &lwp
->lwp_status
)) != 0)
2976 * For the live process multi-LWP case, we have to work a little
2977 * harder: the /proc/pid/lstatus file has the array of LWP structs.
2979 if ((Lhp
= Plstatus(P
)) == NULL
)
2982 for (nlwp
= Lhp
->pr_nent
, Lsp
= (lwpstatus_t
*)(uintptr_t)(Lhp
+ 1);
2984 nlwp
--, Lsp
= (lwpstatus_t
*)((uintptr_t)Lsp
+ Lhp
->pr_entsize
)) {
2985 if ((rv
= func(cd
, Lsp
)) != 0)
2994 * Extended LWP iteration interface.
2995 * Iterate over all LWPs, active and zombie.
2998 Plwp_iter_all(struct ps_prochandle
*P
, proc_lwp_all_f
*func
, void *cd
)
3000 prheader_t
*Lhp
= NULL
;
3003 prheader_t
*Lphp
= NULL
;
3014 if (P
->state
== PS_RUN
)
3015 (void) Pstopstatus(P
, PCNULL
, 0);
3018 if (P
->state
== PS_STOP
)
3022 * For either live processes or cores, the single LWP case is easy:
3023 * the pstatus_t contains the lwpstatus_t for the only LWP and
3024 * the psinfo_t contains the lwpsinfo_t for the only LWP.
3026 if (P
->status
.pr_nlwp
+ P
->status
.pr_nzomb
<= 1)
3027 return (func(cd
, &P
->status
.pr_lwp
, &P
->psinfo
.pr_lwp
));
3030 * For the core file multi-LWP case, we just iterate through the
3031 * list of LWP structs we read in from the core file.
3033 if (P
->state
== PS_DEAD
) {
3034 core_info_t
*core
= P
->data
;
3035 lwp_info_t
*lwp
= list_prev(&core
->core_lwp_head
);
3038 for (i
= 0; i
< core
->core_nlwp
; i
++, lwp
= list_prev(lwp
)) {
3039 sp
= (lwp
->lwp_psinfo
.pr_sname
== 'Z')? NULL
:
3041 if ((rv
= func(cd
, sp
, &lwp
->lwp_psinfo
)) != 0)
3049 * For all other cases retrieve the array of lwpstatus_t's and
3052 if ((Lhp
= Plstatus(P
)) == NULL
)
3054 if ((Lphp
= Plpsinfo(P
)) == NULL
) {
3060 * If we are looking at a running process, or one we do not control,
3061 * the active and zombie lwps in the process may have changed since
3062 * we read the process status structure. If so, just start over.
3064 if (Lhp
->pr_nent
!= P
->status
.pr_nlwp
||
3065 Lphp
->pr_nent
!= P
->status
.pr_nlwp
+ P
->status
.pr_nzomb
)
3069 * To be perfectly safe, prescan the two arrays, checking consistency.
3070 * We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
3071 * same order (the lwp directory order) in their respective files.
3072 * We also rely on there being (possibly) more lwpsinfo_t's than
3073 * lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
3075 Lsp
= (lwpstatus_t
*)(uintptr_t)(Lhp
+ 1);
3076 Lpsp
= (lwpsinfo_t
*)(uintptr_t)(Lphp
+ 1);
3077 nstat
= Lhp
->pr_nent
;
3078 for (ninfo
= Lphp
->pr_nent
; ninfo
!= 0; ninfo
--) {
3079 if (Lpsp
->pr_sname
!= 'Z') {
3081 * Not a zombie lwp; check for matching lwpids.
3083 if (nstat
== 0 || Lsp
->pr_lwpid
!= Lpsp
->pr_lwpid
)
3085 Lsp
= (lwpstatus_t
*)((uintptr_t)Lsp
+ Lhp
->pr_entsize
);
3088 Lpsp
= (lwpsinfo_t
*)((uintptr_t)Lpsp
+ Lphp
->pr_entsize
);
3094 * Rescan, this time for real.
3096 Lsp
= (lwpstatus_t
*)(uintptr_t)(Lhp
+ 1);
3097 Lpsp
= (lwpsinfo_t
*)(uintptr_t)(Lphp
+ 1);
3098 for (ninfo
= Lphp
->pr_nent
; ninfo
!= 0; ninfo
--) {
3099 if (Lpsp
->pr_sname
!= 'Z') {
3101 Lsp
= (lwpstatus_t
*)((uintptr_t)Lsp
+ Lhp
->pr_entsize
);
3105 if ((rv
= func(cd
, sp
, Lpsp
)) != 0)
3107 Lpsp
= (lwpsinfo_t
*)((uintptr_t)Lpsp
+ Lphp
->pr_entsize
);
3116 Pcontent(struct ps_prochandle
*P
)
3118 core_info_t
*core
= P
->data
;
3120 if (P
->state
== PS_DEAD
)
3121 return (core
->core_content
);
3122 if (P
->state
== PS_IDLE
)
3123 return (CC_CONTENT_TEXT
| CC_CONTENT_DATA
| CC_CONTENT_CTF
);
3125 return (CC_CONTENT_ALL
);
3129 * =================================================================
3130 * The remainder of the functions in this file are for the
3131 * control of individual LWPs in the controlled process.
3132 * =================================================================
3136 * Find an entry in the process hash table for the specified lwpid.
3137 * The entry will either point to an existing struct ps_lwphandle
3138 * or it will point to an empty slot for a new struct ps_lwphandle.
3140 static struct ps_lwphandle
**
3141 Lfind(struct ps_prochandle
*P
, lwpid_t lwpid
)
3143 struct ps_lwphandle
**Lp
;
3144 struct ps_lwphandle
*L
;
3146 for (Lp
= &P
->hashtab
[lwpid
% (HASHSIZE
- 1)];
3147 (L
= *Lp
) != NULL
; Lp
= &L
->lwp_hash
)
3148 if (L
->lwp_id
== lwpid
)
3154 * Grab an LWP contained within the controlled process.
3155 * Return an opaque pointer to its LWP control structure.
3156 * perr: pointer to error return code.
3158 struct ps_lwphandle
*
3159 Lgrab(struct ps_prochandle
*P
, lwpid_t lwpid
, int *perr
)
3161 struct ps_lwphandle
**Lp
;
3162 struct ps_lwphandle
*L
;
3164 char procname
[PATH_MAX
];
3168 (void) mutex_lock(&P
->proc_lock
);
3170 if (P
->state
== PS_UNDEAD
|| P
->state
== PS_IDLE
)
3172 else if (P
->hashtab
== NULL
&&
3173 (P
->hashtab
= calloc(HASHSIZE
, sizeof (struct ps_lwphandle
*)))
3176 else if (*(Lp
= Lfind(P
, lwpid
)) != NULL
)
3178 else if ((L
= malloc(sizeof (struct ps_lwphandle
))) == NULL
)
3182 (void) mutex_unlock(&P
->proc_lock
);
3186 (void) memset(L
, 0, sizeof (*L
));
3191 *Lp
= L
; /* insert into the hash table */
3193 if (P
->state
== PS_DEAD
) { /* core file */
3194 if (getlwpstatus(P
, lwpid
, &L
->lwp_status
) == -1) {
3198 L
->lwp_state
= PS_DEAD
;
3200 (void) mutex_unlock(&P
->proc_lock
);
3205 * Open the /proc/<pid>/lwp/<lwpid> files
3207 (void) snprintf(procname
, sizeof (procname
), "%s/%d/lwp/%d/",
3208 procfs_path
, (int)P
->pid
, (int)lwpid
);
3209 fname
= procname
+ strlen(procname
);
3212 (void) strcpy(fname
, "lwpstatus");
3213 if ((fd
= open(procname
, O_RDONLY
)) < 0 ||
3214 (fd
= dupfd(fd
, 0)) < 0) {
3220 dprintf("Lgrab: failed to open %s: %s\n",
3221 procname
, strerror(errno
));
3229 if (pread(fd
, &L
->lwp_status
, sizeof (L
->lwp_status
), (off_t
)0) < 0) {
3235 dprintf("Lgrab: failed to read %s: %s\n",
3236 procname
, strerror(errno
));
3243 (void) strcpy(fname
, "lwpctl");
3244 if ((fd
= open(procname
, O_WRONLY
)) < 0 ||
3245 (fd
= dupfd(fd
, 0)) < 0) {
3251 dprintf("Lgrab: failed to open %s: %s\n",
3252 procname
, strerror(errno
));
3261 ((L
->lwp_status
.pr_flags
& (PR_STOPPED
|PR_ISTOP
))
3262 == (PR_STOPPED
|PR_ISTOP
))?
3266 (void) mutex_unlock(&P
->proc_lock
);
3270 Lfree_internal(P
, L
);
3272 (void) mutex_unlock(&P
->proc_lock
);
3277 * Return a printable string corresponding to an Lgrab() error return.
3280 Lgrab_error(int error
)
3286 str
= "no such LWP";
3289 str
= "LWP already grabbed";
3292 str
= "unanticipated system error";
3295 str
= "unknown error";
3303 * Free an LWP control structure.
3306 Lfree(struct ps_lwphandle
*L
)
3308 struct ps_prochandle
*P
= L
->lwp_proc
;
3310 (void) mutex_lock(&P
->proc_lock
);
3311 Lfree_internal(P
, L
);
3312 (void) mutex_unlock(&P
->proc_lock
);
3316 Lfree_internal(struct ps_prochandle
*P
, struct ps_lwphandle
*L
)
3318 *Lfind(P
, L
->lwp_id
) = L
->lwp_hash
; /* delete from hash table */
3319 if (L
->lwp_ctlfd
>= 0)
3320 (void) close(L
->lwp_ctlfd
);
3321 if (L
->lwp_statfd
>= 0)
3322 (void) close(L
->lwp_statfd
);
3324 /* clear out the structure as a precaution against reuse */
3325 (void) memset(L
, 0, sizeof (*L
));
3333 * Return the state of the process, one of the PS_* values.
3336 Lstate(struct ps_lwphandle
*L
)
3338 return (L
->lwp_state
);
3342 * Return the open control file descriptor for the LWP.
3343 * Clients must not close this file descriptor, nor use it
3344 * after the LWP is freed.
3347 Lctlfd(struct ps_lwphandle
*L
)
3349 return (L
->lwp_ctlfd
);
3353 * Return a pointer to the LWP lwpsinfo structure.
3354 * Clients should not hold on to this pointer indefinitely.
3355 * It will become invalid on Lfree().
3358 Lpsinfo(struct ps_lwphandle
*L
)
3360 if (Plwp_getpsinfo(L
->lwp_proc
, L
->lwp_id
, &L
->lwp_psinfo
) == -1)
3363 return (&L
->lwp_psinfo
);
3367 * Return a pointer to the LWP status structure.
3368 * Clients should not hold on to this pointer indefinitely.
3369 * It will become invalid on Lfree().
3372 Lstatus(struct ps_lwphandle
*L
)
3374 return (&L
->lwp_status
);
3378 * Given an LWP handle, return the process handle.
3380 struct ps_prochandle
*
3381 Lprochandle(struct ps_lwphandle
*L
)
3383 return (L
->lwp_proc
);
3387 * Ensure that all cached state is written to the LWP.
3388 * The cached state is the LWP's signal mask and registers.
3391 Lsync(struct ps_lwphandle
*L
)
3393 int ctlfd
= L
->lwp_ctlfd
;
3398 if (L
->lwp_flags
& SETHOLD
) {
3400 iov
[n
].iov_base
= (caddr_t
)&cmd
[0];
3401 iov
[n
++].iov_len
= sizeof (long);
3402 iov
[n
].iov_base
= (caddr_t
)&L
->lwp_status
.pr_lwphold
;
3403 iov
[n
++].iov_len
= sizeof (L
->lwp_status
.pr_lwphold
);
3405 if (L
->lwp_flags
& SETREGS
) {
3407 iov
[n
].iov_base
= (caddr_t
)&cmd
[1];
3408 iov
[n
++].iov_len
= sizeof (long);
3409 iov
[n
].iov_base
= (caddr_t
)&L
->lwp_status
.pr_reg
[0];
3410 iov
[n
++].iov_len
= sizeof (L
->lwp_status
.pr_reg
);
3413 if (n
== 0 || writev(ctlfd
, iov
, n
) < 0)
3414 return; /* nothing to do or write failed */
3416 L
->lwp_flags
&= ~(SETHOLD
|SETREGS
);
3420 * Wait for the specified LWP to stop or terminate.
3421 * Or, just get the current status (PCNULL).
3422 * Or, direct it to stop and get the current status (PCDSTOP).
3425 Lstopstatus(struct ps_lwphandle
*L
,
3426 long request
, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
3427 uint_t msec
) /* if non-zero, timeout in milliseconds */
3429 int ctlfd
= L
->lwp_ctlfd
;
3434 switch (L
->lwp_state
) {
3438 if (request
!= PCNULL
&& request
!= PCDSTOP
)
3442 if (request
!= PCNULL
) {
3449 if (request
!= PCNULL
) {
3454 default: /* corrupted state */
3455 dprintf("Lstopstatus: corrupted state: %d\n", L
->lwp_state
);
3462 ctl
[2] = (long)msec
;
3466 rc
= write(ctlfd
, &ctl
[0], 3*sizeof (long));
3469 rc
= write(ctlfd
, &ctl
[1], 2*sizeof (long));
3472 rc
= write(ctlfd
, &ctl
[0], 1*sizeof (long));
3475 if (L
->lwp_state
== PS_DEAD
)
3476 return (0); /* Nothing else to do for cores */
3478 default: /* programming error */
3482 err
= (rc
< 0)? errno
: 0;
3485 if (pread(L
->lwp_statfd
, &L
->lwp_status
,
3486 sizeof (L
->lwp_status
), (off_t
)0) < 0)
3491 case EINTR
: /* user typed ctl-C */
3493 dprintf("Lstopstatus: EINTR\n");
3495 case EAGAIN
: /* we lost control of the the process */
3496 dprintf("Lstopstatus: EAGAIN\n");
3497 L
->lwp_state
= PS_LOST
;
3501 if (_libproc_debug
) {
3506 errstr
= "Lstopstatus PCNULL"; break;
3508 errstr
= "Lstopstatus PCSTOP"; break;
3510 errstr
= "Lstopstatus PCDSTOP"; break;
3512 errstr
= "Lstopstatus PCWSTOP"; break;
3514 errstr
= "Lstopstatus PC???"; break;
3516 dprintf("%s: %s\n", errstr
, strerror(err
));
3518 L
->lwp_state
= PS_UNDEAD
;
3524 if ((L
->lwp_status
.pr_flags
& (PR_STOPPED
|PR_ISTOP
))
3525 != (PR_STOPPED
|PR_ISTOP
)) {
3526 L
->lwp_state
= PS_RUN
;
3527 if (request
== PCNULL
|| request
== PCDSTOP
|| msec
!= 0)
3529 dprintf("Lstopstatus: LWP is not stopped\n");
3534 L
->lwp_state
= PS_STOP
;
3536 if (_libproc_debug
) /* debugging */
3537 prldump("Lstopstatus", &L
->lwp_status
);
3539 switch (L
->lwp_status
.pr_why
) {
3557 * Wait for the LWP to stop for any reason.
3560 Lwait(struct ps_lwphandle
*L
, uint_t msec
)
3562 return (Lstopstatus(L
, PCWSTOP
, msec
));
3566 * Direct the LWP to stop; wait for it to stop.
3569 Lstop(struct ps_lwphandle
*L
, uint_t msec
)
3571 return (Lstopstatus(L
, PCSTOP
, msec
));
3575 * Direct the LWP to stop; don't wait.
3578 Ldstop(struct ps_lwphandle
*L
)
3580 return (Lstopstatus(L
, PCDSTOP
, 0));
3584 * Get the value of one register from stopped LWP.
3587 Lgetareg(struct ps_lwphandle
*L
, int regno
, prgreg_t
*preg
)
3589 if (regno
< 0 || regno
>= NPRGREG
) {
3594 if (L
->lwp_state
!= PS_STOP
) {
3599 *preg
= L
->lwp_status
.pr_reg
[regno
];
3604 * Put value of one register into stopped LWP.
3607 Lputareg(struct ps_lwphandle
*L
, int regno
, prgreg_t reg
)
3609 if (regno
< 0 || regno
>= NPRGREG
) {
3614 if (L
->lwp_state
!= PS_STOP
) {
3619 L
->lwp_status
.pr_reg
[regno
] = reg
;
3620 L
->lwp_flags
|= SETREGS
; /* set registers before continuing */
3625 Lsetrun(struct ps_lwphandle
*L
,
3626 int sig
, /* signal to pass to LWP */
3627 int flags
) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
3629 int ctlfd
= L
->lwp_ctlfd
;
3630 int sbits
= (PR_DSTOP
| PR_ISTOP
| PR_ASLEEP
);
3632 long ctl
[1 + /* PCCFAULT */
3633 1 + sizeof (siginfo_t
)/sizeof (long) + /* PCSSIG/PCCSIG */
3639 if (L
->lwp_state
!= PS_STOP
&&
3640 (L
->lwp_status
.pr_flags
& sbits
) == 0) {
3645 Lsync(L
); /* flush registers */
3647 if (flags
& PRCFAULT
) { /* clear current fault */
3652 if (flags
& PRCSIG
) { /* clear current signal */
3655 } else if (sig
&& sig
!= L
->lwp_status
.pr_cursig
) {
3656 /* make current signal */
3660 infop
= (siginfo_t
*)ctlp
;
3661 (void) memset(infop
, 0, sizeof (*infop
));
3662 infop
->si_signo
= sig
;
3663 ctlp
+= sizeof (siginfo_t
) / sizeof (long);
3668 size
= (char *)ctlp
- (char *)ctl
;
3670 L
->lwp_proc
->info_valid
= 0; /* will need to update map and file info */
3671 L
->lwp_proc
->state
= PS_RUN
;
3672 L
->lwp_state
= PS_RUN
;
3674 if (write(ctlfd
, ctl
, size
) != size
) {
3675 /* Pretend that a job-stopped LWP is running */
3676 if (errno
!= EBUSY
|| L
->lwp_status
.pr_why
!= PR_JOBCONTROL
)
3677 return (Lstopstatus(L
, PCNULL
, 0));
3684 Lclearsig(struct ps_lwphandle
*L
)
3686 int ctlfd
= L
->lwp_ctlfd
;
3689 if (write(ctlfd
, &ctl
, sizeof (ctl
)) != sizeof (ctl
))
3691 L
->lwp_status
.pr_cursig
= 0;
3696 Lclearfault(struct ps_lwphandle
*L
)
3698 int ctlfd
= L
->lwp_ctlfd
;
3699 long ctl
= PCCFAULT
;
3701 if (write(ctlfd
, &ctl
, sizeof (ctl
)) != sizeof (ctl
))
3707 * Step over a breakpoint, i.e., execute the instruction that
3708 * really belongs at the breakpoint location (the current %pc)
3709 * and leave the LWP stopped at the next instruction.
3712 Lxecbkpt(struct ps_lwphandle
*L
, ulong_t saved
)
3714 struct ps_prochandle
*P
= L
->lwp_proc
;
3717 if (L
->lwp_state
!= PS_STOP
) {
3723 error
= execute_bkpt(L
->lwp_ctlfd
,
3724 &P
->status
.pr_flttrace
, &L
->lwp_status
.pr_lwphold
,
3725 L
->lwp_status
.pr_reg
[R_PC
], saved
);
3726 rv
= Lstopstatus(L
, PCNULL
, 0);
3729 if (L
->lwp_status
.pr_why
== PR_JOBCONTROL
&&
3730 error
== EBUSY
) { /* jobcontrol stop -- back off */
3731 L
->lwp_state
= PS_RUN
;
3734 if (error
== ENOENT
)
3744 * Step over a watchpoint, i.e., execute the instruction that was stopped by
3745 * the watchpoint, and then leave the LWP stopped at the next instruction.
3748 Lxecwapt(struct ps_lwphandle
*L
, const prwatch_t
*wp
)
3750 struct ps_prochandle
*P
= L
->lwp_proc
;
3753 if (L
->lwp_state
!= PS_STOP
) {
3759 error
= execute_wapt(L
->lwp_ctlfd
,
3760 &P
->status
.pr_flttrace
, &L
->lwp_status
.pr_lwphold
, wp
);
3761 rv
= Lstopstatus(L
, PCNULL
, 0);
3764 if (L
->lwp_status
.pr_why
== PR_JOBCONTROL
&&
3765 error
== EBUSY
) { /* jobcontrol stop -- back off */
3766 L
->lwp_state
= PS_RUN
;
3769 if (error
== ENOENT
)
3779 Lstack(struct ps_lwphandle
*L
, stack_t
*stkp
)
3781 struct ps_prochandle
*P
= L
->lwp_proc
;
3782 uintptr_t addr
= L
->lwp_status
.pr_ustack
;
3784 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
3785 if (Pread(P
, stkp
, sizeof (*stkp
), addr
) != sizeof (*stkp
))
3791 if (Pread(P
, &stk32
, sizeof (stk32
), addr
) != sizeof (stk32
))
3794 stack_32_to_n(&stk32
, stkp
);
3802 Lmain_stack(struct ps_lwphandle
*L
, stack_t
*stkp
)
3804 struct ps_prochandle
*P
= L
->lwp_proc
;
3806 if (Lstack(L
, stkp
) != 0)
3810 * If the SS_ONSTACK flag is set then this LWP is operating on the
3811 * alternate signal stack. We can recover the original stack from
3814 if (!(stkp
->ss_flags
& SS_ONSTACK
))
3817 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
3818 ucontext_t
*ctxp
= (void *)L
->lwp_status
.pr_oldcontext
;
3820 if (Pread(P
, stkp
, sizeof (*stkp
),
3821 (uintptr_t)&ctxp
->uc_stack
) != sizeof (*stkp
))
3825 ucontext32_t
*ctxp
= (void *)L
->lwp_status
.pr_oldcontext
;
3828 if (Pread(P
, &stk32
, sizeof (stk32
),
3829 (uintptr_t)&ctxp
->uc_stack
) != sizeof (stk32
))
3832 stack_32_to_n(&stk32
, stkp
);
3840 Lalt_stack(struct ps_lwphandle
*L
, stack_t
*stkp
)
3842 if (L
->lwp_status
.pr_altstack
.ss_flags
& SS_DISABLE
) {
3847 *stkp
= L
->lwp_status
.pr_altstack
;
3853 * Add a mapping to the given proc handle. Resizes the array as appropriate and
3854 * manages reference counts on the given file_info_t.
3856 * The 'map_relocate' member is used to tell Psort_mappings() that the
3857 * associated file_map pointer needs to be relocated after the mappings have
3858 * been sorted. It is only set for the first mapping, and has no meaning
3859 * outside these two functions.
3862 Padd_mapping(struct ps_prochandle
*P
, off64_t off
, file_info_t
*fp
,
3867 if (P
->map_count
== P
->map_alloc
) {
3868 size_t next
= P
->map_alloc
? P
->map_alloc
* 2 : 16;
3870 if ((P
->mappings
= realloc(P
->mappings
,
3871 next
* sizeof (map_info_t
))) == NULL
)
3874 P
->map_alloc
= next
;
3877 mp
= &P
->mappings
[P
->map_count
++];
3879 mp
->map_offset
= off
;
3880 mp
->map_pmap
= *pmap
;
3881 mp
->map_relocate
= 0;
3882 if ((mp
->map_file
= fp
) != NULL
) {
3883 if (fp
->file_map
== NULL
) {
3885 mp
->map_relocate
= 1;
3894 map_sort(const void *a
, const void *b
)
3896 const map_info_t
*ap
= a
, *bp
= b
;
3898 if (ap
->map_pmap
.pr_vaddr
< bp
->map_pmap
.pr_vaddr
)
3900 else if (ap
->map_pmap
.pr_vaddr
> bp
->map_pmap
.pr_vaddr
)
3907 * Sort the current set of mappings. Should be called during target
3908 * initialization after all calls to Padd_mapping() have been made.
3911 Psort_mappings(struct ps_prochandle
*P
)
3916 qsort(P
->mappings
, P
->map_count
, sizeof (map_info_t
), map_sort
);
3919 * Update all the file_map pointers to refer to the new locations.
3921 for (i
= 0; i
< P
->map_count
; i
++) {
3922 mp
= &P
->mappings
[i
];
3923 if (mp
->map_relocate
)
3924 mp
->map_file
->file_map
= mp
;
3925 mp
->map_relocate
= 0;
3929 struct ps_prochandle
*
3930 Pgrab_ops(pid_t pid
, void *data
, const ps_ops_t
*ops
, int flags
)
3932 struct ps_prochandle
*P
;
3934 if ((P
= calloc(1, sizeof (*P
))) == NULL
) {
3938 Pinit_ops(&P
->ops
, ops
);
3939 (void) mutex_init(&P
->proc_lock
, USYNC_THREAD
, NULL
);
3946 P
->agentstatfd
= -1;
3951 if (flags
& PGRAB_INCORE
) {