Print this page
3946 ::gcore
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libproc/common/Pcontrol.c
+++ new/usr/src/lib/libproc/common/Pcontrol.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 *
26 26 * Portions Copyright 2007 Chad Mynhier
27 27 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
28 + * Copyright (c) 2013 by Delphix. All rights reserved.
28 29 */
29 30
30 31 #include <assert.h>
31 32 #include <stdio.h>
32 33 #include <stdlib.h>
33 34 #include <unistd.h>
34 35 #include <ctype.h>
35 36 #include <fcntl.h>
36 37 #include <string.h>
37 38 #include <strings.h>
38 39 #include <memory.h>
39 40 #include <errno.h>
40 41 #include <dirent.h>
41 42 #include <limits.h>
42 43 #include <signal.h>
43 44 #include <atomic.h>
45 +#include <zone.h>
44 46 #include <sys/types.h>
45 47 #include <sys/uio.h>
46 48 #include <sys/stat.h>
47 49 #include <sys/resource.h>
48 50 #include <sys/param.h>
49 51 #include <sys/stack.h>
50 52 #include <sys/fault.h>
51 53 #include <sys/syscall.h>
52 54 #include <sys/sysmacros.h>
55 +#include <sys/systeminfo.h>
53 56
54 57 #include "libproc.h"
55 58 #include "Pcontrol.h"
56 59 #include "Putil.h"
57 60 #include "P32ton.h"
58 61
59 62 int _libproc_debug; /* set non-zero to enable debugging printfs */
60 63 int _libproc_no_qsort; /* set non-zero to inhibit sorting */
61 64 /* of symbol tables */
62 65 int _libproc_incore_elf; /* only use in-core elf data */
63 66
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
64 67 sigset_t blockable_sigs; /* signals to block when we need to be safe */
65 68 static int minfd; /* minimum file descriptor returned by dupfd(fd, 0) */
66 69 char procfs_path[PATH_MAX] = "/proc";
67 70
68 71 /*
69 72 * Function prototypes for static routines in this module.
70 73 */
71 74 static void deadcheck(struct ps_prochandle *);
72 75 static void restore_tracing_flags(struct ps_prochandle *);
73 76 static void Lfree_internal(struct ps_prochandle *, struct ps_lwphandle *);
77 +static prheader_t *read_lfile(struct ps_prochandle *, const char *);
74 78
75 79 /*
76 - * Read/write interface for live processes: just pread/pwrite the
77 - * /proc/<pid>/as file:
80 + * Ops vector functions for live processes.
78 81 */
79 82
83 +/*ARGSUSED*/
80 84 static ssize_t
81 -Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)
85 +Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
86 + void *data)
82 87 {
83 88 return (pread(P->asfd, buf, n, (off_t)addr));
84 89 }
85 90
91 +/*ARGSUSED*/
86 92 static ssize_t
87 -Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)
93 +Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr,
94 + void *data)
88 95 {
89 96 return (pwrite(P->asfd, buf, n, (off_t)addr));
90 97 }
91 98
92 -static const ps_rwops_t P_live_ops = { Pread_live, Pwrite_live };
99 +/*ARGSUSED*/
100 +static int
101 +Pread_maps_live(struct ps_prochandle *P, prmap_t **Pmapp, ssize_t *nmapp,
102 + void *data)
103 +{
104 + char mapfile[PATH_MAX];
105 + int mapfd;
106 + struct stat statb;
107 + ssize_t nmap;
108 + prmap_t *Pmap = NULL;
93 109
110 + (void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
111 + procfs_path, (int)P->pid);
112 + if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
113 + fstat(mapfd, &statb) != 0 ||
114 + statb.st_size < sizeof (prmap_t) ||
115 + (Pmap = malloc(statb.st_size)) == NULL ||
116 + (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
117 + (nmap /= sizeof (prmap_t)) == 0) {
118 + if (Pmap != NULL)
119 + free(Pmap);
120 + if (mapfd >= 0)
121 + (void) close(mapfd);
122 + Preset_maps(P); /* utter failure; destroy tables */
123 + return (-1);
124 + }
125 + (void) close(mapfd);
126 +
127 + *Pmapp = Pmap;
128 + *nmapp = nmap;
129 +
130 + return (0);
131 +}
132 +
133 +/*ARGSUSED*/
134 +static void
135 +Pread_aux_live(struct ps_prochandle *P, auxv_t **auxvp, int *nauxp, void *data)
136 +{
137 + char auxfile[64];
138 + int fd;
139 + struct stat statb;
140 + auxv_t *auxv;
141 + ssize_t naux;
142 +
143 + (void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
144 + procfs_path, (int)P->pid);
145 + if ((fd = open(auxfile, O_RDONLY)) < 0) {
146 + dprintf("%s: failed to open %s: %s\n",
147 + __func__, auxfile, strerror(errno));
148 + return;
149 + }
150 +
151 + if (fstat(fd, &statb) == 0 &&
152 + statb.st_size >= sizeof (auxv_t) &&
153 + (auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
154 + if ((naux = read(fd, auxv, statb.st_size)) < 0 ||
155 + (naux /= sizeof (auxv_t)) < 1) {
156 + dprintf("%s: read failed: %s\n",
157 + __func__, strerror(errno));
158 + free(auxv);
159 + } else {
160 + auxv[naux].a_type = AT_NULL;
161 + auxv[naux].a_un.a_val = 0L;
162 +
163 + *auxvp = auxv;
164 + *nauxp = (int)naux;
165 + }
166 + }
167 +
168 + (void) close(fd);
169 +}
170 +
171 +/*ARGSUSED*/
172 +static int
173 +Pcred_live(struct ps_prochandle *P, prcred_t *pcrp, int ngroups, void *data)
174 +{
175 + return (proc_get_cred(P->pid, pcrp, ngroups));
176 +}
177 +
178 +/*ARGSUSED*/
179 +static int
180 +Ppriv_live(struct ps_prochandle *P, prpriv_t **pprv, void *data)
181 +{
182 + prpriv_t *pp;
183 +
184 + pp = proc_get_priv(P->pid);
185 + if (pp == NULL) {
186 + return (-1);
187 + }
188 +
189 + *pprv = pp;
190 + return (0);
191 +}
192 +
193 +/*ARGSUSED*/
194 +static const psinfo_t *
195 +Ppsinfo_live(struct ps_prochandle *P, psinfo_t *psinfo, void *data)
196 +{
197 + if (proc_get_psinfo(P->pid, psinfo) == -1)
198 + return (NULL);
199 +
200 + return (psinfo);
201 +}
202 +
203 +/*ARGSUSED*/
204 +static prheader_t *
205 +Plstatus_live(struct ps_prochandle *P, void *data)
206 +{
207 + return (read_lfile(P, "lstatus"));
208 +}
209 +
210 +/*ARGSUSED*/
211 +static prheader_t *
212 +Plpsinfo_live(struct ps_prochandle *P, void *data)
213 +{
214 + return (read_lfile(P, "lpsinfo"));
215 +}
216 +
217 +/*ARGSUSED*/
218 +static char *
219 +Pplatform_live(struct ps_prochandle *P, char *s, size_t n, void *data)
220 +{
221 + if (sysinfo(SI_PLATFORM, s, n) == -1)
222 + return (NULL);
223 + return (s);
224 +}
225 +
226 +/*ARGSUSED*/
227 +static int
228 +Puname_live(struct ps_prochandle *P, struct utsname *u, void *data)
229 +{
230 + return (uname(u));
231 +}
232 +
233 +/*ARGSUSED*/
234 +static char *
235 +Pzonename_live(struct ps_prochandle *P, char *s, size_t n, void *data)
236 +{
237 + if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0)
238 + return (NULL);
239 + s[n - 1] = '\0';
240 + return (s);
241 +}
242 +
94 243 /*
244 + * Callback function for Pfindexec(). We return a match if we can stat the
245 + * suggested pathname and confirm its device and inode number match our
246 + * previous information about the /proc/<pid>/object/a.out file.
247 + */
248 +static int
249 +stat_exec(const char *path, void *arg)
250 +{
251 + struct stat64 *stp = arg;
252 + struct stat64 st;
253 +
254 + return (stat64(path, &st) == 0 && S_ISREG(st.st_mode) &&
255 + stp->st_dev == st.st_dev && stp->st_ino == st.st_ino);
256 +}
257 +
258 +/*ARGSUSED*/
259 +static char *
260 +Pexecname_live(struct ps_prochandle *P, char *buf, size_t buflen, void *data)
261 +{
262 + char exec_name[PATH_MAX];
263 + char cwd[PATH_MAX];
264 + char proc_cwd[64];
265 + struct stat64 st;
266 + int ret;
267 +
268 + /*
269 + * Try to get the path information first.
270 + */
271 + (void) snprintf(exec_name, sizeof (exec_name),
272 + "%s/%d/path/a.out", procfs_path, (int)P->pid);
273 + if ((ret = readlink(exec_name, buf, buflen - 1)) > 0) {
274 + buf[ret] = '\0';
275 + (void) Pfindobj(P, buf, buf, buflen);
276 + return (buf);
277 + }
278 +
279 + /*
280 + * Stat the executable file so we can compare Pfindexec's
281 + * suggestions to the actual device and inode number.
282 + */
283 + (void) snprintf(exec_name, sizeof (exec_name),
284 + "%s/%d/object/a.out", procfs_path, (int)P->pid);
285 +
286 + if (stat64(exec_name, &st) != 0 || !S_ISREG(st.st_mode))
287 + return (NULL);
288 +
289 + /*
290 + * Attempt to figure out the current working directory of the
291 + * target process. This only works if the target process has
292 + * not changed its current directory since it was exec'd.
293 + */
294 + (void) snprintf(proc_cwd, sizeof (proc_cwd),
295 + "%s/%d/path/cwd", procfs_path, (int)P->pid);
296 +
297 + if ((ret = readlink(proc_cwd, cwd, PATH_MAX - 1)) > 0)
298 + cwd[ret] = '\0';
299 +
300 + (void) Pfindexec(P, ret > 0 ? cwd : NULL, stat_exec, &st);
301 +
302 + return (NULL);
303 +}
304 +
305 +#if defined(__i386) || defined(__amd64)
306 +/*ARGSUSED*/
307 +static int
308 +Pldt_live(struct ps_prochandle *P, struct ssd *pldt, int nldt, void *data)
309 +{
310 + return (proc_get_ldt(P->pid, pldt, nldt));
311 +}
312 +#endif
313 +
314 +static const ps_ops_t P_live_ops = {
315 + .pop_pread = Pread_live,
316 + .pop_pwrite = Pwrite_live,
317 + .pop_read_maps = Pread_maps_live,
318 + .pop_read_aux = Pread_aux_live,
319 + .pop_cred = Pcred_live,
320 + .pop_priv = Ppriv_live,
321 + .pop_psinfo = Ppsinfo_live,
322 + .pop_lstatus = Plstatus_live,
323 + .pop_lpsinfo = Plpsinfo_live,
324 + .pop_platform = Pplatform_live,
325 + .pop_uname = Puname_live,
326 + .pop_zonename = Pzonename_live,
327 + .pop_execname = Pexecname_live,
328 +#if defined(__i386) || defined(__amd64)
329 + .pop_ldt = Pldt_live
330 +#endif
331 +};
332 +
333 +/*
95 334 * This is the library's .init handler.
96 335 */
97 336 #pragma init(_libproc_init)
98 337 void
99 338 _libproc_init(void)
100 339 {
101 340 _libproc_debug = getenv("LIBPROC_DEBUG") != NULL;
102 341 _libproc_no_qsort = getenv("LIBPROC_NO_QSORT") != NULL;
103 342 _libproc_incore_elf = getenv("LIBPROC_INCORE_ELF") != NULL;
104 343
105 344 (void) sigfillset(&blockable_sigs);
106 345 (void) sigdelset(&blockable_sigs, SIGKILL);
107 346 (void) sigdelset(&blockable_sigs, SIGSTOP);
108 347 }
109 348
110 349 void
111 350 Pset_procfs_path(const char *path)
112 351 {
113 352 (void) snprintf(procfs_path, sizeof (procfs_path), "%s", path);
114 353 }
115 354
116 355 /*
117 356 * Call set_minfd() once before calling dupfd() several times.
118 357 * We assume that the application will not reduce its current file
119 358 * descriptor limit lower than 512 once it has set at least that value.
120 359 */
121 360 int
122 361 set_minfd(void)
123 362 {
124 363 static mutex_t minfd_lock = DEFAULTMUTEX;
125 364 struct rlimit rlim;
126 365 int fd;
127 366
128 367 if ((fd = minfd) < 256) {
129 368 (void) mutex_lock(&minfd_lock);
130 369 if ((fd = minfd) < 256) {
131 370 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0)
132 371 rlim.rlim_cur = rlim.rlim_max = 0;
133 372 if (rlim.rlim_cur >= 512)
134 373 fd = 256;
135 374 else if ((fd = rlim.rlim_cur / 2) < 3)
136 375 fd = 3;
137 376 membar_producer();
138 377 minfd = fd;
139 378 }
140 379 (void) mutex_unlock(&minfd_lock);
141 380 }
142 381 return (fd);
143 382 }
144 383
145 384 int
146 385 dupfd(int fd, int dfd)
147 386 {
148 387 int mfd;
149 388
150 389 /*
151 390 * Make fd be greater than 255 (the 32-bit stdio limit),
152 391 * or at least make it greater than 2 so that the
153 392 * program will work when spawned by init(1m).
154 393 * Also, if dfd is non-zero, dup the fd to be dfd.
155 394 */
156 395 if ((mfd = minfd) == 0)
157 396 mfd = set_minfd();
158 397 if (dfd > 0 || (0 <= fd && fd < mfd)) {
159 398 if (dfd <= 0)
160 399 dfd = mfd;
161 400 dfd = fcntl(fd, F_DUPFD, dfd);
162 401 (void) close(fd);
163 402 fd = dfd;
164 403 }
165 404 /*
166 405 * Mark it close-on-exec so any created process doesn't inherit it.
167 406 */
168 407 if (fd >= 0)
169 408 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
170 409 return (fd);
171 410 }
172 411
173 412 /*
174 413 * Create a new controlled process.
175 414 * Leave it stopped on successful exit from exec() or execve().
176 415 * Return an opaque pointer to its process control structure.
177 416 * Return NULL if process cannot be created (fork()/exec() not successful).
178 417 */
179 418 struct ps_prochandle *
180 419 Pxcreate(const char *file, /* executable file name */
181 420 char *const *argv, /* argument vector */
182 421 char *const *envp, /* environment */
183 422 int *perr, /* pointer to error return code */
184 423 char *path, /* if non-null, holds exec path name on return */
185 424 size_t len) /* size of the path buffer */
186 425 {
187 426 char execpath[PATH_MAX];
188 427 char procname[PATH_MAX];
189 428 struct ps_prochandle *P;
190 429 pid_t pid;
191 430 int fd;
192 431 char *fname;
193 432 int rc;
194 433 int lasterrno = 0;
195 434
196 435 if (len == 0) /* zero length, no path */
197 436 path = NULL;
198 437 if (path != NULL)
199 438 *path = '\0';
200 439
201 440 if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
202 441 *perr = C_STRANGE;
203 442 return (NULL);
204 443 }
205 444
206 445 if ((pid = fork1()) == -1) {
207 446 free(P);
208 447 *perr = C_FORK;
209 448 return (NULL);
210 449 }
211 450
212 451 if (pid == 0) { /* child process */
213 452 id_t id;
214 453 extern char **environ;
215 454
216 455 /*
217 456 * If running setuid or setgid, reset credentials to normal.
218 457 */
219 458 if ((id = getgid()) != getegid())
220 459 (void) setgid(id);
221 460 if ((id = getuid()) != geteuid())
222 461 (void) setuid(id);
223 462
224 463 Pcreate_callback(P); /* execute callback (see below) */
225 464 (void) pause(); /* wait for PRSABORT from parent */
226 465
227 466 /*
228 467 * This is ugly. There is no execvep() function that takes a
229 468 * path and an environment. We cheat here by replacing the
230 469 * global 'environ' variable right before we call this.
231 470 */
232 471 if (envp)
233 472 environ = (char **)envp;
234 473
235 474 (void) execvp(file, argv); /* execute the program */
236 475 _exit(127);
237 476 }
238 477
239 478 /*
240 479 * Initialize the process structure.
241 480 */
↓ open down ↓ |
137 lines elided |
↑ open up ↑ |
242 481 (void) memset(P, 0, sizeof (*P));
243 482 (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
244 483 P->flags |= CREATED;
245 484 P->state = PS_RUN;
246 485 P->pid = pid;
247 486 P->asfd = -1;
248 487 P->ctlfd = -1;
249 488 P->statfd = -1;
250 489 P->agentctlfd = -1;
251 490 P->agentstatfd = -1;
252 - P->ops = &P_live_ops;
491 + Pinit_ops(&P->ops, &P_live_ops);
253 492 Pinitsym(P);
254 493
255 494 /*
256 495 * Open the /proc/pid files.
257 496 */
258 497 (void) snprintf(procname, sizeof (procname), "%s/%d/",
259 498 procfs_path, (int)pid);
260 499 fname = procname + strlen(procname);
261 500 (void) set_minfd();
262 501
263 502 /*
264 503 * Exclusive write open advises others not to interfere.
265 504 * There is no reason for any of these open()s to fail.
266 505 */
267 506 (void) strcpy(fname, "as");
268 507 if ((fd = open(procname, (O_RDWR|O_EXCL))) < 0 ||
269 508 (fd = dupfd(fd, 0)) < 0) {
270 509 dprintf("Pcreate: failed to open %s: %s\n",
271 510 procname, strerror(errno));
272 511 rc = C_STRANGE;
273 512 goto bad;
274 513 }
275 514 P->asfd = fd;
276 515
277 516 (void) strcpy(fname, "status");
278 517 if ((fd = open(procname, O_RDONLY)) < 0 ||
279 518 (fd = dupfd(fd, 0)) < 0) {
280 519 dprintf("Pcreate: failed to open %s: %s\n",
281 520 procname, strerror(errno));
282 521 rc = C_STRANGE;
283 522 goto bad;
284 523 }
285 524 P->statfd = fd;
286 525
287 526 (void) strcpy(fname, "ctl");
288 527 if ((fd = open(procname, O_WRONLY)) < 0 ||
289 528 (fd = dupfd(fd, 0)) < 0) {
290 529 dprintf("Pcreate: failed to open %s: %s\n",
291 530 procname, strerror(errno));
292 531 rc = C_STRANGE;
293 532 goto bad;
294 533 }
295 534 P->ctlfd = fd;
296 535
297 536 (void) Pstop(P, 0); /* stop the controlled process */
298 537
299 538 /*
300 539 * Wait for process to sleep in pause().
301 540 * If the process has already called pause(), then it should be
302 541 * stopped (PR_REQUESTED) while asleep in pause and we are done.
303 542 * Else we set up to catch entry/exit to pause() and set the process
304 543 * running again, expecting it to stop when it reaches pause().
305 544 * There is no reason for this to fail other than an interrupt.
306 545 */
307 546 (void) Psysentry(P, SYS_pause, 1);
308 547 (void) Psysexit(P, SYS_pause, 1);
309 548 for (;;) {
310 549 if (P->state == PS_STOP &&
311 550 P->status.pr_lwp.pr_syscall == SYS_pause &&
312 551 (P->status.pr_lwp.pr_why == PR_REQUESTED ||
313 552 P->status.pr_lwp.pr_why == PR_SYSENTRY ||
314 553 P->status.pr_lwp.pr_why == PR_SYSEXIT))
315 554 break;
316 555
317 556 if (P->state != PS_STOP || /* interrupt or process died */
318 557 Psetrun(P, 0, 0) != 0) { /* can't restart */
319 558 if (errno == EINTR || errno == ERESTART)
320 559 rc = C_INTR;
321 560 else {
322 561 dprintf("Pcreate: Psetrun failed: %s\n",
323 562 strerror(errno));
324 563 rc = C_STRANGE;
325 564 }
326 565 goto bad;
327 566 }
328 567
329 568 (void) Pwait(P, 0);
330 569 }
331 570 (void) Psysentry(P, SYS_pause, 0);
332 571 (void) Psysexit(P, SYS_pause, 0);
333 572
334 573 /*
335 574 * Kick the process off the pause() and catch
336 575 * it again on entry to exec() or exit().
337 576 */
338 577 (void) Psysentry(P, SYS_exit, 1);
339 578 (void) Psysentry(P, SYS_execve, 1);
340 579 if (Psetrun(P, 0, PRSABORT) == -1) {
341 580 dprintf("Pcreate: Psetrun failed: %s\n", strerror(errno));
342 581 rc = C_STRANGE;
343 582 goto bad;
344 583 }
345 584 (void) Pwait(P, 0);
346 585 if (P->state != PS_STOP) {
347 586 dprintf("Pcreate: Pwait failed: %s\n", strerror(errno));
348 587 rc = C_STRANGE;
349 588 goto bad;
350 589 }
351 590
352 591 /*
353 592 * Move the process through instances of failed exec()s
354 593 * to reach the point of stopped on successful exec().
355 594 */
356 595 (void) Psysexit(P, SYS_execve, TRUE);
357 596
358 597 while (P->state == PS_STOP &&
359 598 P->status.pr_lwp.pr_why == PR_SYSENTRY &&
360 599 P->status.pr_lwp.pr_what == SYS_execve) {
361 600 /*
362 601 * Fetch the exec path name now, before we complete
363 602 * the exec(). We may lose the process and be unable
364 603 * to get the information later.
365 604 */
366 605 (void) Pread_string(P, execpath, sizeof (execpath),
367 606 (off_t)P->status.pr_lwp.pr_sysarg[0]);
368 607 if (path != NULL)
369 608 (void) strncpy(path, execpath, len);
370 609 /*
371 610 * Set the process running and wait for
372 611 * it to stop on exit from the exec().
373 612 */
374 613 (void) Psetrun(P, 0, 0);
375 614 (void) Pwait(P, 0);
376 615
377 616 if (P->state == PS_LOST && /* we lost control */
378 617 Preopen(P) != 0) { /* and we can't get it back */
379 618 rc = C_PERM;
380 619 goto bad;
381 620 }
382 621
383 622 /*
384 623 * If the exec() failed, continue the loop, expecting
385 624 * there to be more attempts to exec(), based on PATH.
386 625 */
387 626 if (P->state == PS_STOP &&
388 627 P->status.pr_lwp.pr_why == PR_SYSEXIT &&
389 628 P->status.pr_lwp.pr_what == SYS_execve &&
390 629 (lasterrno = P->status.pr_lwp.pr_errno) != 0) {
391 630 /*
392 631 * The exec() failed. Set the process running and
393 632 * wait for it to stop on entry to the next exec().
394 633 */
395 634 (void) Psetrun(P, 0, 0);
396 635 (void) Pwait(P, 0);
397 636
398 637 continue;
399 638 }
400 639 break;
401 640 }
402 641
403 642 if (P->state == PS_STOP &&
404 643 P->status.pr_lwp.pr_why == PR_SYSEXIT &&
405 644 P->status.pr_lwp.pr_what == SYS_execve &&
406 645 P->status.pr_lwp.pr_errno == 0) {
407 646 /*
408 647 * The process is stopped on successful exec() or execve().
409 648 * Turn off all tracing flags and return success.
410 649 */
411 650 restore_tracing_flags(P);
412 651 #ifndef _LP64
413 652 /* We must be a 64-bit process to deal with a 64-bit process */
414 653 if (P->status.pr_dmodel == PR_MODEL_LP64) {
415 654 rc = C_LP64;
416 655 goto bad;
417 656 }
418 657 #endif
419 658 /*
420 659 * Set run-on-last-close so the controlled process
421 660 * runs even if we die on a signal.
422 661 */
423 662 (void) Psetflags(P, PR_RLC);
424 663 *perr = 0;
425 664 return (P);
426 665 }
427 666
428 667 rc = lasterrno == ENOENT ? C_NOENT : C_NOEXEC;
429 668
430 669 bad:
431 670 (void) kill(pid, SIGKILL);
432 671 if (path != NULL && rc != C_PERM && rc != C_LP64)
433 672 *path = '\0';
434 673 Pfree(P);
435 674 *perr = rc;
436 675 return (NULL);
437 676 }
438 677
439 678 struct ps_prochandle *
440 679 Pcreate(
441 680 const char *file, /* executable file name */
442 681 char *const *argv, /* argument vector */
443 682 int *perr, /* pointer to error return code */
444 683 char *path, /* if non-null, holds exec path name on return */
445 684 size_t len) /* size of the path buffer */
446 685 {
447 686 return (Pxcreate(file, argv, NULL, perr, path, len));
448 687 }
449 688
450 689 /*
451 690 * Return a printable string corresponding to a Pcreate() error return.
452 691 */
453 692 const char *
454 693 Pcreate_error(int error)
455 694 {
456 695 const char *str;
457 696
458 697 switch (error) {
459 698 case C_FORK:
460 699 str = "cannot fork";
461 700 break;
462 701 case C_PERM:
463 702 str = "file is set-id or unreadable";
464 703 break;
465 704 case C_NOEXEC:
466 705 str = "cannot execute file";
467 706 break;
468 707 case C_INTR:
469 708 str = "operation interrupted";
470 709 break;
471 710 case C_LP64:
472 711 str = "program is _LP64, self is not";
473 712 break;
474 713 case C_STRANGE:
475 714 str = "unanticipated system error";
476 715 break;
477 716 case C_NOENT:
478 717 str = "cannot find executable file";
479 718 break;
480 719 default:
481 720 str = "unknown error";
482 721 break;
483 722 }
484 723
485 724 return (str);
486 725 }
487 726
488 727 /*
489 728 * Callback to execute in each child process created with Pcreate() after fork
490 729 * but before it execs the new process image. By default, we do nothing, but
491 730 * by calling this function we allow the client program to define its own
492 731 * version of the function which will interpose on our empty default. This
493 732 * may be useful for clients that need to modify signal dispositions, terminal
494 733 * attributes, or process group and session properties for each new victim.
495 734 */
496 735 /*ARGSUSED*/
497 736 void
498 737 Pcreate_callback(struct ps_prochandle *P)
499 738 {
500 739 /* nothing to do here */
501 740 }
502 741
503 742 /*
504 743 * Grab an existing process.
505 744 * Return an opaque pointer to its process control structure.
506 745 *
507 746 * pid: UNIX process ID.
508 747 * flags:
509 748 * PGRAB_RETAIN Retain tracing flags (default clears all tracing flags).
510 749 * PGRAB_FORCE Grab regardless of whether process is already traced.
511 750 * PGRAB_RDONLY Open the address space file O_RDONLY instead of O_RDWR,
512 751 * and do not open the process control file.
513 752 * PGRAB_NOSTOP Open the process but do not force it to stop.
514 753 * perr: pointer to error return code.
515 754 */
516 755 struct ps_prochandle *
517 756 Pgrab(pid_t pid, int flags, int *perr)
518 757 {
519 758 struct ps_prochandle *P;
520 759 int fd, omode;
521 760 char procname[PATH_MAX];
522 761 char *fname;
523 762 int rc = 0;
524 763
525 764 /*
526 765 * PGRAB_RDONLY means that we do not open the /proc/<pid>/control file,
527 766 * and so it implies RETAIN and NOSTOP since both require control.
528 767 */
529 768 if (flags & PGRAB_RDONLY)
530 769 flags |= PGRAB_RETAIN | PGRAB_NOSTOP;
531 770
532 771 if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
533 772 *perr = G_STRANGE;
534 773 return (NULL);
535 774 }
536 775
537 776 P->asfd = -1;
538 777 P->ctlfd = -1;
539 778 P->statfd = -1;
540 779
541 780 again: /* Come back here if we lose it in the Window of Vulnerability */
542 781 if (P->ctlfd >= 0)
543 782 (void) close(P->ctlfd);
544 783 if (P->asfd >= 0)
↓ open down ↓ |
282 lines elided |
↑ open up ↑ |
545 784 (void) close(P->asfd);
546 785 if (P->statfd >= 0)
547 786 (void) close(P->statfd);
548 787 (void) memset(P, 0, sizeof (*P));
549 788 (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
550 789 P->ctlfd = -1;
551 790 P->asfd = -1;
552 791 P->statfd = -1;
553 792 P->agentctlfd = -1;
554 793 P->agentstatfd = -1;
555 - P->ops = &P_live_ops;
794 + Pinit_ops(&P->ops, &P_live_ops);
556 795 Pinitsym(P);
557 796
558 797 /*
559 798 * Open the /proc/pid files
560 799 */
561 800 (void) snprintf(procname, sizeof (procname), "%s/%d/",
562 801 procfs_path, (int)pid);
563 802 fname = procname + strlen(procname);
564 803 (void) set_minfd();
565 804
566 805 /*
567 806 * Request exclusive open to avoid grabbing someone else's
568 807 * process and to prevent others from interfering afterwards.
569 808 * If this fails and the 'PGRAB_FORCE' flag is set, attempt to
570 809 * open non-exclusively.
571 810 */
572 811 (void) strcpy(fname, "as");
573 812 omode = (flags & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
574 813
575 814 if (((fd = open(procname, omode | O_EXCL)) < 0 &&
576 815 (fd = ((flags & PGRAB_FORCE)? open(procname, omode) : -1)) < 0) ||
577 816 (fd = dupfd(fd, 0)) < 0) {
578 817 switch (errno) {
579 818 case ENOENT:
580 819 rc = G_NOPROC;
581 820 break;
582 821 case EACCES:
583 822 case EPERM:
584 823 rc = G_PERM;
585 824 break;
586 825 case EMFILE:
587 826 rc = G_NOFD;
588 827 break;
589 828 case EBUSY:
590 829 if (!(flags & PGRAB_FORCE) || geteuid() != 0) {
591 830 rc = G_BUSY;
592 831 break;
593 832 }
594 833 /* FALLTHROUGH */
595 834 default:
596 835 dprintf("Pgrab: failed to open %s: %s\n",
597 836 procname, strerror(errno));
598 837 rc = G_STRANGE;
599 838 break;
600 839 }
601 840 goto err;
602 841 }
603 842 P->asfd = fd;
604 843
605 844 (void) strcpy(fname, "status");
606 845 if ((fd = open(procname, O_RDONLY)) < 0 ||
607 846 (fd = dupfd(fd, 0)) < 0) {
608 847 switch (errno) {
609 848 case ENOENT:
610 849 rc = G_NOPROC;
611 850 break;
612 851 case EMFILE:
613 852 rc = G_NOFD;
614 853 break;
615 854 default:
616 855 dprintf("Pgrab: failed to open %s: %s\n",
617 856 procname, strerror(errno));
618 857 rc = G_STRANGE;
619 858 break;
620 859 }
621 860 goto err;
622 861 }
623 862 P->statfd = fd;
624 863
625 864 if (!(flags & PGRAB_RDONLY)) {
626 865 (void) strcpy(fname, "ctl");
627 866 if ((fd = open(procname, O_WRONLY)) < 0 ||
628 867 (fd = dupfd(fd, 0)) < 0) {
629 868 switch (errno) {
630 869 case ENOENT:
631 870 rc = G_NOPROC;
632 871 break;
633 872 case EMFILE:
634 873 rc = G_NOFD;
635 874 break;
636 875 default:
637 876 dprintf("Pgrab: failed to open %s: %s\n",
638 877 procname, strerror(errno));
639 878 rc = G_STRANGE;
640 879 break;
641 880 }
642 881 goto err;
643 882 }
644 883 P->ctlfd = fd;
645 884 }
646 885
647 886 P->state = PS_RUN;
648 887 P->pid = pid;
649 888
650 889 /*
651 890 * We are now in the Window of Vulnerability (WoV). The process may
652 891 * exec() a setuid/setgid or unreadable object file between the open()
653 892 * and the PCSTOP. We will get EAGAIN in this case and must start over.
654 893 * As Pstopstatus will trigger the first read() from a /proc file,
655 894 * we also need to handle EOVERFLOW here when 32-bit as an indicator
656 895 * that this process is 64-bit. Finally, if the process has become
657 896 * a zombie (PS_UNDEAD) while we were trying to grab it, just remain
658 897 * silent about this and pretend there was no process.
659 898 */
660 899 if (Pstopstatus(P, PCNULL, 0) != 0) {
661 900 #ifndef _LP64
662 901 if (errno == EOVERFLOW) {
663 902 rc = G_LP64;
664 903 goto err;
665 904 }
666 905 #endif
667 906 if (P->state == PS_LOST) { /* WoV */
668 907 (void) mutex_destroy(&P->proc_lock);
669 908 goto again;
670 909 }
671 910
672 911 if (P->state == PS_UNDEAD)
673 912 rc = G_NOPROC;
674 913 else
675 914 rc = G_STRANGE;
676 915
677 916 goto err;
678 917 }
679 918
680 919 /*
681 920 * If the process is a system process, we can't control it even as root
682 921 */
683 922 if (P->status.pr_flags & PR_ISSYS) {
684 923 rc = G_SYS;
685 924 goto err;
686 925 }
687 926 #ifndef _LP64
688 927 /*
689 928 * We must be a 64-bit process to deal with a 64-bit process
690 929 */
691 930 if (P->status.pr_dmodel == PR_MODEL_LP64) {
692 931 rc = G_LP64;
693 932 goto err;
694 933 }
695 934 #endif
696 935
697 936 /*
698 937 * Remember the status for use by Prelease().
699 938 */
700 939 P->orig_status = P->status; /* structure copy */
701 940
702 941 /*
703 942 * Before stopping the process, make sure we are not grabbing ourselves.
704 943 * If we are, make sure we are doing it PGRAB_RDONLY.
705 944 */
706 945 if (pid == getpid()) {
707 946 /*
708 947 * Verify that the process is really ourself:
709 948 * Set a magic number, read it through the
710 949 * /proc file and see if the results match.
711 950 */
712 951 uint32_t magic1 = 0;
713 952 uint32_t magic2 = 2;
714 953
715 954 errno = 0;
716 955
717 956 if (Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
718 957 == sizeof (magic2) &&
719 958 magic2 == 0 &&
720 959 (magic1 = 0xfeedbeef) &&
721 960 Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
722 961 == sizeof (magic2) &&
723 962 magic2 == 0xfeedbeef &&
724 963 !(flags & PGRAB_RDONLY)) {
725 964 rc = G_SELF;
726 965 goto err;
727 966 }
728 967 }
729 968
730 969 /*
731 970 * If the process is already stopped or has been directed
732 971 * to stop via /proc, do not set run-on-last-close.
733 972 */
734 973 if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
735 974 !(flags & PGRAB_RDONLY)) {
736 975 /*
737 976 * Mark the process run-on-last-close so
738 977 * it runs even if we die from SIGKILL.
739 978 */
740 979 if (Psetflags(P, PR_RLC) != 0) {
741 980 if (errno == EAGAIN) { /* WoV */
742 981 (void) mutex_destroy(&P->proc_lock);
743 982 goto again;
744 983 }
745 984 if (errno == ENOENT) /* No complaint about zombies */
746 985 rc = G_ZOMB;
747 986 else {
748 987 dprintf("Pgrab: failed to set RLC\n");
749 988 rc = G_STRANGE;
750 989 }
751 990 goto err;
752 991 }
753 992 }
754 993
755 994 /*
756 995 * If a stop directive is pending and the process has not yet stopped,
757 996 * then synchronously wait for the stop directive to take effect.
758 997 * Limit the time spent waiting for the process to stop by iterating
759 998 * at most 10 times. The time-out of 20 ms corresponds to the time
760 999 * between sending the stop directive and the process actually stopped
761 1000 * as measured by DTrace on a slow, busy system. If the process doesn't
762 1001 * stop voluntarily, clear the PR_DSTOP flag so that the code below
763 1002 * forces the process to stop.
764 1003 */
765 1004 if (!(flags & PGRAB_RDONLY)) {
766 1005 int niter = 0;
767 1006 while ((P->status.pr_lwp.pr_flags & (PR_STOPPED|PR_DSTOP)) ==
768 1007 PR_DSTOP && niter < 10 &&
769 1008 Pstopstatus(P, PCTWSTOP, 20) != 0) {
770 1009 niter++;
771 1010 if (flags & PGRAB_NOSTOP)
772 1011 break;
773 1012 }
774 1013 if (niter == 10 && !(flags & PGRAB_NOSTOP)) {
775 1014 /* Try it harder down below */
776 1015 P->status.pr_lwp.pr_flags &= ~PR_DSTOP;
777 1016 }
778 1017 }
779 1018
780 1019 /*
781 1020 * If the process is not already stopped or directed to stop
782 1021 * and PGRAB_NOSTOP was not specified, stop the process now.
783 1022 */
784 1023 if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
785 1024 !(flags & PGRAB_NOSTOP)) {
786 1025 /*
787 1026 * Stop the process, get its status and signal/syscall masks.
788 1027 */
789 1028 if (((P->status.pr_lwp.pr_flags & PR_STOPPED) &&
790 1029 Pstopstatus(P, PCDSTOP, 0) != 0) ||
791 1030 Pstopstatus(P, PCSTOP, 2000) != 0) {
792 1031 #ifndef _LP64
793 1032 if (errno == EOVERFLOW) {
794 1033 rc = G_LP64;
795 1034 goto err;
796 1035 }
797 1036 #endif
798 1037 if (P->state == PS_LOST) { /* WoV */
799 1038 (void) mutex_destroy(&P->proc_lock);
800 1039 goto again;
801 1040 }
802 1041 if ((errno != EINTR && errno != ERESTART) ||
803 1042 (P->state != PS_STOP &&
804 1043 !(P->status.pr_flags & PR_DSTOP))) {
805 1044 if (P->state != PS_RUN && errno != ENOENT) {
806 1045 dprintf("Pgrab: failed to PCSTOP\n");
807 1046 rc = G_STRANGE;
808 1047 } else {
809 1048 rc = G_ZOMB;
810 1049 }
811 1050 goto err;
812 1051 }
813 1052 }
814 1053
815 1054 /*
816 1055 * Process should now either be stopped via /proc or there
817 1056 * should be an outstanding stop directive.
818 1057 */
819 1058 if (!(P->status.pr_flags & (PR_ISTOP|PR_DSTOP))) {
820 1059 dprintf("Pgrab: process is not stopped\n");
821 1060 rc = G_STRANGE;
822 1061 goto err;
823 1062 }
824 1063 #ifndef _LP64
825 1064 /*
826 1065 * Test this again now because the 32-bit victim process may
827 1066 * have exec'd a 64-bit process in the meantime.
828 1067 */
829 1068 if (P->status.pr_dmodel == PR_MODEL_LP64) {
830 1069 rc = G_LP64;
831 1070 goto err;
832 1071 }
833 1072 #endif
834 1073 }
835 1074
836 1075 /*
837 1076 * Cancel all tracing flags unless the PGRAB_RETAIN flag is set.
838 1077 */
839 1078 if (!(flags & PGRAB_RETAIN)) {
840 1079 (void) Psysentry(P, 0, FALSE);
841 1080 (void) Psysexit(P, 0, FALSE);
842 1081 (void) Psignal(P, 0, FALSE);
843 1082 (void) Pfault(P, 0, FALSE);
844 1083 Psync(P);
845 1084 }
846 1085
847 1086 *perr = 0;
848 1087 return (P);
849 1088
850 1089 err:
851 1090 Pfree(P);
852 1091 *perr = rc;
853 1092 return (NULL);
854 1093 }
855 1094
856 1095 /*
857 1096 * Return a printable string corresponding to a Pgrab() error return.
858 1097 */
859 1098 const char *
860 1099 Pgrab_error(int error)
861 1100 {
862 1101 const char *str;
863 1102
864 1103 switch (error) {
865 1104 case G_NOPROC:
866 1105 str = "no such process";
867 1106 break;
868 1107 case G_NOCORE:
869 1108 str = "no such core file";
870 1109 break;
871 1110 case G_NOPROCORCORE:
872 1111 str = "no such process or core file";
873 1112 break;
874 1113 case G_NOEXEC:
875 1114 str = "cannot find executable file";
876 1115 break;
877 1116 case G_ZOMB:
878 1117 str = "zombie process";
879 1118 break;
880 1119 case G_PERM:
881 1120 str = "permission denied";
882 1121 break;
883 1122 case G_BUSY:
884 1123 str = "process is traced";
885 1124 break;
886 1125 case G_SYS:
887 1126 str = "system process";
888 1127 break;
889 1128 case G_SELF:
890 1129 str = "attempt to grab self";
891 1130 break;
892 1131 case G_INTR:
893 1132 str = "operation interrupted";
894 1133 break;
895 1134 case G_LP64:
896 1135 str = "program is _LP64, self is not";
897 1136 break;
898 1137 case G_FORMAT:
899 1138 str = "file is not an ELF core file";
900 1139 break;
901 1140 case G_ELF:
902 1141 str = "libelf error";
903 1142 break;
904 1143 case G_NOTE:
905 1144 str = "core file is corrupt or missing required data";
906 1145 break;
907 1146 case G_STRANGE:
908 1147 str = "unanticipated system error";
909 1148 break;
910 1149 case G_ISAINVAL:
911 1150 str = "wrong ELF machine type";
912 1151 break;
913 1152 case G_BADLWPS:
914 1153 str = "bad lwp specification";
915 1154 break;
916 1155 case G_NOFD:
917 1156 str = "too many open files";
918 1157 break;
919 1158 default:
920 1159 str = "unknown error";
921 1160 break;
922 1161 }
923 1162
924 1163 return (str);
925 1164 }
↓ open down ↓ |
360 lines elided |
↑ open up ↑ |
926 1165
927 1166 /*
928 1167 * Free a process control structure.
929 1168 * Close the file descriptors but don't do the Prelease logic.
930 1169 */
931 1170 void
932 1171 Pfree(struct ps_prochandle *P)
933 1172 {
934 1173 uint_t i;
935 1174
936 - if (P->core != NULL) {
937 - extern void __priv_free_info(void *);
938 - lwp_info_t *nlwp, *lwp = list_next(&P->core->core_lwp_head);
939 -
940 - for (i = 0; i < P->core->core_nlwp; i++, lwp = nlwp) {
941 - nlwp = list_next(lwp);
942 -#ifdef __sparc
943 - if (lwp->lwp_gwins != NULL)
944 - free(lwp->lwp_gwins);
945 - if (lwp->lwp_xregs != NULL)
946 - free(lwp->lwp_xregs);
947 - if (lwp->lwp_asrs != NULL)
948 - free(lwp->lwp_asrs);
949 -#endif
950 - free(lwp);
951 - }
952 -
953 - if (P->core->core_platform != NULL)
954 - free(P->core->core_platform);
955 - if (P->core->core_uts != NULL)
956 - free(P->core->core_uts);
957 - if (P->core->core_cred != NULL)
958 - free(P->core->core_cred);
959 - if (P->core->core_priv != NULL)
960 - free(P->core->core_priv);
961 - if (P->core->core_privinfo != NULL)
962 - __priv_free_info(P->core->core_privinfo);
963 - if (P->core->core_ppii != NULL)
964 - free(P->core->core_ppii);
965 - if (P->core->core_zonename != NULL)
966 - free(P->core->core_zonename);
967 -#if defined(__i386) || defined(__amd64)
968 - if (P->core->core_ldt != NULL)
969 - free(P->core->core_ldt);
970 -#endif
971 -
972 - free(P->core);
973 - }
974 -
975 1175 if (P->ucaddrs != NULL) {
976 1176 free(P->ucaddrs);
977 1177 P->ucaddrs = NULL;
978 1178 P->ucnelems = 0;
979 1179 }
980 1180
981 1181 (void) mutex_lock(&P->proc_lock);
982 1182 if (P->hashtab != NULL) {
983 1183 struct ps_lwphandle *L;
984 1184 for (i = 0; i < HASHSIZE; i++) {
985 1185 while ((L = P->hashtab[i]) != NULL)
986 1186 Lfree_internal(P, L);
987 1187 }
988 1188 free(P->hashtab);
989 1189 }
990 1190
991 1191 while (P->num_fd > 0) {
992 1192 fd_info_t *fip = list_next(&P->fd_head);
993 1193 list_unlink(fip);
994 1194 free(fip);
995 1195 P->num_fd--;
996 1196 }
997 1197 (void) mutex_unlock(&P->proc_lock);
998 1198 (void) mutex_destroy(&P->proc_lock);
999 1199
1000 1200 if (P->agentctlfd >= 0)
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
1001 1201 (void) close(P->agentctlfd);
1002 1202 if (P->agentstatfd >= 0)
1003 1203 (void) close(P->agentstatfd);
1004 1204 if (P->ctlfd >= 0)
1005 1205 (void) close(P->ctlfd);
1006 1206 if (P->asfd >= 0)
1007 1207 (void) close(P->asfd);
1008 1208 if (P->statfd >= 0)
1009 1209 (void) close(P->statfd);
1010 1210 Preset_maps(P);
1211 + P->ops.pop_fini(P, P->data);
1011 1212
1012 1213 /* clear out the structure as a precaution against reuse */
1013 1214 (void) memset(P, 0, sizeof (*P));
1014 1215 P->ctlfd = -1;
1015 1216 P->asfd = -1;
1016 1217 P->statfd = -1;
1017 1218 P->agentctlfd = -1;
1018 1219 P->agentstatfd = -1;
1019 1220
1020 1221 free(P);
1021 1222 }
1022 1223
1023 1224 /*
1024 1225 * Return the state of the process, one of the PS_* values.
1025 1226 */
1026 1227 int
1027 1228 Pstate(struct ps_prochandle *P)
1028 1229 {
1029 1230 return (P->state);
1030 1231 }
1031 1232
1032 1233 /*
1033 1234 * Return the open address space file descriptor for the process.
1034 1235 * Clients must not close this file descriptor, not use it
1035 1236 * after the process is freed.
1036 1237 */
1037 1238 int
1038 1239 Pasfd(struct ps_prochandle *P)
1039 1240 {
1040 1241 return (P->asfd);
1041 1242 }
1042 1243
1043 1244 /*
1044 1245 * Return the open control file descriptor for the process.
1045 1246 * Clients must not close this file descriptor, not use it
1046 1247 * after the process is freed.
1047 1248 */
1048 1249 int
1049 1250 Pctlfd(struct ps_prochandle *P)
1050 1251 {
1051 1252 return (P->ctlfd);
↓ open down ↓ |
31 lines elided |
↑ open up ↑ |
1052 1253 }
1053 1254
1054 1255 /*
1055 1256 * Return a pointer to the process psinfo structure.
1056 1257 * Clients should not hold on to this pointer indefinitely.
1057 1258 * It will become invalid on Prelease().
1058 1259 */
1059 1260 const psinfo_t *
1060 1261 Ppsinfo(struct ps_prochandle *P)
1061 1262 {
1062 - if (P->state == PS_IDLE) {
1063 - errno = ENODATA;
1064 - return (NULL);
1065 - }
1066 -
1067 - if (P->state != PS_DEAD && proc_get_psinfo(P->pid, &P->psinfo) == -1)
1068 - return (NULL);
1069 -
1070 - return (&P->psinfo);
1263 + return (P->ops.pop_psinfo(P, &P->psinfo, P->data));
1071 1264 }
1072 1265
1073 1266 /*
1074 1267 * Return a pointer to the process status structure.
1075 1268 * Clients should not hold on to this pointer indefinitely.
1076 1269 * It will become invalid on Prelease().
1077 1270 */
1078 1271 const pstatus_t *
1079 1272 Pstatus(struct ps_prochandle *P)
1080 1273 {
1081 1274 return (&P->status);
1082 1275 }
1083 1276
1277 +static void
1278 +Pread_status(struct ps_prochandle *P)
1279 +{
1280 + P->ops.pop_status(P, &P->status, P->data);
1281 +}
1282 +
1084 1283 /*
1085 1284 * Fill in a pointer to a process credentials structure. The ngroups parameter
1086 1285 * is the number of supplementary group entries allocated in the caller's cred
1087 1286 * structure. It should equal zero or one unless extra space has been
1088 1287 * allocated for the group list by the caller.
1089 1288 */
1090 1289 int
1091 1290 Pcred(struct ps_prochandle *P, prcred_t *pcrp, int ngroups)
1092 1291 {
1093 - if (P->state == PS_IDLE) {
1094 - errno = ENODATA;
1095 - return (-1);
1096 - }
1292 + return (P->ops.pop_cred(P, pcrp, ngroups, P->data));
1293 +}
1097 1294
1098 - if (P->state != PS_DEAD)
1099 - return (proc_get_cred(P->pid, pcrp, ngroups));
1295 +static prheader_t *
1296 +Plstatus(struct ps_prochandle *P)
1297 +{
1298 + return (P->ops.pop_lstatus(P, P->data));
1299 +}
1100 1300
1101 - if (P->core->core_cred != NULL) {
1102 - /*
1103 - * Avoid returning more supplementary group data than the
1104 - * caller has allocated in their buffer. We expect them to
1105 - * check pr_ngroups afterward and potentially call us again.
1106 - */
1107 - ngroups = MIN(ngroups, P->core->core_cred->pr_ngroups);
1108 -
1109 - (void) memcpy(pcrp, P->core->core_cred,
1110 - sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
1111 -
1112 - return (0);
1113 - }
1114 -
1115 - errno = ENODATA;
1116 - return (-1);
1301 +static prheader_t *
1302 +Plpsinfo(struct ps_prochandle *P)
1303 +{
1304 + return (P->ops.pop_lpsinfo(P, P->data));
1117 1305 }
1118 1306
1307 +
1119 1308 #if defined(__i386) || defined(__amd64)
1120 1309 /*
1121 1310 * Fill in a pointer to a process LDT structure.
1122 1311 * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
1123 1312 * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
1124 1313 * Otherwise we return the actual number of LDT entries fetched (<= nldt).
1125 1314 */
1126 1315 int
1127 1316 Pldt(struct ps_prochandle *P, struct ssd *pldt, int nldt)
1128 1317 {
1129 - if (P->state == PS_IDLE) {
1130 - errno = ENODATA;
1131 - return (-1);
1132 - }
1318 + return (P->ops.pop_ldt(P, pldt, nldt, P->data));
1133 1319
1134 - if (P->state != PS_DEAD)
1135 - return (proc_get_ldt(P->pid, pldt, nldt));
1136 -
1137 - if (pldt == NULL || nldt == 0)
1138 - return (P->core->core_nldt);
1139 -
1140 - if (P->core->core_ldt != NULL) {
1141 - nldt = MIN(nldt, P->core->core_nldt);
1142 -
1143 - (void) memcpy(pldt, P->core->core_ldt,
1144 - nldt * sizeof (struct ssd));
1145 -
1146 - return (nldt);
1147 - }
1148 -
1149 - errno = ENODATA;
1150 - return (-1);
1151 1320 }
1152 1321 #endif /* __i386 */
1153 1322
1154 1323 /*
1155 - * Fill in a pointer to a process privilege structure.
1324 + * Return a malloced process privilege structure in *pprv.
1156 1325 */
1157 -ssize_t
1158 -Ppriv(struct ps_prochandle *P, prpriv_t *pprv, size_t size)
1326 +int
1327 +Ppriv(struct ps_prochandle *P, prpriv_t **pprv)
1159 1328 {
1160 - if (P->state != PS_DEAD) {
1161 - prpriv_t *pp = proc_get_priv(P->pid);
1162 - if (pp != NULL) {
1163 - size = MIN(size, PRIV_PRPRIV_SIZE(pp));
1164 - (void) memcpy(pprv, pp, size);
1165 - free(pp);
1166 - return (size);
1167 - }
1168 - return (-1);
1169 - }
1170 -
1171 - if (P->core->core_priv != NULL) {
1172 - size = MIN(P->core->core_priv_size, size);
1173 - (void) memcpy(pprv, P->core->core_priv, size);
1174 - return (size);
1175 - }
1176 - errno = ENODATA;
1177 - return (-1);
1329 + return (P->ops.pop_priv(P, pprv, P->data));
1178 1330 }
1179 1331
1180 1332 int
1181 1333 Psetpriv(struct ps_prochandle *P, prpriv_t *pprv)
1182 1334 {
1183 1335 int rc;
1184 1336 long *ctl;
1185 1337 size_t sz;
1186 1338
1187 1339 if (P->state == PS_DEAD) {
1188 1340 errno = EBADF;
1189 1341 return (-1);
1190 1342 }
1191 1343
1192 1344 sz = PRIV_PRPRIV_SIZE(pprv) + sizeof (long);
1193 1345
1194 1346 sz = ((sz - 1) / sizeof (long) + 1) * sizeof (long);
1195 1347
1196 1348 ctl = malloc(sz);
1197 1349 if (ctl == NULL)
1198 1350 return (-1);
1199 1351
1200 1352 ctl[0] = PCSPRIV;
1201 1353
1202 1354 (void) memcpy(&ctl[1], pprv, PRIV_PRPRIV_SIZE(pprv));
1203 1355
1204 1356 if (write(P->ctlfd, ctl, sz) != sz)
1205 1357 rc = -1;
1206 1358 else
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
1207 1359 rc = 0;
1208 1360
1209 1361 free(ctl);
1210 1362
1211 1363 return (rc);
1212 1364 }
1213 1365
1214 1366 void *
1215 1367 Pprivinfo(struct ps_prochandle *P)
1216 1368 {
1369 + core_info_t *core = P->data;
1370 +
1217 1371 /* Use default from libc */
1218 1372 if (P->state != PS_DEAD)
1219 1373 return (NULL);
1220 1374
1221 - return (P->core->core_privinfo);
1375 + return (core->core_privinfo);
1222 1376 }
1223 1377
1224 1378 /*
1225 1379 * Ensure that all cached state is written to the process.
1226 1380 * The cached state is the LWP's signal mask and registers
1227 1381 * and the process's tracing flags.
1228 1382 */
1229 1383 void
1230 1384 Psync(struct ps_prochandle *P)
1231 1385 {
1232 1386 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
1233 1387 long cmd[6];
1234 1388 iovec_t iov[12];
1235 1389 int n = 0;
1236 1390
1237 1391 if (P->flags & SETHOLD) {
1238 1392 cmd[0] = PCSHOLD;
1239 1393 iov[n].iov_base = (caddr_t)&cmd[0];
1240 1394 iov[n++].iov_len = sizeof (long);
1241 1395 iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_lwphold;
1242 1396 iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_lwphold);
1243 1397 }
1244 1398 if (P->flags & SETREGS) {
1245 1399 cmd[1] = PCSREG;
1246 1400 #ifdef __i386
1247 1401 /* XX64 we should probably restore REG_GS after this */
1248 1402 if (ctlfd == P->agentctlfd)
1249 1403 P->status.pr_lwp.pr_reg[GS] = 0;
1250 1404 #elif defined(__amd64)
1251 1405 /* XX64 */
1252 1406 #endif
1253 1407 iov[n].iov_base = (caddr_t)&cmd[1];
1254 1408 iov[n++].iov_len = sizeof (long);
1255 1409 iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_reg[0];
1256 1410 iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_reg);
1257 1411 }
1258 1412 if (P->flags & SETSIG) {
1259 1413 cmd[2] = PCSTRACE;
1260 1414 iov[n].iov_base = (caddr_t)&cmd[2];
1261 1415 iov[n++].iov_len = sizeof (long);
1262 1416 iov[n].iov_base = (caddr_t)&P->status.pr_sigtrace;
1263 1417 iov[n++].iov_len = sizeof (P->status.pr_sigtrace);
1264 1418 }
1265 1419 if (P->flags & SETFAULT) {
1266 1420 cmd[3] = PCSFAULT;
1267 1421 iov[n].iov_base = (caddr_t)&cmd[3];
1268 1422 iov[n++].iov_len = sizeof (long);
1269 1423 iov[n].iov_base = (caddr_t)&P->status.pr_flttrace;
1270 1424 iov[n++].iov_len = sizeof (P->status.pr_flttrace);
1271 1425 }
1272 1426 if (P->flags & SETENTRY) {
1273 1427 cmd[4] = PCSENTRY;
1274 1428 iov[n].iov_base = (caddr_t)&cmd[4];
1275 1429 iov[n++].iov_len = sizeof (long);
1276 1430 iov[n].iov_base = (caddr_t)&P->status.pr_sysentry;
1277 1431 iov[n++].iov_len = sizeof (P->status.pr_sysentry);
1278 1432 }
1279 1433 if (P->flags & SETEXIT) {
1280 1434 cmd[5] = PCSEXIT;
1281 1435 iov[n].iov_base = (caddr_t)&cmd[5];
1282 1436 iov[n++].iov_len = sizeof (long);
1283 1437 iov[n].iov_base = (caddr_t)&P->status.pr_sysexit;
1284 1438 iov[n++].iov_len = sizeof (P->status.pr_sysexit);
1285 1439 }
1286 1440
1287 1441 if (n == 0 || writev(ctlfd, iov, n) < 0)
1288 1442 return; /* nothing to do or write failed */
1289 1443
1290 1444 P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT|SETHOLD|SETREGS);
1291 1445 }
1292 1446
1293 1447 /*
1294 1448 * Reopen the /proc file (after PS_LOST).
1295 1449 */
1296 1450 int
1297 1451 Preopen(struct ps_prochandle *P)
1298 1452 {
1299 1453 int fd;
1300 1454 char procname[PATH_MAX];
1301 1455 char *fname;
1302 1456
1303 1457 if (P->state == PS_DEAD || P->state == PS_IDLE)
1304 1458 return (0);
1305 1459
1306 1460 if (P->agentcnt > 0) {
1307 1461 P->agentcnt = 1;
1308 1462 Pdestroy_agent(P);
1309 1463 }
1310 1464
1311 1465 (void) snprintf(procname, sizeof (procname), "%s/%d/",
1312 1466 procfs_path, (int)P->pid);
1313 1467 fname = procname + strlen(procname);
1314 1468
1315 1469 (void) strcpy(fname, "as");
1316 1470 if ((fd = open(procname, O_RDWR)) < 0 ||
1317 1471 close(P->asfd) < 0 ||
1318 1472 (fd = dupfd(fd, P->asfd)) != P->asfd) {
1319 1473 dprintf("Preopen: failed to open %s: %s\n",
1320 1474 procname, strerror(errno));
1321 1475 if (fd >= 0)
1322 1476 (void) close(fd);
1323 1477 return (-1);
1324 1478 }
1325 1479 P->asfd = fd;
1326 1480
1327 1481 (void) strcpy(fname, "status");
1328 1482 if ((fd = open(procname, O_RDONLY)) < 0 ||
1329 1483 close(P->statfd) < 0 ||
1330 1484 (fd = dupfd(fd, P->statfd)) != P->statfd) {
1331 1485 dprintf("Preopen: failed to open %s: %s\n",
1332 1486 procname, strerror(errno));
1333 1487 if (fd >= 0)
1334 1488 (void) close(fd);
1335 1489 return (-1);
1336 1490 }
1337 1491 P->statfd = fd;
1338 1492
1339 1493 (void) strcpy(fname, "ctl");
1340 1494 if ((fd = open(procname, O_WRONLY)) < 0 ||
1341 1495 close(P->ctlfd) < 0 ||
1342 1496 (fd = dupfd(fd, P->ctlfd)) != P->ctlfd) {
1343 1497 dprintf("Preopen: failed to open %s: %s\n",
1344 1498 procname, strerror(errno));
1345 1499 if (fd >= 0)
1346 1500 (void) close(fd);
1347 1501 return (-1);
1348 1502 }
1349 1503 P->ctlfd = fd;
1350 1504
1351 1505 /*
1352 1506 * Set the state to PS_RUN and wait for the process to stop so that
1353 1507 * we re-read the status from the new P->statfd. If this fails, Pwait
1354 1508 * will reset the state to PS_LOST and we fail the reopen. Before
1355 1509 * returning, we also forge a bit of P->status to allow the debugger to
1356 1510 * see that we are PS_LOST following a successful exec.
1357 1511 */
1358 1512 P->state = PS_RUN;
1359 1513 if (Pwait(P, 0) == -1) {
1360 1514 #ifdef _ILP32
1361 1515 if (errno == EOVERFLOW)
1362 1516 P->status.pr_dmodel = PR_MODEL_LP64;
1363 1517 #endif
1364 1518 P->status.pr_lwp.pr_why = PR_SYSEXIT;
1365 1519 P->status.pr_lwp.pr_what = SYS_execve;
1366 1520 P->status.pr_lwp.pr_errno = 0;
1367 1521 return (-1);
1368 1522 }
1369 1523
1370 1524 /*
1371 1525 * The process should be stopped on exec (REQUESTED)
1372 1526 * or else should be stopped on exit from exec() (SYSEXIT)
1373 1527 */
1374 1528 if (P->state == PS_STOP &&
1375 1529 (P->status.pr_lwp.pr_why == PR_REQUESTED ||
1376 1530 (P->status.pr_lwp.pr_why == PR_SYSEXIT &&
1377 1531 P->status.pr_lwp.pr_what == SYS_execve))) {
1378 1532 /* fake up stop-on-exit-from-execve */
1379 1533 if (P->status.pr_lwp.pr_why == PR_REQUESTED) {
1380 1534 P->status.pr_lwp.pr_why = PR_SYSEXIT;
1381 1535 P->status.pr_lwp.pr_what = SYS_execve;
1382 1536 P->status.pr_lwp.pr_errno = 0;
1383 1537 }
1384 1538 } else {
1385 1539 dprintf("Preopen: expected REQUESTED or "
1386 1540 "SYSEXIT(SYS_execve) stop\n");
1387 1541 }
1388 1542
1389 1543 return (0);
1390 1544 }
1391 1545
1392 1546 /*
1393 1547 * Define all settable flags other than the microstate accounting flags.
1394 1548 */
1395 1549 #define ALL_SETTABLE_FLAGS (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_PTRACE)
1396 1550
1397 1551 /*
1398 1552 * Restore /proc tracing flags to their original values
1399 1553 * in preparation for releasing the process.
1400 1554 * Also called by Pcreate() to clear all tracing flags.
1401 1555 */
1402 1556 static void
1403 1557 restore_tracing_flags(struct ps_prochandle *P)
1404 1558 {
1405 1559 long flags;
1406 1560 long cmd[4];
1407 1561 iovec_t iov[8];
1408 1562
1409 1563 if (P->flags & CREATED) {
1410 1564 /* we created this process; clear all tracing flags */
1411 1565 premptyset(&P->status.pr_sigtrace);
1412 1566 premptyset(&P->status.pr_flttrace);
1413 1567 premptyset(&P->status.pr_sysentry);
1414 1568 premptyset(&P->status.pr_sysexit);
1415 1569 if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) != 0)
1416 1570 (void) Punsetflags(P, ALL_SETTABLE_FLAGS);
1417 1571 } else {
1418 1572 /* we grabbed the process; restore its tracing flags */
1419 1573 P->status.pr_sigtrace = P->orig_status.pr_sigtrace;
1420 1574 P->status.pr_flttrace = P->orig_status.pr_flttrace;
1421 1575 P->status.pr_sysentry = P->orig_status.pr_sysentry;
1422 1576 P->status.pr_sysexit = P->orig_status.pr_sysexit;
1423 1577 if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) !=
1424 1578 (flags = (P->orig_status.pr_flags & ALL_SETTABLE_FLAGS))) {
1425 1579 (void) Punsetflags(P, ALL_SETTABLE_FLAGS);
1426 1580 if (flags)
1427 1581 (void) Psetflags(P, flags);
1428 1582 }
1429 1583 }
1430 1584
1431 1585 cmd[0] = PCSTRACE;
1432 1586 iov[0].iov_base = (caddr_t)&cmd[0];
1433 1587 iov[0].iov_len = sizeof (long);
1434 1588 iov[1].iov_base = (caddr_t)&P->status.pr_sigtrace;
1435 1589 iov[1].iov_len = sizeof (P->status.pr_sigtrace);
1436 1590
1437 1591 cmd[1] = PCSFAULT;
1438 1592 iov[2].iov_base = (caddr_t)&cmd[1];
1439 1593 iov[2].iov_len = sizeof (long);
1440 1594 iov[3].iov_base = (caddr_t)&P->status.pr_flttrace;
1441 1595 iov[3].iov_len = sizeof (P->status.pr_flttrace);
1442 1596
1443 1597 cmd[2] = PCSENTRY;
1444 1598 iov[4].iov_base = (caddr_t)&cmd[2];
1445 1599 iov[4].iov_len = sizeof (long);
1446 1600 iov[5].iov_base = (caddr_t)&P->status.pr_sysentry;
1447 1601 iov[5].iov_len = sizeof (P->status.pr_sysentry);
1448 1602
1449 1603 cmd[3] = PCSEXIT;
1450 1604 iov[6].iov_base = (caddr_t)&cmd[3];
1451 1605 iov[6].iov_len = sizeof (long);
1452 1606 iov[7].iov_base = (caddr_t)&P->status.pr_sysexit;
1453 1607 iov[7].iov_len = sizeof (P->status.pr_sysexit);
1454 1608
1455 1609 (void) writev(P->ctlfd, iov, 8);
1456 1610
1457 1611 P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT);
1458 1612 }
1459 1613
1460 1614 /*
1461 1615 * Release the process. Frees the process control structure.
1462 1616 * flags:
1463 1617 * PRELEASE_CLEAR Clear all tracing flags.
1464 1618 * PRELEASE_RETAIN Retain current tracing flags.
1465 1619 * PRELEASE_HANG Leave the process stopped and abandoned.
1466 1620 * PRELEASE_KILL Terminate the process with SIGKILL.
1467 1621 */
1468 1622 void
1469 1623 Prelease(struct ps_prochandle *P, int flags)
1470 1624 {
1471 1625 if (P->state == PS_DEAD) {
1472 1626 dprintf("Prelease: releasing handle %p PS_DEAD of pid %d\n",
1473 1627 (void *)P, (int)P->pid);
1474 1628 Pfree(P);
1475 1629 return;
1476 1630 }
1477 1631
1478 1632 if (P->state == PS_IDLE) {
1479 1633 file_info_t *fptr = list_next(&P->file_head);
1480 1634 dprintf("Prelease: releasing handle %p PS_IDLE of file %s\n",
1481 1635 (void *)P, fptr->file_pname);
1482 1636 Pfree(P);
1483 1637 return;
1484 1638 }
1485 1639
1486 1640 dprintf("Prelease: releasing handle %p pid %d\n",
1487 1641 (void *)P, (int)P->pid);
1488 1642
1489 1643 if (P->ctlfd == -1) {
1490 1644 Pfree(P);
1491 1645 return;
1492 1646 }
1493 1647
1494 1648 if (P->agentcnt > 0) {
1495 1649 P->agentcnt = 1;
1496 1650 Pdestroy_agent(P);
1497 1651 }
1498 1652
1499 1653 /*
1500 1654 * Attempt to stop the process.
1501 1655 */
1502 1656 P->state = PS_RUN;
1503 1657 (void) Pstop(P, 1000);
1504 1658
1505 1659 if (flags & PRELEASE_KILL) {
1506 1660 if (P->state == PS_STOP)
1507 1661 (void) Psetrun(P, SIGKILL, 0);
1508 1662 (void) kill(P->pid, SIGKILL);
1509 1663 Pfree(P);
1510 1664 return;
1511 1665 }
1512 1666
1513 1667 /*
1514 1668 * If we lost control, all we can do now is close the files.
1515 1669 * In this case, the last close sets the process running.
1516 1670 */
1517 1671 if (P->state != PS_STOP &&
1518 1672 (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
1519 1673 Pfree(P);
1520 1674 return;
1521 1675 }
1522 1676
1523 1677 /*
1524 1678 * We didn't lose control; we do more.
1525 1679 */
1526 1680 Psync(P);
1527 1681
1528 1682 if (flags & PRELEASE_CLEAR)
1529 1683 P->flags |= CREATED;
1530 1684
1531 1685 if (!(flags & PRELEASE_RETAIN))
1532 1686 restore_tracing_flags(P);
1533 1687
1534 1688 if (flags & PRELEASE_HANG) {
1535 1689 /* Leave the process stopped and abandoned */
1536 1690 (void) Punsetflags(P, PR_RLC|PR_KLC);
1537 1691 Pfree(P);
1538 1692 return;
1539 1693 }
1540 1694
1541 1695 /*
1542 1696 * Set the process running if we created it or if it was
1543 1697 * not originally stopped or directed to stop via /proc
1544 1698 * or if we were given the PRELEASE_CLEAR flag.
1545 1699 */
1546 1700 if ((P->flags & CREATED) ||
1547 1701 (P->orig_status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
1548 1702 (void) Psetflags(P, PR_RLC);
1549 1703 /*
1550 1704 * We do this repeatedly because the process may have
1551 1705 * more than one LWP stopped on an event of interest.
1552 1706 * This makes sure all of them are set running.
1553 1707 */
1554 1708 do {
1555 1709 if (Psetrun(P, 0, 0) == -1 && errno == EBUSY)
1556 1710 break; /* Agent LWP may be stuck */
1557 1711 } while (Pstopstatus(P, PCNULL, 0) == 0 &&
1558 1712 P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP));
1559 1713
1560 1714 if (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP))
1561 1715 dprintf("Prelease: failed to set process running\n");
1562 1716 }
1563 1717
1564 1718 Pfree(P);
1565 1719 }
1566 1720
1567 1721 /* debugging */
1568 1722 void
1569 1723 prldump(const char *caller, lwpstatus_t *lsp)
1570 1724 {
1571 1725 char name[32];
1572 1726 uint32_t bits;
1573 1727
1574 1728 switch (lsp->pr_why) {
1575 1729 case PR_REQUESTED:
1576 1730 dprintf("%s: REQUESTED\n", caller);
1577 1731 break;
1578 1732 case PR_SIGNALLED:
1579 1733 dprintf("%s: SIGNALLED %s\n", caller,
1580 1734 proc_signame(lsp->pr_what, name, sizeof (name)));
1581 1735 break;
1582 1736 case PR_FAULTED:
1583 1737 dprintf("%s: FAULTED %s\n", caller,
1584 1738 proc_fltname(lsp->pr_what, name, sizeof (name)));
1585 1739 break;
1586 1740 case PR_SYSENTRY:
1587 1741 dprintf("%s: SYSENTRY %s\n", caller,
1588 1742 proc_sysname(lsp->pr_what, name, sizeof (name)));
1589 1743 break;
1590 1744 case PR_SYSEXIT:
1591 1745 dprintf("%s: SYSEXIT %s\n", caller,
1592 1746 proc_sysname(lsp->pr_what, name, sizeof (name)));
1593 1747 break;
1594 1748 case PR_JOBCONTROL:
1595 1749 dprintf("%s: JOBCONTROL %s\n", caller,
1596 1750 proc_signame(lsp->pr_what, name, sizeof (name)));
1597 1751 break;
1598 1752 case PR_SUSPENDED:
1599 1753 dprintf("%s: SUSPENDED\n", caller);
1600 1754 break;
1601 1755 default:
1602 1756 dprintf("%s: Unknown\n", caller);
1603 1757 break;
1604 1758 }
1605 1759
1606 1760 if (lsp->pr_cursig)
1607 1761 dprintf("%s: p_cursig = %d\n", caller, lsp->pr_cursig);
1608 1762
1609 1763 bits = *((uint32_t *)&lsp->pr_lwppend);
1610 1764 if (bits)
1611 1765 dprintf("%s: pr_lwppend = 0x%.8X\n", caller, bits);
1612 1766 }
1613 1767
1614 1768 /* debugging */
1615 1769 static void
1616 1770 prdump(struct ps_prochandle *P)
1617 1771 {
1618 1772 uint32_t bits;
1619 1773
1620 1774 prldump("Pstopstatus", &P->status.pr_lwp);
1621 1775
1622 1776 bits = *((uint32_t *)&P->status.pr_sigpend);
1623 1777 if (bits)
1624 1778 dprintf("Pstopstatus: pr_sigpend = 0x%.8X\n", bits);
1625 1779 }
1626 1780
1627 1781 /*
1628 1782 * Wait for the specified process to stop or terminate.
1629 1783 * Or, just get the current status (PCNULL).
1630 1784 * Or, direct it to stop and get the current status (PCDSTOP).
1631 1785 * If the agent LWP exists, do these things to the agent,
1632 1786 * else do these things to the process as a whole.
1633 1787 */
1634 1788 int
1635 1789 Pstopstatus(struct ps_prochandle *P,
1636 1790 long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
1637 1791 uint_t msec) /* if non-zero, timeout in milliseconds */
1638 1792 {
1639 1793 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
1640 1794 long ctl[3];
1641 1795 ssize_t rc;
1642 1796 int err;
1643 1797 int old_state = P->state;
1644 1798
1645 1799 switch (P->state) {
1646 1800 case PS_RUN:
1647 1801 break;
1648 1802 case PS_STOP:
1649 1803 if (request != PCNULL && request != PCDSTOP)
1650 1804 return (0);
1651 1805 break;
1652 1806 case PS_LOST:
1653 1807 if (request != PCNULL) {
1654 1808 errno = EAGAIN;
1655 1809 return (-1);
1656 1810 }
1657 1811 break;
1658 1812 case PS_UNDEAD:
1659 1813 case PS_DEAD:
1660 1814 case PS_IDLE:
1661 1815 if (request != PCNULL) {
1662 1816 errno = ENOENT;
1663 1817 return (-1);
1664 1818 }
1665 1819 break;
1666 1820 default: /* corrupted state */
1667 1821 dprintf("Pstopstatus: corrupted state: %d\n", P->state);
1668 1822 errno = EINVAL;
1669 1823 return (-1);
1670 1824 }
1671 1825
1672 1826 ctl[0] = PCDSTOP;
1673 1827 ctl[1] = PCTWSTOP;
1674 1828 ctl[2] = (long)msec;
1675 1829 rc = 0;
1676 1830 switch (request) {
1677 1831 case PCSTOP:
1678 1832 rc = write(ctlfd, &ctl[0], 3*sizeof (long));
1679 1833 break;
1680 1834 case PCWSTOP:
1681 1835 rc = write(ctlfd, &ctl[1], 2*sizeof (long));
1682 1836 break;
1683 1837 case PCDSTOP:
1684 1838 rc = write(ctlfd, &ctl[0], 1*sizeof (long));
1685 1839 break;
1686 1840 case PCNULL:
1687 1841 if (P->state == PS_DEAD || P->state == PS_IDLE)
1688 1842 return (0);
1689 1843 break;
1690 1844 default: /* programming error */
1691 1845 errno = EINVAL;
1692 1846 return (-1);
1693 1847 }
1694 1848 err = (rc < 0)? errno : 0;
1695 1849 Psync(P);
1696 1850
1697 1851 if (P->agentstatfd < 0) {
1698 1852 if (pread(P->statfd, &P->status,
1699 1853 sizeof (P->status), (off_t)0) < 0)
1700 1854 err = errno;
1701 1855 } else {
1702 1856 if (pread(P->agentstatfd, &P->status.pr_lwp,
1703 1857 sizeof (P->status.pr_lwp), (off_t)0) < 0)
1704 1858 err = errno;
1705 1859 P->status.pr_flags = P->status.pr_lwp.pr_flags;
1706 1860 }
1707 1861
1708 1862 if (err) {
1709 1863 switch (err) {
1710 1864 case EINTR: /* user typed ctl-C */
1711 1865 case ERESTART:
1712 1866 dprintf("Pstopstatus: EINTR\n");
1713 1867 break;
1714 1868 case EAGAIN: /* we lost control of the the process */
1715 1869 case EOVERFLOW:
1716 1870 dprintf("Pstopstatus: PS_LOST, errno=%d\n", err);
1717 1871 P->state = PS_LOST;
1718 1872 break;
1719 1873 default: /* check for dead process */
1720 1874 if (_libproc_debug) {
1721 1875 const char *errstr;
1722 1876
1723 1877 switch (request) {
1724 1878 case PCNULL:
1725 1879 errstr = "Pstopstatus PCNULL"; break;
1726 1880 case PCSTOP:
1727 1881 errstr = "Pstopstatus PCSTOP"; break;
1728 1882 case PCDSTOP:
1729 1883 errstr = "Pstopstatus PCDSTOP"; break;
1730 1884 case PCWSTOP:
1731 1885 errstr = "Pstopstatus PCWSTOP"; break;
1732 1886 default:
1733 1887 errstr = "Pstopstatus PC???"; break;
1734 1888 }
1735 1889 dprintf("%s: %s\n", errstr, strerror(err));
1736 1890 }
1737 1891 deadcheck(P);
1738 1892 break;
1739 1893 }
1740 1894 if (err != EINTR && err != ERESTART) {
1741 1895 errno = err;
1742 1896 return (-1);
1743 1897 }
1744 1898 }
1745 1899
1746 1900 if (!(P->status.pr_flags & PR_STOPPED)) {
1747 1901 P->state = PS_RUN;
1748 1902 if (request == PCNULL || request == PCDSTOP || msec != 0)
1749 1903 return (0);
1750 1904 dprintf("Pstopstatus: process is not stopped\n");
1751 1905 errno = EPROTO;
1752 1906 return (-1);
1753 1907 }
1754 1908
1755 1909 P->state = PS_STOP;
1756 1910
1757 1911 if (_libproc_debug) /* debugging */
1758 1912 prdump(P);
1759 1913
1760 1914 /*
1761 1915 * If the process was already stopped coming into Pstopstatus(),
1762 1916 * then don't use its PC to set P->sysaddr since it may have been
1763 1917 * changed since the time the process originally stopped.
1764 1918 */
1765 1919 if (old_state == PS_STOP)
1766 1920 return (0);
1767 1921
1768 1922 switch (P->status.pr_lwp.pr_why) {
1769 1923 case PR_SYSENTRY:
1770 1924 case PR_SYSEXIT:
1771 1925 if (Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC],
1772 1926 &P->sysaddr) == 0)
1773 1927 P->sysaddr = P->status.pr_lwp.pr_reg[R_PC];
1774 1928 break;
1775 1929 case PR_REQUESTED:
1776 1930 case PR_SIGNALLED:
1777 1931 case PR_FAULTED:
1778 1932 case PR_JOBCONTROL:
1779 1933 case PR_SUSPENDED:
1780 1934 break;
1781 1935 default:
1782 1936 errno = EPROTO;
1783 1937 return (-1);
1784 1938 }
1785 1939
1786 1940 return (0);
1787 1941 }
1788 1942
1789 1943 /*
1790 1944 * Wait for the process to stop for any reason.
1791 1945 */
1792 1946 int
1793 1947 Pwait(struct ps_prochandle *P, uint_t msec)
1794 1948 {
1795 1949 return (Pstopstatus(P, PCWSTOP, msec));
1796 1950 }
1797 1951
1798 1952 /*
1799 1953 * Direct the process to stop; wait for it to stop.
1800 1954 */
1801 1955 int
1802 1956 Pstop(struct ps_prochandle *P, uint_t msec)
1803 1957 {
1804 1958 return (Pstopstatus(P, PCSTOP, msec));
1805 1959 }
1806 1960
1807 1961 /*
1808 1962 * Direct the process to stop; don't wait.
1809 1963 */
1810 1964 int
1811 1965 Pdstop(struct ps_prochandle *P)
1812 1966 {
1813 1967 return (Pstopstatus(P, PCDSTOP, 0));
1814 1968 }
1815 1969
1816 1970 static void
1817 1971 deadcheck(struct ps_prochandle *P)
1818 1972 {
1819 1973 int fd;
1820 1974 void *buf;
1821 1975 size_t size;
1822 1976
1823 1977 if (P->statfd < 0)
1824 1978 P->state = PS_UNDEAD;
1825 1979 else {
1826 1980 if (P->agentstatfd < 0) {
1827 1981 fd = P->statfd;
1828 1982 buf = &P->status;
1829 1983 size = sizeof (P->status);
1830 1984 } else {
1831 1985 fd = P->agentstatfd;
1832 1986 buf = &P->status.pr_lwp;
1833 1987 size = sizeof (P->status.pr_lwp);
1834 1988 }
1835 1989 while (pread(fd, buf, size, (off_t)0) != size) {
1836 1990 switch (errno) {
1837 1991 default:
1838 1992 P->state = PS_UNDEAD;
1839 1993 break;
1840 1994 case EINTR:
1841 1995 case ERESTART:
1842 1996 continue;
1843 1997 case EAGAIN:
1844 1998 P->state = PS_LOST;
1845 1999 break;
1846 2000 }
1847 2001 break;
1848 2002 }
1849 2003 P->status.pr_flags = P->status.pr_lwp.pr_flags;
1850 2004 }
1851 2005 }
1852 2006
1853 2007 /*
1854 2008 * Get the value of one register from stopped process.
1855 2009 */
1856 2010 int
1857 2011 Pgetareg(struct ps_prochandle *P, int regno, prgreg_t *preg)
1858 2012 {
1859 2013 if (regno < 0 || regno >= NPRGREG) {
1860 2014 errno = EINVAL;
1861 2015 return (-1);
1862 2016 }
1863 2017
1864 2018 if (P->state == PS_IDLE) {
1865 2019 errno = ENODATA;
1866 2020 return (-1);
1867 2021 }
1868 2022
1869 2023 if (P->state != PS_STOP && P->state != PS_DEAD) {
1870 2024 errno = EBUSY;
1871 2025 return (-1);
1872 2026 }
1873 2027
1874 2028 *preg = P->status.pr_lwp.pr_reg[regno];
1875 2029 return (0);
1876 2030 }
1877 2031
1878 2032 /*
1879 2033 * Put value of one register into stopped process.
1880 2034 */
1881 2035 int
1882 2036 Pputareg(struct ps_prochandle *P, int regno, prgreg_t reg)
1883 2037 {
1884 2038 if (regno < 0 || regno >= NPRGREG) {
1885 2039 errno = EINVAL;
1886 2040 return (-1);
1887 2041 }
1888 2042
1889 2043 if (P->state != PS_STOP) {
1890 2044 errno = EBUSY;
1891 2045 return (-1);
1892 2046 }
1893 2047
1894 2048 P->status.pr_lwp.pr_reg[regno] = reg;
1895 2049 P->flags |= SETREGS; /* set registers before continuing */
1896 2050 return (0);
1897 2051 }
1898 2052
1899 2053 int
1900 2054 Psetrun(struct ps_prochandle *P,
1901 2055 int sig, /* signal to pass to process */
1902 2056 int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
1903 2057 {
1904 2058 int ctlfd = (P->agentctlfd >= 0) ? P->agentctlfd : P->ctlfd;
1905 2059 int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
1906 2060
1907 2061 long ctl[1 + /* PCCFAULT */
1908 2062 1 + sizeof (siginfo_t)/sizeof (long) + /* PCSSIG/PCCSIG */
1909 2063 2 ]; /* PCRUN */
1910 2064
1911 2065 long *ctlp = ctl;
1912 2066 size_t size;
1913 2067
1914 2068 if (P->state != PS_STOP && (P->status.pr_lwp.pr_flags & sbits) == 0) {
1915 2069 errno = EBUSY;
1916 2070 return (-1);
1917 2071 }
1918 2072
1919 2073 Psync(P); /* flush tracing flags and registers */
1920 2074
1921 2075 if (flags & PRCFAULT) { /* clear current fault */
1922 2076 *ctlp++ = PCCFAULT;
1923 2077 flags &= ~PRCFAULT;
1924 2078 }
1925 2079
1926 2080 if (flags & PRCSIG) { /* clear current signal */
1927 2081 *ctlp++ = PCCSIG;
1928 2082 flags &= ~PRCSIG;
1929 2083 } else if (sig && sig != P->status.pr_lwp.pr_cursig) {
1930 2084 /* make current signal */
1931 2085 siginfo_t *infop;
1932 2086
1933 2087 *ctlp++ = PCSSIG;
1934 2088 infop = (siginfo_t *)ctlp;
1935 2089 (void) memset(infop, 0, sizeof (*infop));
1936 2090 infop->si_signo = sig;
1937 2091 ctlp += sizeof (siginfo_t) / sizeof (long);
1938 2092 }
1939 2093
1940 2094 *ctlp++ = PCRUN;
1941 2095 *ctlp++ = flags;
1942 2096 size = (char *)ctlp - (char *)ctl;
1943 2097
1944 2098 P->info_valid = 0; /* will need to update map and file info */
1945 2099
1946 2100 /*
1947 2101 * If we've cached ucontext-list information while we were stopped,
1948 2102 * free it now.
1949 2103 */
1950 2104 if (P->ucaddrs != NULL) {
1951 2105 free(P->ucaddrs);
1952 2106 P->ucaddrs = NULL;
1953 2107 P->ucnelems = 0;
1954 2108 }
1955 2109
1956 2110 if (write(ctlfd, ctl, size) != size) {
1957 2111 /* If it is dead or lost, return the real status, not PS_RUN */
1958 2112 if (errno == ENOENT || errno == EAGAIN) {
1959 2113 (void) Pstopstatus(P, PCNULL, 0);
1960 2114 return (0);
1961 2115 }
1962 2116 /* If it is not in a jobcontrol stop, issue an error message */
1963 2117 if (errno != EBUSY ||
1964 2118 P->status.pr_lwp.pr_why != PR_JOBCONTROL) {
1965 2119 dprintf("Psetrun: %s\n", strerror(errno));
1966 2120 return (-1);
1967 2121 }
1968 2122 /* Otherwise pretend that the job-stopped process is running */
1969 2123 }
1970 2124
↓ open down ↓ |
739 lines elided |
↑ open up ↑ |
1971 2125 P->state = PS_RUN;
1972 2126 return (0);
1973 2127 }
1974 2128
1975 2129 ssize_t
1976 2130 Pread(struct ps_prochandle *P,
1977 2131 void *buf, /* caller's buffer */
1978 2132 size_t nbyte, /* number of bytes to read */
1979 2133 uintptr_t address) /* address in process */
1980 2134 {
1981 - return (P->ops->p_pread(P, buf, nbyte, address));
2135 + return (P->ops.pop_pread(P, buf, nbyte, address, P->data));
1982 2136 }
1983 2137
1984 2138 ssize_t
1985 2139 Pread_string(struct ps_prochandle *P,
1986 - char *buf, /* caller's buffer */
2140 + char *buf, /* caller's buffer */
1987 2141 size_t size, /* upper limit on bytes to read */
1988 2142 uintptr_t addr) /* address in process */
1989 2143 {
1990 2144 enum { STRSZ = 40 };
1991 2145 char string[STRSZ + 1];
1992 2146 ssize_t leng = 0;
1993 2147 int nbyte;
1994 2148
1995 2149 if (size < 2) {
1996 2150 errno = EINVAL;
1997 2151 return (-1);
1998 2152 }
1999 2153
2000 2154 size--; /* ensure trailing null fits in buffer */
2001 2155
2002 2156 *buf = '\0';
2003 2157 string[STRSZ] = '\0';
2004 2158
2005 2159 for (nbyte = STRSZ; nbyte == STRSZ && leng < size; addr += STRSZ) {
2006 - if ((nbyte = P->ops->p_pread(P, string, STRSZ, addr)) <= 0) {
2160 + if ((nbyte = P->ops.pop_pread(P, string, STRSZ, addr,
2161 + P->data)) <= 0) {
2007 2162 buf[leng] = '\0';
2008 2163 return (leng ? leng : -1);
2009 2164 }
2010 2165 if ((nbyte = strlen(string)) > 0) {
2011 2166 if (leng + nbyte > size)
2012 2167 nbyte = size - leng;
2013 2168 (void) strncpy(buf + leng, string, nbyte);
2014 2169 leng += nbyte;
2015 2170 }
2016 2171 }
2017 2172 buf[leng] = '\0';
2018 2173 return (leng);
2019 2174 }
2020 2175
2021 2176 ssize_t
2022 2177 Pwrite(struct ps_prochandle *P,
2023 2178 const void *buf, /* caller's buffer */
2024 2179 size_t nbyte, /* number of bytes to write */
2025 2180 uintptr_t address) /* address in process */
2026 2181 {
2027 - return (P->ops->p_pwrite(P, buf, nbyte, address));
2182 + return (P->ops.pop_pwrite(P, buf, nbyte, address, P->data));
2028 2183 }
2029 2184
2030 2185 int
2031 2186 Pclearsig(struct ps_prochandle *P)
2032 2187 {
2033 2188 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2034 2189 long ctl = PCCSIG;
2035 2190
2036 2191 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
2037 2192 return (-1);
2038 2193 P->status.pr_lwp.pr_cursig = 0;
2039 2194 return (0);
2040 2195 }
2041 2196
2042 2197 int
2043 2198 Pclearfault(struct ps_prochandle *P)
2044 2199 {
2045 2200 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2046 2201 long ctl = PCCFAULT;
2047 2202
2048 2203 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
2049 2204 return (-1);
2050 2205 return (0);
2051 2206 }
2052 2207
2053 2208 /*
2054 2209 * Set a breakpoint trap, return original instruction.
2055 2210 */
2056 2211 int
2057 2212 Psetbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t *saved)
2058 2213 {
2059 2214 long ctl[1 + sizeof (priovec_t) / sizeof (long) + /* PCREAD */
2060 2215 1 + sizeof (priovec_t) / sizeof (long)]; /* PCWRITE */
2061 2216 long *ctlp = ctl;
2062 2217 size_t size;
2063 2218 priovec_t *iovp;
2064 2219 instr_t bpt = BPT;
2065 2220 instr_t old;
2066 2221
2067 2222 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2068 2223 P->state == PS_IDLE) {
2069 2224 errno = ENOENT;
2070 2225 return (-1);
2071 2226 }
2072 2227
2073 2228 /* fetch the old instruction */
2074 2229 *ctlp++ = PCREAD;
2075 2230 iovp = (priovec_t *)ctlp;
2076 2231 iovp->pio_base = &old;
2077 2232 iovp->pio_len = sizeof (old);
2078 2233 iovp->pio_offset = address;
2079 2234 ctlp += sizeof (priovec_t) / sizeof (long);
2080 2235
2081 2236 /* write the BPT instruction */
2082 2237 *ctlp++ = PCWRITE;
2083 2238 iovp = (priovec_t *)ctlp;
2084 2239 iovp->pio_base = &bpt;
2085 2240 iovp->pio_len = sizeof (bpt);
2086 2241 iovp->pio_offset = address;
2087 2242 ctlp += sizeof (priovec_t) / sizeof (long);
2088 2243
2089 2244 size = (char *)ctlp - (char *)ctl;
2090 2245 if (write(P->ctlfd, ctl, size) != size)
2091 2246 return (-1);
2092 2247
2093 2248 /*
2094 2249 * Fail if there was already a breakpoint there from another debugger
2095 2250 * or DTrace's user-level tracing on x86.
2096 2251 */
2097 2252 if (old == BPT) {
2098 2253 errno = EBUSY;
2099 2254 return (-1);
2100 2255 }
2101 2256
2102 2257 *saved = (ulong_t)old;
2103 2258 return (0);
2104 2259 }
2105 2260
2106 2261 /*
2107 2262 * Restore original instruction where a breakpoint was set.
2108 2263 */
2109 2264 int
2110 2265 Pdelbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t saved)
2111 2266 {
2112 2267 instr_t old = (instr_t)saved;
2113 2268 instr_t cur;
2114 2269
2115 2270 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2116 2271 P->state == PS_IDLE) {
2117 2272 errno = ENOENT;
2118 2273 return (-1);
2119 2274 }
2120 2275
2121 2276 /*
2122 2277 * If the breakpoint instruction we had placed has been overwritten
2123 2278 * with a new instruction, then don't try to replace it with the
2124 2279 * old instruction. Doing do can cause problems with self-modifying
2125 2280 * code -- PLTs for example. If the Pread() fails, we assume that we
2126 2281 * should proceed though most likely the Pwrite() will also fail.
2127 2282 */
2128 2283 if (Pread(P, &cur, sizeof (cur), address) == sizeof (cur) &&
2129 2284 cur != BPT)
2130 2285 return (0);
2131 2286
2132 2287 if (Pwrite(P, &old, sizeof (old), address) != sizeof (old))
2133 2288 return (-1);
2134 2289
2135 2290 return (0);
2136 2291 }
2137 2292
2138 2293 /*
2139 2294 * Common code for Pxecbkpt() and Lxecbkpt().
2140 2295 * Develop the array of requests that will do the job, then
2141 2296 * write them to the specified control file descriptor.
2142 2297 * Return the non-zero errno if the write fails.
2143 2298 */
2144 2299 static int
2145 2300 execute_bkpt(
2146 2301 int ctlfd, /* process or LWP control file descriptor */
2147 2302 const fltset_t *faultset, /* current set of traced faults */
2148 2303 const sigset_t *sigmask, /* current signal mask */
2149 2304 uintptr_t address, /* address of breakpint */
2150 2305 ulong_t saved) /* the saved instruction */
2151 2306 {
2152 2307 long ctl[
2153 2308 1 + sizeof (sigset_t) / sizeof (long) + /* PCSHOLD */
2154 2309 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
2155 2310 1 + sizeof (priovec_t) / sizeof (long) + /* PCWRITE */
2156 2311 2 + /* PCRUN */
2157 2312 1 + /* PCWSTOP */
2158 2313 1 + /* PCCFAULT */
2159 2314 1 + sizeof (priovec_t) / sizeof (long) + /* PCWRITE */
2160 2315 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
2161 2316 1 + sizeof (sigset_t) / sizeof (long)]; /* PCSHOLD */
2162 2317 long *ctlp = ctl;
2163 2318 sigset_t unblock;
2164 2319 size_t size;
2165 2320 ssize_t ssize;
2166 2321 priovec_t *iovp;
2167 2322 sigset_t *holdp;
2168 2323 fltset_t *faultp;
2169 2324 instr_t old = (instr_t)saved;
2170 2325 instr_t bpt = BPT;
2171 2326 int error = 0;
2172 2327
2173 2328 /* block our signals for the duration */
2174 2329 (void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
2175 2330
2176 2331 /* hold posted signals */
2177 2332 *ctlp++ = PCSHOLD;
2178 2333 holdp = (sigset_t *)ctlp;
2179 2334 prfillset(holdp);
2180 2335 prdelset(holdp, SIGKILL);
2181 2336 prdelset(holdp, SIGSTOP);
2182 2337 ctlp += sizeof (sigset_t) / sizeof (long);
2183 2338
2184 2339 /* force tracing of FLTTRACE */
2185 2340 if (!(prismember(faultset, FLTTRACE))) {
2186 2341 *ctlp++ = PCSFAULT;
2187 2342 faultp = (fltset_t *)ctlp;
2188 2343 *faultp = *faultset;
2189 2344 praddset(faultp, FLTTRACE);
2190 2345 ctlp += sizeof (fltset_t) / sizeof (long);
2191 2346 }
2192 2347
2193 2348 /* restore the old instruction */
2194 2349 *ctlp++ = PCWRITE;
2195 2350 iovp = (priovec_t *)ctlp;
2196 2351 iovp->pio_base = &old;
2197 2352 iovp->pio_len = sizeof (old);
2198 2353 iovp->pio_offset = address;
2199 2354 ctlp += sizeof (priovec_t) / sizeof (long);
2200 2355
2201 2356 /* clear current signal and fault; set running w/ single-step */
2202 2357 *ctlp++ = PCRUN;
2203 2358 *ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
2204 2359
2205 2360 /* wait for stop, cancel the fault */
2206 2361 *ctlp++ = PCWSTOP;
2207 2362 *ctlp++ = PCCFAULT;
2208 2363
2209 2364 /* restore the breakpoint trap */
2210 2365 *ctlp++ = PCWRITE;
2211 2366 iovp = (priovec_t *)ctlp;
2212 2367 iovp->pio_base = &bpt;
2213 2368 iovp->pio_len = sizeof (bpt);
2214 2369 iovp->pio_offset = address;
2215 2370 ctlp += sizeof (priovec_t) / sizeof (long);
2216 2371
2217 2372 /* restore fault tracing set */
2218 2373 if (!(prismember(faultset, FLTTRACE))) {
2219 2374 *ctlp++ = PCSFAULT;
2220 2375 *(fltset_t *)ctlp = *faultset;
2221 2376 ctlp += sizeof (fltset_t) / sizeof (long);
2222 2377 }
2223 2378
2224 2379 /* restore the hold mask */
2225 2380 *ctlp++ = PCSHOLD;
2226 2381 *(sigset_t *)ctlp = *sigmask;
2227 2382 ctlp += sizeof (sigset_t) / sizeof (long);
2228 2383
2229 2384 size = (char *)ctlp - (char *)ctl;
2230 2385 if ((ssize = write(ctlfd, ctl, size)) != size)
2231 2386 error = (ssize == -1)? errno : EINTR;
2232 2387 (void) sigprocmask(SIG_SETMASK, &unblock, NULL);
2233 2388 return (error);
2234 2389 }
2235 2390
2236 2391 /*
2237 2392 * Step over a breakpoint, i.e., execute the instruction that
2238 2393 * really belongs at the breakpoint location (the current %pc)
2239 2394 * and leave the process stopped at the next instruction.
2240 2395 */
2241 2396 int
2242 2397 Pxecbkpt(struct ps_prochandle *P, ulong_t saved)
2243 2398 {
2244 2399 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2245 2400 int rv, error;
2246 2401
2247 2402 if (P->state != PS_STOP) {
2248 2403 errno = EBUSY;
2249 2404 return (-1);
2250 2405 }
2251 2406
2252 2407 Psync(P);
2253 2408
2254 2409 error = execute_bkpt(ctlfd,
2255 2410 &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold,
2256 2411 P->status.pr_lwp.pr_reg[R_PC], saved);
2257 2412 rv = Pstopstatus(P, PCNULL, 0);
2258 2413
2259 2414 if (error != 0) {
2260 2415 if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
2261 2416 error == EBUSY) { /* jobcontrol stop -- back off */
2262 2417 P->state = PS_RUN;
2263 2418 return (0);
2264 2419 }
2265 2420 if (error == ENOENT)
2266 2421 return (0);
2267 2422 errno = error;
2268 2423 return (-1);
2269 2424 }
2270 2425
2271 2426 return (rv);
2272 2427 }
2273 2428
2274 2429 /*
2275 2430 * Install the watchpoint described by wp.
2276 2431 */
2277 2432 int
2278 2433 Psetwapt(struct ps_prochandle *P, const prwatch_t *wp)
2279 2434 {
2280 2435 long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
2281 2436 prwatch_t *cwp = (prwatch_t *)&ctl[1];
2282 2437
2283 2438 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2284 2439 P->state == PS_IDLE) {
2285 2440 errno = ENOENT;
2286 2441 return (-1);
2287 2442 }
2288 2443
2289 2444 ctl[0] = PCWATCH;
2290 2445 cwp->pr_vaddr = wp->pr_vaddr;
2291 2446 cwp->pr_size = wp->pr_size;
2292 2447 cwp->pr_wflags = wp->pr_wflags;
2293 2448
2294 2449 if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
2295 2450 return (-1);
2296 2451
2297 2452 return (0);
2298 2453 }
2299 2454
2300 2455 /*
2301 2456 * Remove the watchpoint described by wp.
2302 2457 */
2303 2458 int
2304 2459 Pdelwapt(struct ps_prochandle *P, const prwatch_t *wp)
2305 2460 {
2306 2461 long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
2307 2462 prwatch_t *cwp = (prwatch_t *)&ctl[1];
2308 2463
2309 2464 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2310 2465 P->state == PS_IDLE) {
2311 2466 errno = ENOENT;
2312 2467 return (-1);
2313 2468 }
2314 2469
2315 2470 ctl[0] = PCWATCH;
2316 2471 cwp->pr_vaddr = wp->pr_vaddr;
2317 2472 cwp->pr_size = wp->pr_size;
2318 2473 cwp->pr_wflags = 0;
2319 2474
2320 2475 if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
2321 2476 return (-1);
2322 2477
2323 2478 return (0);
2324 2479 }
2325 2480
2326 2481 /*
2327 2482 * Common code for Pxecwapt() and Lxecwapt(). Develop the array of requests
2328 2483 * that will do the job, then write them to the specified control file
2329 2484 * descriptor. Return the non-zero errno if the write fails.
2330 2485 */
2331 2486 static int
2332 2487 execute_wapt(
2333 2488 int ctlfd, /* process or LWP control file descriptor */
2334 2489 const fltset_t *faultset, /* current set of traced faults */
2335 2490 const sigset_t *sigmask, /* current signal mask */
2336 2491 const prwatch_t *wp) /* watchpoint descriptor */
2337 2492 {
2338 2493 long ctl[
2339 2494 1 + sizeof (sigset_t) / sizeof (long) + /* PCSHOLD */
2340 2495 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
2341 2496 1 + sizeof (prwatch_t) / sizeof (long) + /* PCWATCH */
2342 2497 2 + /* PCRUN */
2343 2498 1 + /* PCWSTOP */
2344 2499 1 + /* PCCFAULT */
2345 2500 1 + sizeof (prwatch_t) / sizeof (long) + /* PCWATCH */
2346 2501 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
2347 2502 1 + sizeof (sigset_t) / sizeof (long)]; /* PCSHOLD */
2348 2503
2349 2504 long *ctlp = ctl;
2350 2505 int error = 0;
2351 2506
2352 2507 sigset_t unblock;
2353 2508 sigset_t *holdp;
2354 2509 fltset_t *faultp;
2355 2510 prwatch_t *prw;
2356 2511 ssize_t ssize;
2357 2512 size_t size;
2358 2513
2359 2514 (void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
2360 2515
2361 2516 /*
2362 2517 * Hold all posted signals in the victim process prior to stepping.
2363 2518 */
2364 2519 *ctlp++ = PCSHOLD;
2365 2520 holdp = (sigset_t *)ctlp;
2366 2521 prfillset(holdp);
2367 2522 prdelset(holdp, SIGKILL);
2368 2523 prdelset(holdp, SIGSTOP);
2369 2524 ctlp += sizeof (sigset_t) / sizeof (long);
2370 2525
2371 2526 /*
2372 2527 * Force tracing of FLTTRACE since we need to single step.
2373 2528 */
2374 2529 if (!(prismember(faultset, FLTTRACE))) {
2375 2530 *ctlp++ = PCSFAULT;
2376 2531 faultp = (fltset_t *)ctlp;
2377 2532 *faultp = *faultset;
2378 2533 praddset(faultp, FLTTRACE);
2379 2534 ctlp += sizeof (fltset_t) / sizeof (long);
2380 2535 }
2381 2536
2382 2537 /*
2383 2538 * Clear only the current watchpoint by setting pr_wflags to zero.
2384 2539 */
2385 2540 *ctlp++ = PCWATCH;
2386 2541 prw = (prwatch_t *)ctlp;
2387 2542 prw->pr_vaddr = wp->pr_vaddr;
2388 2543 prw->pr_size = wp->pr_size;
2389 2544 prw->pr_wflags = 0;
2390 2545 ctlp += sizeof (prwatch_t) / sizeof (long);
2391 2546
2392 2547 /*
2393 2548 * Clear the current signal and fault; set running with single-step.
2394 2549 * Then wait for the victim to stop and cancel the FLTTRACE.
2395 2550 */
2396 2551 *ctlp++ = PCRUN;
2397 2552 *ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
2398 2553 *ctlp++ = PCWSTOP;
2399 2554 *ctlp++ = PCCFAULT;
2400 2555
2401 2556 /*
2402 2557 * Restore the current watchpoint.
2403 2558 */
2404 2559 *ctlp++ = PCWATCH;
2405 2560 (void) memcpy(ctlp, wp, sizeof (prwatch_t));
2406 2561 ctlp += sizeof (prwatch_t) / sizeof (long);
2407 2562
2408 2563 /*
2409 2564 * Restore fault tracing set if we modified it.
2410 2565 */
2411 2566 if (!(prismember(faultset, FLTTRACE))) {
2412 2567 *ctlp++ = PCSFAULT;
2413 2568 *(fltset_t *)ctlp = *faultset;
2414 2569 ctlp += sizeof (fltset_t) / sizeof (long);
2415 2570 }
2416 2571
2417 2572 /*
2418 2573 * Restore the hold mask to the current hold mask (i.e. the one
2419 2574 * before we executed any of the previous operations).
2420 2575 */
2421 2576 *ctlp++ = PCSHOLD;
2422 2577 *(sigset_t *)ctlp = *sigmask;
2423 2578 ctlp += sizeof (sigset_t) / sizeof (long);
2424 2579
2425 2580 size = (char *)ctlp - (char *)ctl;
2426 2581 if ((ssize = write(ctlfd, ctl, size)) != size)
2427 2582 error = (ssize == -1)? errno : EINTR;
2428 2583 (void) sigprocmask(SIG_SETMASK, &unblock, NULL);
2429 2584 return (error);
2430 2585 }
2431 2586
2432 2587 /*
2433 2588 * Step over a watchpoint, i.e., execute the instruction that was stopped by
2434 2589 * the watchpoint, and then leave the LWP stopped at the next instruction.
2435 2590 */
2436 2591 int
2437 2592 Pxecwapt(struct ps_prochandle *P, const prwatch_t *wp)
2438 2593 {
2439 2594 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2440 2595 int rv, error;
2441 2596
2442 2597 if (P->state != PS_STOP) {
2443 2598 errno = EBUSY;
2444 2599 return (-1);
2445 2600 }
2446 2601
2447 2602 Psync(P);
2448 2603 error = execute_wapt(ctlfd,
2449 2604 &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold, wp);
2450 2605 rv = Pstopstatus(P, PCNULL, 0);
2451 2606
2452 2607 if (error != 0) {
2453 2608 if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
2454 2609 error == EBUSY) { /* jobcontrol stop -- back off */
2455 2610 P->state = PS_RUN;
2456 2611 return (0);
2457 2612 }
2458 2613 if (error == ENOENT)
2459 2614 return (0);
2460 2615 errno = error;
2461 2616 return (-1);
2462 2617 }
2463 2618
2464 2619 return (rv);
2465 2620 }
2466 2621
2467 2622 int
2468 2623 Psetflags(struct ps_prochandle *P, long flags)
2469 2624 {
2470 2625 int rc;
2471 2626 long ctl[2];
2472 2627
2473 2628 ctl[0] = PCSET;
2474 2629 ctl[1] = flags;
2475 2630
2476 2631 if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
2477 2632 rc = -1;
2478 2633 } else {
2479 2634 P->status.pr_flags |= flags;
2480 2635 P->status.pr_lwp.pr_flags |= flags;
2481 2636 rc = 0;
2482 2637 }
2483 2638
2484 2639 return (rc);
2485 2640 }
2486 2641
2487 2642 int
2488 2643 Punsetflags(struct ps_prochandle *P, long flags)
2489 2644 {
2490 2645 int rc;
2491 2646 long ctl[2];
2492 2647
2493 2648 ctl[0] = PCUNSET;
2494 2649 ctl[1] = flags;
2495 2650
2496 2651 if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
2497 2652 rc = -1;
2498 2653 } else {
2499 2654 P->status.pr_flags &= ~flags;
2500 2655 P->status.pr_lwp.pr_flags &= ~flags;
2501 2656 rc = 0;
2502 2657 }
2503 2658
2504 2659 return (rc);
2505 2660 }
2506 2661
2507 2662 /*
2508 2663 * Common function to allow clients to manipulate the action to be taken
2509 2664 * on receipt of a signal, receipt of machine fault, entry to a system call,
2510 2665 * or exit from a system call. We make use of our private prset_* functions
2511 2666 * in order to make this code be common. The 'which' parameter identifies
2512 2667 * the code for the event of interest (0 means change the entire set), and
2513 2668 * the 'stop' parameter is a boolean indicating whether the process should
2514 2669 * stop when the event of interest occurs. The previous value is returned
2515 2670 * to the caller; -1 is returned if an error occurred.
2516 2671 */
2517 2672 static int
2518 2673 Psetaction(struct ps_prochandle *P, void *sp, size_t size,
2519 2674 uint_t flag, int max, int which, int stop)
2520 2675 {
2521 2676 int oldval;
2522 2677
2523 2678 if (which < 0 || which > max) {
2524 2679 errno = EINVAL;
2525 2680 return (-1);
2526 2681 }
2527 2682
2528 2683 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2529 2684 P->state == PS_IDLE) {
2530 2685 errno = ENOENT;
2531 2686 return (-1);
2532 2687 }
2533 2688
2534 2689 oldval = prset_ismember(sp, size, which) ? TRUE : FALSE;
2535 2690
2536 2691 if (stop) {
2537 2692 if (which == 0) {
2538 2693 prset_fill(sp, size);
2539 2694 P->flags |= flag;
2540 2695 } else if (!oldval) {
2541 2696 prset_add(sp, size, which);
2542 2697 P->flags |= flag;
2543 2698 }
2544 2699 } else {
2545 2700 if (which == 0) {
2546 2701 prset_empty(sp, size);
2547 2702 P->flags |= flag;
2548 2703 } else if (oldval) {
2549 2704 prset_del(sp, size, which);
2550 2705 P->flags |= flag;
2551 2706 }
2552 2707 }
2553 2708
2554 2709 if (P->state == PS_RUN)
2555 2710 Psync(P);
2556 2711
2557 2712 return (oldval);
2558 2713 }
2559 2714
2560 2715 /*
2561 2716 * Set action on specified signal.
2562 2717 */
2563 2718 int
2564 2719 Psignal(struct ps_prochandle *P, int which, int stop)
2565 2720 {
2566 2721 int oldval;
2567 2722
2568 2723 if (which == SIGKILL && stop != 0) {
2569 2724 errno = EINVAL;
2570 2725 return (-1);
2571 2726 }
2572 2727
2573 2728 oldval = Psetaction(P, &P->status.pr_sigtrace, sizeof (sigset_t),
2574 2729 SETSIG, PRMAXSIG, which, stop);
2575 2730
2576 2731 if (oldval != -1 && which == 0 && stop != 0)
2577 2732 prdelset(&P->status.pr_sigtrace, SIGKILL);
2578 2733
2579 2734 return (oldval);
2580 2735 }
2581 2736
2582 2737 /*
2583 2738 * Set all signal tracing flags.
2584 2739 */
2585 2740 void
2586 2741 Psetsignal(struct ps_prochandle *P, const sigset_t *set)
2587 2742 {
2588 2743 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2589 2744 P->state == PS_IDLE)
2590 2745 return;
2591 2746
2592 2747 P->status.pr_sigtrace = *set;
2593 2748 P->flags |= SETSIG;
2594 2749
2595 2750 if (P->state == PS_RUN)
2596 2751 Psync(P);
2597 2752 }
2598 2753
2599 2754 /*
2600 2755 * Set action on specified fault.
2601 2756 */
2602 2757 int
2603 2758 Pfault(struct ps_prochandle *P, int which, int stop)
2604 2759 {
2605 2760 return (Psetaction(P, &P->status.pr_flttrace, sizeof (fltset_t),
2606 2761 SETFAULT, PRMAXFAULT, which, stop));
2607 2762 }
2608 2763
2609 2764 /*
2610 2765 * Set all machine fault tracing flags.
2611 2766 */
2612 2767 void
2613 2768 Psetfault(struct ps_prochandle *P, const fltset_t *set)
2614 2769 {
2615 2770 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2616 2771 P->state == PS_IDLE)
2617 2772 return;
2618 2773
2619 2774 P->status.pr_flttrace = *set;
2620 2775 P->flags |= SETFAULT;
2621 2776
2622 2777 if (P->state == PS_RUN)
2623 2778 Psync(P);
2624 2779 }
2625 2780
2626 2781 /*
2627 2782 * Set action on specified system call entry.
2628 2783 */
2629 2784 int
2630 2785 Psysentry(struct ps_prochandle *P, int which, int stop)
2631 2786 {
2632 2787 return (Psetaction(P, &P->status.pr_sysentry, sizeof (sysset_t),
2633 2788 SETENTRY, PRMAXSYS, which, stop));
2634 2789 }
2635 2790
2636 2791 /*
2637 2792 * Set all system call entry tracing flags.
2638 2793 */
2639 2794 void
2640 2795 Psetsysentry(struct ps_prochandle *P, const sysset_t *set)
2641 2796 {
2642 2797 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2643 2798 P->state == PS_IDLE)
2644 2799 return;
2645 2800
2646 2801 P->status.pr_sysentry = *set;
2647 2802 P->flags |= SETENTRY;
2648 2803
2649 2804 if (P->state == PS_RUN)
2650 2805 Psync(P);
2651 2806 }
2652 2807
2653 2808 /*
2654 2809 * Set action on specified system call exit.
2655 2810 */
2656 2811 int
2657 2812 Psysexit(struct ps_prochandle *P, int which, int stop)
2658 2813 {
2659 2814 return (Psetaction(P, &P->status.pr_sysexit, sizeof (sysset_t),
2660 2815 SETEXIT, PRMAXSYS, which, stop));
2661 2816 }
2662 2817
2663 2818 /*
2664 2819 * Set all system call exit tracing flags.
2665 2820 */
2666 2821 void
2667 2822 Psetsysexit(struct ps_prochandle *P, const sysset_t *set)
2668 2823 {
2669 2824 if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2670 2825 P->state == PS_IDLE)
2671 2826 return;
2672 2827
2673 2828 P->status.pr_sysexit = *set;
2674 2829 P->flags |= SETEXIT;
2675 2830
2676 2831 if (P->state == PS_RUN)
2677 2832 Psync(P);
2678 2833 }
2679 2834
2680 2835 /*
2681 2836 * Utility function to read the contents of a file that contains a
2682 2837 * prheader_t at the start (/proc/pid/lstatus or /proc/pid/lpsinfo).
2683 2838 * Returns a malloc()d buffer or NULL on failure.
2684 2839 */
2685 2840 static prheader_t *
2686 2841 read_lfile(struct ps_prochandle *P, const char *lname)
2687 2842 {
2688 2843 prheader_t *Lhp;
2689 2844 char lpath[PATH_MAX];
2690 2845 struct stat64 statb;
2691 2846 int fd;
2692 2847 size_t size;
2693 2848 ssize_t rval;
2694 2849
2695 2850 (void) snprintf(lpath, sizeof (lpath), "%s/%d/%s", procfs_path,
2696 2851 (int)P->status.pr_pid, lname);
2697 2852 if ((fd = open(lpath, O_RDONLY)) < 0 || fstat64(fd, &statb) != 0) {
2698 2853 if (fd >= 0)
2699 2854 (void) close(fd);
2700 2855 return (NULL);
2701 2856 }
2702 2857
2703 2858 /*
2704 2859 * 'size' is just the initial guess at the buffer size.
2705 2860 * It will have to grow if the number of lwps increases
2706 2861 * while we are looking at the process.
2707 2862 * 'size' must be larger than the actual file size.
2708 2863 */
2709 2864 size = statb.st_size + 32;
2710 2865
2711 2866 for (;;) {
2712 2867 if ((Lhp = malloc(size)) == NULL)
2713 2868 break;
2714 2869 if ((rval = pread(fd, Lhp, size, 0)) < 0 ||
2715 2870 rval <= sizeof (prheader_t)) {
2716 2871 free(Lhp);
2717 2872 Lhp = NULL;
2718 2873 break;
2719 2874 }
2720 2875 if (rval < size)
2721 2876 break;
2722 2877 /* need a bigger buffer */
2723 2878 free(Lhp);
2724 2879 size *= 2;
2725 2880 }
2726 2881
2727 2882 (void) close(fd);
2728 2883 return (Lhp);
2729 2884 }
2730 2885
2731 2886 /*
2732 2887 * LWP iteration interface.
2733 2888 */
2734 2889 int
2735 2890 Plwp_iter(struct ps_prochandle *P, proc_lwp_f *func, void *cd)
2736 2891 {
2737 2892 prheader_t *Lhp;
2738 2893 lwpstatus_t *Lsp;
2739 2894 long nlwp;
2740 2895 int rv;
2741 2896
2742 2897 switch (P->state) {
2743 2898 case PS_RUN:
2744 2899 (void) Pstopstatus(P, PCNULL, 0);
2745 2900 break;
2746 2901
2747 2902 case PS_STOP:
2748 2903 Psync(P);
2749 2904 break;
2750 2905
2751 2906 case PS_IDLE:
2752 2907 errno = ENODATA;
2753 2908 return (-1);
2754 2909 }
2755 2910
2756 2911 /*
2757 2912 * For either live processes or cores, the single LWP case is easy:
↓ open down ↓ |
720 lines elided |
↑ open up ↑ |
2758 2913 * the pstatus_t contains the lwpstatus_t for the only LWP.
2759 2914 */
2760 2915 if (P->status.pr_nlwp <= 1)
2761 2916 return (func(cd, &P->status.pr_lwp));
2762 2917
2763 2918 /*
2764 2919 * For the core file multi-LWP case, we just iterate through the
2765 2920 * list of LWP structs we read in from the core file.
2766 2921 */
2767 2922 if (P->state == PS_DEAD) {
2768 - lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);
2923 + core_info_t *core = P->data;
2924 + lwp_info_t *lwp = list_prev(&core->core_lwp_head);
2769 2925 uint_t i;
2770 2926
2771 - for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
2927 + for (i = 0; i < core->core_nlwp; i++, lwp = list_prev(lwp)) {
2772 2928 if (lwp->lwp_psinfo.pr_sname != 'Z' &&
2773 2929 (rv = func(cd, &lwp->lwp_status)) != 0)
2774 2930 break;
2775 2931 }
2776 2932
2777 2933 return (rv);
2778 2934 }
2779 2935
2780 2936 /*
2781 2937 * For the live process multi-LWP case, we have to work a little
2782 2938 * harder: the /proc/pid/lstatus file has the array of LWP structs.
2783 2939 */
2784 - if ((Lhp = read_lfile(P, "lstatus")) == NULL)
2940 + if ((Lhp = Plstatus(P)) == NULL)
2785 2941 return (-1);
2786 2942
2787 2943 for (nlwp = Lhp->pr_nent, Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
2788 2944 nlwp > 0;
2789 2945 nlwp--, Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize)) {
2790 2946 if ((rv = func(cd, Lsp)) != 0)
2791 2947 break;
2792 2948 }
2793 2949
2794 2950 free(Lhp);
2795 2951 return (rv);
2796 2952 }
2797 2953
2798 2954 /*
2799 2955 * Extended LWP iteration interface.
2800 2956 * Iterate over all LWPs, active and zombie.
2801 2957 */
2802 2958 int
2803 2959 Plwp_iter_all(struct ps_prochandle *P, proc_lwp_all_f *func, void *cd)
2804 2960 {
2805 2961 prheader_t *Lhp = NULL;
2806 2962 lwpstatus_t *Lsp;
2807 2963 lwpstatus_t *sp;
2808 2964 prheader_t *Lphp = NULL;
2809 2965 lwpsinfo_t *Lpsp;
2810 2966 long nstat;
2811 2967 long ninfo;
2812 2968 int rv;
2813 2969
2814 2970 retry:
2815 2971 if (Lhp != NULL)
2816 2972 free(Lhp);
2817 2973 if (Lphp != NULL)
2818 2974 free(Lphp);
2819 2975 if (P->state == PS_RUN)
2820 2976 (void) Pstopstatus(P, PCNULL, 0);
2821 2977 (void) Ppsinfo(P);
2822 2978
2823 2979 if (P->state == PS_STOP)
2824 2980 Psync(P);
2825 2981
2826 2982 /*
2827 2983 * For either live processes or cores, the single LWP case is easy:
2828 2984 * the pstatus_t contains the lwpstatus_t for the only LWP and
↓ open down ↓ |
34 lines elided |
↑ open up ↑ |
2829 2985 * the psinfo_t contains the lwpsinfo_t for the only LWP.
2830 2986 */
2831 2987 if (P->status.pr_nlwp + P->status.pr_nzomb <= 1)
2832 2988 return (func(cd, &P->status.pr_lwp, &P->psinfo.pr_lwp));
2833 2989
2834 2990 /*
2835 2991 * For the core file multi-LWP case, we just iterate through the
2836 2992 * list of LWP structs we read in from the core file.
2837 2993 */
2838 2994 if (P->state == PS_DEAD) {
2839 - lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);
2995 + core_info_t *core = P->data;
2996 + lwp_info_t *lwp = list_prev(&core->core_lwp_head);
2840 2997 uint_t i;
2841 2998
2842 - for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
2999 + for (i = 0; i < core->core_nlwp; i++, lwp = list_prev(lwp)) {
2843 3000 sp = (lwp->lwp_psinfo.pr_sname == 'Z')? NULL :
2844 3001 &lwp->lwp_status;
2845 3002 if ((rv = func(cd, sp, &lwp->lwp_psinfo)) != 0)
2846 3003 break;
2847 3004 }
2848 3005
2849 3006 return (rv);
2850 3007 }
2851 3008
2852 3009 /*
2853 - * For the live process multi-LWP case, we have to work a little
2854 - * harder: the /proc/pid/lstatus file has the array of lwpstatus_t's
2855 - * and the /proc/pid/lpsinfo file has the array of lwpsinfo_t's.
3010 + * For all other cases retrieve the array of lwpstatus_t's and
3011 + * lwpsinfo_t's.
2856 3012 */
2857 - if ((Lhp = read_lfile(P, "lstatus")) == NULL)
3013 + if ((Lhp = Plstatus(P)) == NULL)
2858 3014 return (-1);
2859 - if ((Lphp = read_lfile(P, "lpsinfo")) == NULL) {
3015 + if ((Lphp = Plpsinfo(P)) == NULL) {
2860 3016 free(Lhp);
2861 3017 return (-1);
2862 3018 }
2863 3019
2864 3020 /*
2865 3021 * If we are looking at a running process, or one we do not control,
2866 3022 * the active and zombie lwps in the process may have changed since
2867 3023 * we read the process status structure. If so, just start over.
2868 3024 */
2869 3025 if (Lhp->pr_nent != P->status.pr_nlwp ||
2870 3026 Lphp->pr_nent != P->status.pr_nlwp + P->status.pr_nzomb)
2871 3027 goto retry;
2872 3028
2873 3029 /*
2874 3030 * To be perfectly safe, prescan the two arrays, checking consistency.
2875 3031 * We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
2876 3032 * same order (the lwp directory order) in their respective files.
2877 3033 * We also rely on there being (possibly) more lwpsinfo_t's than
2878 3034 * lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
2879 3035 */
2880 3036 Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
2881 3037 Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
2882 3038 nstat = Lhp->pr_nent;
2883 3039 for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
2884 3040 if (Lpsp->pr_sname != 'Z') {
2885 3041 /*
2886 3042 * Not a zombie lwp; check for matching lwpids.
2887 3043 */
2888 3044 if (nstat == 0 || Lsp->pr_lwpid != Lpsp->pr_lwpid)
2889 3045 goto retry;
2890 3046 Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
2891 3047 nstat--;
2892 3048 }
2893 3049 Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
2894 3050 }
2895 3051 if (nstat != 0)
2896 3052 goto retry;
2897 3053
2898 3054 /*
2899 3055 * Rescan, this time for real.
2900 3056 */
2901 3057 Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
2902 3058 Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
2903 3059 for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
2904 3060 if (Lpsp->pr_sname != 'Z') {
2905 3061 sp = Lsp;
2906 3062 Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
2907 3063 } else {
2908 3064 sp = NULL;
2909 3065 }
2910 3066 if ((rv = func(cd, sp, Lpsp)) != 0)
2911 3067 break;
2912 3068 Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
2913 3069 }
2914 3070
2915 3071 free(Lhp);
2916 3072 free(Lphp);
2917 3073 return (rv);
2918 3074 }
2919 3075
2920 3076 core_content_t
2921 3077 Pcontent(struct ps_prochandle *P)
2922 3078 {
3079 + core_info_t *core = P->data;
3080 +
2923 3081 if (P->state == PS_DEAD)
2924 - return (P->core->core_content);
3082 + return (core->core_content);
2925 3083 if (P->state == PS_IDLE)
2926 3084 return (CC_CONTENT_TEXT | CC_CONTENT_DATA | CC_CONTENT_CTF);
2927 3085
2928 3086 return (CC_CONTENT_ALL);
2929 3087 }
2930 3088
2931 3089 /*
2932 3090 * =================================================================
2933 3091 * The remainder of the functions in this file are for the
2934 3092 * control of individual LWPs in the controlled process.
2935 3093 * =================================================================
2936 3094 */
2937 3095
2938 3096 /*
2939 3097 * Find an entry in the process hash table for the specified lwpid.
2940 3098 * The entry will either point to an existing struct ps_lwphandle
2941 3099 * or it will point to an empty slot for a new struct ps_lwphandle.
2942 3100 */
2943 3101 static struct ps_lwphandle **
2944 3102 Lfind(struct ps_prochandle *P, lwpid_t lwpid)
2945 3103 {
2946 3104 struct ps_lwphandle **Lp;
2947 3105 struct ps_lwphandle *L;
2948 3106
2949 3107 for (Lp = &P->hashtab[lwpid % (HASHSIZE - 1)];
2950 3108 (L = *Lp) != NULL; Lp = &L->lwp_hash)
2951 3109 if (L->lwp_id == lwpid)
2952 3110 break;
2953 3111 return (Lp);
2954 3112 }
2955 3113
2956 3114 /*
2957 3115 * Grab an LWP contained within the controlled process.
2958 3116 * Return an opaque pointer to its LWP control structure.
2959 3117 * perr: pointer to error return code.
2960 3118 */
2961 3119 struct ps_lwphandle *
2962 3120 Lgrab(struct ps_prochandle *P, lwpid_t lwpid, int *perr)
2963 3121 {
2964 3122 struct ps_lwphandle **Lp;
2965 3123 struct ps_lwphandle *L;
2966 3124 int fd;
2967 3125 char procname[PATH_MAX];
2968 3126 char *fname;
2969 3127 int rc = 0;
2970 3128
2971 3129 (void) mutex_lock(&P->proc_lock);
2972 3130
2973 3131 if (P->state == PS_UNDEAD || P->state == PS_IDLE)
2974 3132 rc = G_NOPROC;
2975 3133 else if (P->hashtab == NULL &&
2976 3134 (P->hashtab = calloc(HASHSIZE, sizeof (struct ps_lwphandle *)))
2977 3135 == NULL)
2978 3136 rc = G_STRANGE;
2979 3137 else if (*(Lp = Lfind(P, lwpid)) != NULL)
2980 3138 rc = G_BUSY;
2981 3139 else if ((L = malloc(sizeof (struct ps_lwphandle))) == NULL)
2982 3140 rc = G_STRANGE;
2983 3141 if (rc) {
2984 3142 *perr = rc;
2985 3143 (void) mutex_unlock(&P->proc_lock);
2986 3144 return (NULL);
2987 3145 }
2988 3146
2989 3147 (void) memset(L, 0, sizeof (*L));
2990 3148 L->lwp_ctlfd = -1;
2991 3149 L->lwp_statfd = -1;
2992 3150 L->lwp_proc = P;
2993 3151 L->lwp_id = lwpid;
2994 3152 *Lp = L; /* insert into the hash table */
2995 3153
2996 3154 if (P->state == PS_DEAD) { /* core file */
2997 3155 if (getlwpstatus(P, lwpid, &L->lwp_status) == -1) {
2998 3156 rc = G_NOPROC;
2999 3157 goto err;
3000 3158 }
3001 3159 L->lwp_state = PS_DEAD;
3002 3160 *perr = 0;
3003 3161 (void) mutex_unlock(&P->proc_lock);
3004 3162 return (L);
3005 3163 }
3006 3164
3007 3165 /*
3008 3166 * Open the /proc/<pid>/lwp/<lwpid> files
3009 3167 */
3010 3168 (void) snprintf(procname, sizeof (procname), "%s/%d/lwp/%d/",
3011 3169 procfs_path, (int)P->pid, (int)lwpid);
3012 3170 fname = procname + strlen(procname);
3013 3171 (void) set_minfd();
3014 3172
3015 3173 (void) strcpy(fname, "lwpstatus");
3016 3174 if ((fd = open(procname, O_RDONLY)) < 0 ||
3017 3175 (fd = dupfd(fd, 0)) < 0) {
3018 3176 switch (errno) {
3019 3177 case ENOENT:
3020 3178 rc = G_NOPROC;
3021 3179 break;
3022 3180 default:
3023 3181 dprintf("Lgrab: failed to open %s: %s\n",
3024 3182 procname, strerror(errno));
3025 3183 rc = G_STRANGE;
3026 3184 break;
3027 3185 }
3028 3186 goto err;
3029 3187 }
3030 3188 L->lwp_statfd = fd;
3031 3189
3032 3190 if (pread(fd, &L->lwp_status, sizeof (L->lwp_status), (off_t)0) < 0) {
3033 3191 switch (errno) {
3034 3192 case ENOENT:
3035 3193 rc = G_NOPROC;
3036 3194 break;
3037 3195 default:
3038 3196 dprintf("Lgrab: failed to read %s: %s\n",
3039 3197 procname, strerror(errno));
3040 3198 rc = G_STRANGE;
3041 3199 break;
3042 3200 }
3043 3201 goto err;
3044 3202 }
3045 3203
3046 3204 (void) strcpy(fname, "lwpctl");
3047 3205 if ((fd = open(procname, O_WRONLY)) < 0 ||
3048 3206 (fd = dupfd(fd, 0)) < 0) {
3049 3207 switch (errno) {
3050 3208 case ENOENT:
3051 3209 rc = G_NOPROC;
3052 3210 break;
3053 3211 default:
3054 3212 dprintf("Lgrab: failed to open %s: %s\n",
3055 3213 procname, strerror(errno));
3056 3214 rc = G_STRANGE;
3057 3215 break;
3058 3216 }
3059 3217 goto err;
3060 3218 }
3061 3219 L->lwp_ctlfd = fd;
3062 3220
3063 3221 L->lwp_state =
3064 3222 ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
3065 3223 == (PR_STOPPED|PR_ISTOP))?
3066 3224 PS_STOP : PS_RUN;
3067 3225
3068 3226 *perr = 0;
3069 3227 (void) mutex_unlock(&P->proc_lock);
3070 3228 return (L);
3071 3229
3072 3230 err:
3073 3231 Lfree_internal(P, L);
3074 3232 *perr = rc;
3075 3233 (void) mutex_unlock(&P->proc_lock);
3076 3234 return (NULL);
3077 3235 }
3078 3236
3079 3237 /*
3080 3238 * Return a printable string corresponding to an Lgrab() error return.
3081 3239 */
3082 3240 const char *
3083 3241 Lgrab_error(int error)
3084 3242 {
3085 3243 const char *str;
3086 3244
3087 3245 switch (error) {
3088 3246 case G_NOPROC:
3089 3247 str = "no such LWP";
3090 3248 break;
3091 3249 case G_BUSY:
3092 3250 str = "LWP already grabbed";
3093 3251 break;
3094 3252 case G_STRANGE:
3095 3253 str = "unanticipated system error";
3096 3254 break;
3097 3255 default:
3098 3256 str = "unknown error";
3099 3257 break;
3100 3258 }
3101 3259
3102 3260 return (str);
3103 3261 }
3104 3262
3105 3263 /*
3106 3264 * Free an LWP control structure.
3107 3265 */
3108 3266 void
3109 3267 Lfree(struct ps_lwphandle *L)
3110 3268 {
3111 3269 struct ps_prochandle *P = L->lwp_proc;
3112 3270
3113 3271 (void) mutex_lock(&P->proc_lock);
3114 3272 Lfree_internal(P, L);
3115 3273 (void) mutex_unlock(&P->proc_lock);
3116 3274 }
3117 3275
3118 3276 static void
3119 3277 Lfree_internal(struct ps_prochandle *P, struct ps_lwphandle *L)
3120 3278 {
3121 3279 *Lfind(P, L->lwp_id) = L->lwp_hash; /* delete from hash table */
3122 3280 if (L->lwp_ctlfd >= 0)
3123 3281 (void) close(L->lwp_ctlfd);
3124 3282 if (L->lwp_statfd >= 0)
3125 3283 (void) close(L->lwp_statfd);
3126 3284
3127 3285 /* clear out the structure as a precaution against reuse */
3128 3286 (void) memset(L, 0, sizeof (*L));
3129 3287 L->lwp_ctlfd = -1;
3130 3288 L->lwp_statfd = -1;
3131 3289
3132 3290 free(L);
3133 3291 }
3134 3292
3135 3293 /*
3136 3294 * Return the state of the process, one of the PS_* values.
3137 3295 */
3138 3296 int
3139 3297 Lstate(struct ps_lwphandle *L)
3140 3298 {
3141 3299 return (L->lwp_state);
3142 3300 }
3143 3301
3144 3302 /*
3145 3303 * Return the open control file descriptor for the LWP.
3146 3304 * Clients must not close this file descriptor, nor use it
3147 3305 * after the LWP is freed.
3148 3306 */
3149 3307 int
3150 3308 Lctlfd(struct ps_lwphandle *L)
3151 3309 {
3152 3310 return (L->lwp_ctlfd);
3153 3311 }
3154 3312
3155 3313 /*
3156 3314 * Return a pointer to the LWP lwpsinfo structure.
3157 3315 * Clients should not hold on to this pointer indefinitely.
3158 3316 * It will become invalid on Lfree().
3159 3317 */
3160 3318 const lwpsinfo_t *
3161 3319 Lpsinfo(struct ps_lwphandle *L)
3162 3320 {
3163 3321 if (Plwp_getpsinfo(L->lwp_proc, L->lwp_id, &L->lwp_psinfo) == -1)
3164 3322 return (NULL);
3165 3323
3166 3324 return (&L->lwp_psinfo);
3167 3325 }
3168 3326
3169 3327 /*
3170 3328 * Return a pointer to the LWP status structure.
3171 3329 * Clients should not hold on to this pointer indefinitely.
3172 3330 * It will become invalid on Lfree().
3173 3331 */
3174 3332 const lwpstatus_t *
3175 3333 Lstatus(struct ps_lwphandle *L)
3176 3334 {
3177 3335 return (&L->lwp_status);
3178 3336 }
3179 3337
3180 3338 /*
3181 3339 * Given an LWP handle, return the process handle.
3182 3340 */
3183 3341 struct ps_prochandle *
3184 3342 Lprochandle(struct ps_lwphandle *L)
3185 3343 {
3186 3344 return (L->lwp_proc);
3187 3345 }
3188 3346
3189 3347 /*
3190 3348 * Ensure that all cached state is written to the LWP.
3191 3349 * The cached state is the LWP's signal mask and registers.
3192 3350 */
3193 3351 void
3194 3352 Lsync(struct ps_lwphandle *L)
3195 3353 {
3196 3354 int ctlfd = L->lwp_ctlfd;
3197 3355 long cmd[2];
3198 3356 iovec_t iov[4];
3199 3357 int n = 0;
3200 3358
3201 3359 if (L->lwp_flags & SETHOLD) {
3202 3360 cmd[0] = PCSHOLD;
3203 3361 iov[n].iov_base = (caddr_t)&cmd[0];
3204 3362 iov[n++].iov_len = sizeof (long);
3205 3363 iov[n].iov_base = (caddr_t)&L->lwp_status.pr_lwphold;
3206 3364 iov[n++].iov_len = sizeof (L->lwp_status.pr_lwphold);
3207 3365 }
3208 3366 if (L->lwp_flags & SETREGS) {
3209 3367 cmd[1] = PCSREG;
3210 3368 iov[n].iov_base = (caddr_t)&cmd[1];
3211 3369 iov[n++].iov_len = sizeof (long);
3212 3370 iov[n].iov_base = (caddr_t)&L->lwp_status.pr_reg[0];
3213 3371 iov[n++].iov_len = sizeof (L->lwp_status.pr_reg);
3214 3372 }
3215 3373
3216 3374 if (n == 0 || writev(ctlfd, iov, n) < 0)
3217 3375 return; /* nothing to do or write failed */
3218 3376
3219 3377 L->lwp_flags &= ~(SETHOLD|SETREGS);
3220 3378 }
3221 3379
3222 3380 /*
3223 3381 * Wait for the specified LWP to stop or terminate.
3224 3382 * Or, just get the current status (PCNULL).
3225 3383 * Or, direct it to stop and get the current status (PCDSTOP).
3226 3384 */
3227 3385 static int
3228 3386 Lstopstatus(struct ps_lwphandle *L,
3229 3387 long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
3230 3388 uint_t msec) /* if non-zero, timeout in milliseconds */
3231 3389 {
3232 3390 int ctlfd = L->lwp_ctlfd;
3233 3391 long ctl[3];
3234 3392 ssize_t rc;
3235 3393 int err;
3236 3394
3237 3395 switch (L->lwp_state) {
3238 3396 case PS_RUN:
3239 3397 break;
3240 3398 case PS_STOP:
3241 3399 if (request != PCNULL && request != PCDSTOP)
3242 3400 return (0);
3243 3401 break;
3244 3402 case PS_LOST:
3245 3403 if (request != PCNULL) {
3246 3404 errno = EAGAIN;
3247 3405 return (-1);
3248 3406 }
3249 3407 break;
3250 3408 case PS_UNDEAD:
3251 3409 case PS_DEAD:
3252 3410 if (request != PCNULL) {
3253 3411 errno = ENOENT;
3254 3412 return (-1);
3255 3413 }
3256 3414 break;
3257 3415 default: /* corrupted state */
3258 3416 dprintf("Lstopstatus: corrupted state: %d\n", L->lwp_state);
3259 3417 errno = EINVAL;
3260 3418 return (-1);
3261 3419 }
3262 3420
3263 3421 ctl[0] = PCDSTOP;
3264 3422 ctl[1] = PCTWSTOP;
3265 3423 ctl[2] = (long)msec;
3266 3424 rc = 0;
3267 3425 switch (request) {
3268 3426 case PCSTOP:
3269 3427 rc = write(ctlfd, &ctl[0], 3*sizeof (long));
3270 3428 break;
3271 3429 case PCWSTOP:
3272 3430 rc = write(ctlfd, &ctl[1], 2*sizeof (long));
3273 3431 break;
3274 3432 case PCDSTOP:
3275 3433 rc = write(ctlfd, &ctl[0], 1*sizeof (long));
3276 3434 break;
3277 3435 case PCNULL:
3278 3436 if (L->lwp_state == PS_DEAD)
3279 3437 return (0); /* Nothing else to do for cores */
3280 3438 break;
3281 3439 default: /* programming error */
3282 3440 errno = EINVAL;
3283 3441 return (-1);
3284 3442 }
3285 3443 err = (rc < 0)? errno : 0;
3286 3444 Lsync(L);
3287 3445
3288 3446 if (pread(L->lwp_statfd, &L->lwp_status,
3289 3447 sizeof (L->lwp_status), (off_t)0) < 0)
3290 3448 err = errno;
3291 3449
3292 3450 if (err) {
3293 3451 switch (err) {
3294 3452 case EINTR: /* user typed ctl-C */
3295 3453 case ERESTART:
3296 3454 dprintf("Lstopstatus: EINTR\n");
3297 3455 break;
3298 3456 case EAGAIN: /* we lost control of the the process */
3299 3457 dprintf("Lstopstatus: EAGAIN\n");
3300 3458 L->lwp_state = PS_LOST;
3301 3459 errno = err;
3302 3460 return (-1);
3303 3461 default:
3304 3462 if (_libproc_debug) {
3305 3463 const char *errstr;
3306 3464
3307 3465 switch (request) {
3308 3466 case PCNULL:
3309 3467 errstr = "Lstopstatus PCNULL"; break;
3310 3468 case PCSTOP:
3311 3469 errstr = "Lstopstatus PCSTOP"; break;
3312 3470 case PCDSTOP:
3313 3471 errstr = "Lstopstatus PCDSTOP"; break;
3314 3472 case PCWSTOP:
3315 3473 errstr = "Lstopstatus PCWSTOP"; break;
3316 3474 default:
3317 3475 errstr = "Lstopstatus PC???"; break;
3318 3476 }
3319 3477 dprintf("%s: %s\n", errstr, strerror(err));
3320 3478 }
3321 3479 L->lwp_state = PS_UNDEAD;
3322 3480 errno = err;
3323 3481 return (-1);
3324 3482 }
3325 3483 }
3326 3484
3327 3485 if ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
3328 3486 != (PR_STOPPED|PR_ISTOP)) {
3329 3487 L->lwp_state = PS_RUN;
3330 3488 if (request == PCNULL || request == PCDSTOP || msec != 0)
3331 3489 return (0);
3332 3490 dprintf("Lstopstatus: LWP is not stopped\n");
3333 3491 errno = EPROTO;
3334 3492 return (-1);
3335 3493 }
3336 3494
3337 3495 L->lwp_state = PS_STOP;
3338 3496
3339 3497 if (_libproc_debug) /* debugging */
3340 3498 prldump("Lstopstatus", &L->lwp_status);
3341 3499
3342 3500 switch (L->lwp_status.pr_why) {
3343 3501 case PR_SYSENTRY:
3344 3502 case PR_SYSEXIT:
3345 3503 case PR_REQUESTED:
3346 3504 case PR_SIGNALLED:
3347 3505 case PR_FAULTED:
3348 3506 case PR_JOBCONTROL:
3349 3507 case PR_SUSPENDED:
3350 3508 break;
3351 3509 default:
3352 3510 errno = EPROTO;
3353 3511 return (-1);
3354 3512 }
3355 3513
3356 3514 return (0);
3357 3515 }
3358 3516
3359 3517 /*
3360 3518 * Wait for the LWP to stop for any reason.
3361 3519 */
3362 3520 int
3363 3521 Lwait(struct ps_lwphandle *L, uint_t msec)
3364 3522 {
3365 3523 return (Lstopstatus(L, PCWSTOP, msec));
3366 3524 }
3367 3525
3368 3526 /*
3369 3527 * Direct the LWP to stop; wait for it to stop.
3370 3528 */
3371 3529 int
3372 3530 Lstop(struct ps_lwphandle *L, uint_t msec)
3373 3531 {
3374 3532 return (Lstopstatus(L, PCSTOP, msec));
3375 3533 }
3376 3534
3377 3535 /*
3378 3536 * Direct the LWP to stop; don't wait.
3379 3537 */
3380 3538 int
3381 3539 Ldstop(struct ps_lwphandle *L)
3382 3540 {
3383 3541 return (Lstopstatus(L, PCDSTOP, 0));
3384 3542 }
3385 3543
3386 3544 /*
3387 3545 * Get the value of one register from stopped LWP.
3388 3546 */
3389 3547 int
3390 3548 Lgetareg(struct ps_lwphandle *L, int regno, prgreg_t *preg)
3391 3549 {
3392 3550 if (regno < 0 || regno >= NPRGREG) {
3393 3551 errno = EINVAL;
3394 3552 return (-1);
3395 3553 }
3396 3554
3397 3555 if (L->lwp_state != PS_STOP) {
3398 3556 errno = EBUSY;
3399 3557 return (-1);
3400 3558 }
3401 3559
3402 3560 *preg = L->lwp_status.pr_reg[regno];
3403 3561 return (0);
3404 3562 }
3405 3563
3406 3564 /*
3407 3565 * Put value of one register into stopped LWP.
3408 3566 */
3409 3567 int
3410 3568 Lputareg(struct ps_lwphandle *L, int regno, prgreg_t reg)
3411 3569 {
3412 3570 if (regno < 0 || regno >= NPRGREG) {
3413 3571 errno = EINVAL;
3414 3572 return (-1);
3415 3573 }
3416 3574
3417 3575 if (L->lwp_state != PS_STOP) {
3418 3576 errno = EBUSY;
3419 3577 return (-1);
3420 3578 }
3421 3579
3422 3580 L->lwp_status.pr_reg[regno] = reg;
3423 3581 L->lwp_flags |= SETREGS; /* set registers before continuing */
3424 3582 return (0);
3425 3583 }
3426 3584
3427 3585 int
3428 3586 Lsetrun(struct ps_lwphandle *L,
3429 3587 int sig, /* signal to pass to LWP */
3430 3588 int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
3431 3589 {
3432 3590 int ctlfd = L->lwp_ctlfd;
3433 3591 int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
3434 3592
3435 3593 long ctl[1 + /* PCCFAULT */
3436 3594 1 + sizeof (siginfo_t)/sizeof (long) + /* PCSSIG/PCCSIG */
3437 3595 2 ]; /* PCRUN */
3438 3596
3439 3597 long *ctlp = ctl;
3440 3598 size_t size;
3441 3599
3442 3600 if (L->lwp_state != PS_STOP &&
3443 3601 (L->lwp_status.pr_flags & sbits) == 0) {
3444 3602 errno = EBUSY;
3445 3603 return (-1);
3446 3604 }
3447 3605
3448 3606 Lsync(L); /* flush registers */
3449 3607
3450 3608 if (flags & PRCFAULT) { /* clear current fault */
3451 3609 *ctlp++ = PCCFAULT;
3452 3610 flags &= ~PRCFAULT;
3453 3611 }
3454 3612
3455 3613 if (flags & PRCSIG) { /* clear current signal */
3456 3614 *ctlp++ = PCCSIG;
3457 3615 flags &= ~PRCSIG;
3458 3616 } else if (sig && sig != L->lwp_status.pr_cursig) {
3459 3617 /* make current signal */
3460 3618 siginfo_t *infop;
3461 3619
3462 3620 *ctlp++ = PCSSIG;
3463 3621 infop = (siginfo_t *)ctlp;
3464 3622 (void) memset(infop, 0, sizeof (*infop));
3465 3623 infop->si_signo = sig;
3466 3624 ctlp += sizeof (siginfo_t) / sizeof (long);
3467 3625 }
3468 3626
3469 3627 *ctlp++ = PCRUN;
3470 3628 *ctlp++ = flags;
3471 3629 size = (char *)ctlp - (char *)ctl;
3472 3630
3473 3631 L->lwp_proc->info_valid = 0; /* will need to update map and file info */
3474 3632 L->lwp_proc->state = PS_RUN;
3475 3633 L->lwp_state = PS_RUN;
3476 3634
3477 3635 if (write(ctlfd, ctl, size) != size) {
3478 3636 /* Pretend that a job-stopped LWP is running */
3479 3637 if (errno != EBUSY || L->lwp_status.pr_why != PR_JOBCONTROL)
3480 3638 return (Lstopstatus(L, PCNULL, 0));
3481 3639 }
3482 3640
3483 3641 return (0);
3484 3642 }
3485 3643
3486 3644 int
3487 3645 Lclearsig(struct ps_lwphandle *L)
3488 3646 {
3489 3647 int ctlfd = L->lwp_ctlfd;
3490 3648 long ctl = PCCSIG;
3491 3649
3492 3650 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
3493 3651 return (-1);
3494 3652 L->lwp_status.pr_cursig = 0;
3495 3653 return (0);
3496 3654 }
3497 3655
3498 3656 int
3499 3657 Lclearfault(struct ps_lwphandle *L)
3500 3658 {
3501 3659 int ctlfd = L->lwp_ctlfd;
3502 3660 long ctl = PCCFAULT;
3503 3661
3504 3662 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
3505 3663 return (-1);
3506 3664 return (0);
3507 3665 }
3508 3666
3509 3667 /*
3510 3668 * Step over a breakpoint, i.e., execute the instruction that
3511 3669 * really belongs at the breakpoint location (the current %pc)
3512 3670 * and leave the LWP stopped at the next instruction.
3513 3671 */
3514 3672 int
3515 3673 Lxecbkpt(struct ps_lwphandle *L, ulong_t saved)
3516 3674 {
3517 3675 struct ps_prochandle *P = L->lwp_proc;
3518 3676 int rv, error;
3519 3677
3520 3678 if (L->lwp_state != PS_STOP) {
3521 3679 errno = EBUSY;
3522 3680 return (-1);
3523 3681 }
3524 3682
3525 3683 Lsync(L);
3526 3684 error = execute_bkpt(L->lwp_ctlfd,
3527 3685 &P->status.pr_flttrace, &L->lwp_status.pr_lwphold,
3528 3686 L->lwp_status.pr_reg[R_PC], saved);
3529 3687 rv = Lstopstatus(L, PCNULL, 0);
3530 3688
3531 3689 if (error != 0) {
3532 3690 if (L->lwp_status.pr_why == PR_JOBCONTROL &&
3533 3691 error == EBUSY) { /* jobcontrol stop -- back off */
3534 3692 L->lwp_state = PS_RUN;
3535 3693 return (0);
3536 3694 }
3537 3695 if (error == ENOENT)
3538 3696 return (0);
3539 3697 errno = error;
3540 3698 return (-1);
3541 3699 }
3542 3700
3543 3701 return (rv);
3544 3702 }
3545 3703
3546 3704 /*
3547 3705 * Step over a watchpoint, i.e., execute the instruction that was stopped by
3548 3706 * the watchpoint, and then leave the LWP stopped at the next instruction.
3549 3707 */
3550 3708 int
3551 3709 Lxecwapt(struct ps_lwphandle *L, const prwatch_t *wp)
3552 3710 {
3553 3711 struct ps_prochandle *P = L->lwp_proc;
3554 3712 int rv, error;
3555 3713
3556 3714 if (L->lwp_state != PS_STOP) {
3557 3715 errno = EBUSY;
3558 3716 return (-1);
3559 3717 }
3560 3718
3561 3719 Lsync(L);
3562 3720 error = execute_wapt(L->lwp_ctlfd,
3563 3721 &P->status.pr_flttrace, &L->lwp_status.pr_lwphold, wp);
3564 3722 rv = Lstopstatus(L, PCNULL, 0);
3565 3723
3566 3724 if (error != 0) {
3567 3725 if (L->lwp_status.pr_why == PR_JOBCONTROL &&
3568 3726 error == EBUSY) { /* jobcontrol stop -- back off */
3569 3727 L->lwp_state = PS_RUN;
3570 3728 return (0);
3571 3729 }
3572 3730 if (error == ENOENT)
3573 3731 return (0);
3574 3732 errno = error;
3575 3733 return (-1);
3576 3734 }
3577 3735
3578 3736 return (rv);
3579 3737 }
3580 3738
3581 3739 int
3582 3740 Lstack(struct ps_lwphandle *L, stack_t *stkp)
3583 3741 {
3584 3742 struct ps_prochandle *P = L->lwp_proc;
3585 3743 uintptr_t addr = L->lwp_status.pr_ustack;
3586 3744
3587 3745 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
3588 3746 if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
3589 3747 return (-1);
3590 3748 #ifdef _LP64
3591 3749 } else {
3592 3750 stack32_t stk32;
3593 3751
3594 3752 if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
3595 3753 return (-1);
3596 3754
3597 3755 stack_32_to_n(&stk32, stkp);
3598 3756 #endif
3599 3757 }
3600 3758
3601 3759 return (0);
3602 3760 }
3603 3761
3604 3762 int
3605 3763 Lmain_stack(struct ps_lwphandle *L, stack_t *stkp)
3606 3764 {
3607 3765 struct ps_prochandle *P = L->lwp_proc;
3608 3766
3609 3767 if (Lstack(L, stkp) != 0)
3610 3768 return (-1);
3611 3769
3612 3770 /*
3613 3771 * If the SS_ONSTACK flag is set then this LWP is operating on the
3614 3772 * alternate signal stack. We can recover the original stack from
3615 3773 * pr_oldcontext.
3616 3774 */
3617 3775 if (!(stkp->ss_flags & SS_ONSTACK))
3618 3776 return (0);
3619 3777
3620 3778 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
3621 3779 ucontext_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
3622 3780
3623 3781 if (Pread(P, stkp, sizeof (*stkp),
3624 3782 (uintptr_t)&ctxp->uc_stack) != sizeof (*stkp))
3625 3783 return (-1);
3626 3784 #ifdef _LP64
3627 3785 } else {
3628 3786 ucontext32_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
3629 3787 stack32_t stk32;
3630 3788
3631 3789 if (Pread(P, &stk32, sizeof (stk32),
3632 3790 (uintptr_t)&ctxp->uc_stack) != sizeof (stk32))
3633 3791 return (-1);
3634 3792
3635 3793 stack_32_to_n(&stk32, stkp);
3636 3794 #endif
3637 3795 }
3638 3796
3639 3797 return (0);
3640 3798 }
3641 3799
3642 3800 int
3643 3801 Lalt_stack(struct ps_lwphandle *L, stack_t *stkp)
3644 3802 {
3645 3803 if (L->lwp_status.pr_altstack.ss_flags & SS_DISABLE) {
3646 3804 errno = ENODATA;
3647 3805 return (-1);
3648 3806 }
3649 3807
3650 3808 *stkp = L->lwp_status.pr_altstack;
3651 3809
3652 3810 return (0);
3653 3811 }
3654 3812
3655 3813 /*
3656 3814 * Add a mapping to the given proc handle. Resizes the array as appropriate and
3657 3815 * manages reference counts on the given file_info_t.
3658 3816 *
3659 3817 * The 'map_relocate' member is used to tell Psort_mappings() that the
3660 3818 * associated file_map pointer needs to be relocated after the mappings have
3661 3819 * been sorted. It is only set for the first mapping, and has no meaning
3662 3820 * outside these two functions.
3663 3821 */
3664 3822 int
3665 3823 Padd_mapping(struct ps_prochandle *P, off64_t off, file_info_t *fp,
3666 3824 prmap_t *pmap)
3667 3825 {
3668 3826 map_info_t *mp;
3669 3827
3670 3828 if (P->map_count == P->map_alloc) {
3671 3829 size_t next = P->map_alloc ? P->map_alloc * 2 : 16;
3672 3830
3673 3831 if ((P->mappings = realloc(P->mappings,
3674 3832 next * sizeof (map_info_t))) == NULL)
3675 3833 return (-1);
3676 3834
3677 3835 P->map_alloc = next;
3678 3836 }
3679 3837
3680 3838 mp = &P->mappings[P->map_count++];
3681 3839
3682 3840 mp->map_offset = off;
3683 3841 mp->map_pmap = *pmap;
3684 3842 mp->map_relocate = 0;
3685 3843 if ((mp->map_file = fp) != NULL) {
3686 3844 if (fp->file_map == NULL) {
3687 3845 fp->file_map = mp;
3688 3846 mp->map_relocate = 1;
3689 3847 }
3690 3848 fp->file_ref++;
3691 3849 }
3692 3850
3693 3851 return (0);
3694 3852 }
3695 3853
3696 3854 static int
3697 3855 map_sort(const void *a, const void *b)
3698 3856 {
3699 3857 const map_info_t *ap = a, *bp = b;
3700 3858
3701 3859 if (ap->map_pmap.pr_vaddr < bp->map_pmap.pr_vaddr)
3702 3860 return (-1);
3703 3861 else if (ap->map_pmap.pr_vaddr > bp->map_pmap.pr_vaddr)
3704 3862 return (1);
3705 3863 else
3706 3864 return (0);
3707 3865 }
3708 3866
3709 3867 /*
3710 3868 * Sort the current set of mappings. Should be called during target
3711 3869 * initialization after all calls to Padd_mapping() have been made.
3712 3870 */
3713 3871 void
3714 3872 Psort_mappings(struct ps_prochandle *P)
3715 3873 {
3716 3874 int i;
3717 3875 map_info_t *mp;
3718 3876
3719 3877 qsort(P->mappings, P->map_count, sizeof (map_info_t), map_sort);
↓ open down ↓ |
785 lines elided |
↑ open up ↑ |
3720 3878
3721 3879 /*
3722 3880 * Update all the file_map pointers to refer to the new locations.
3723 3881 */
3724 3882 for (i = 0; i < P->map_count; i++) {
3725 3883 mp = &P->mappings[i];
3726 3884 if (mp->map_relocate)
3727 3885 mp->map_file->file_map = mp;
3728 3886 mp->map_relocate = 0;
3729 3887 }
3888 +}
3889 +
3890 +struct ps_prochandle *
3891 +Pgrab_ops(pid_t pid, void *data, const ps_ops_t *ops, int flags)
3892 +{
3893 + struct ps_prochandle *P;
3894 +
3895 + if ((P = calloc(1, sizeof (*P))) == NULL) {
3896 + return (NULL);
3897 + }
3898 +
3899 + Pinit_ops(&P->ops, ops);
3900 + (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
3901 + P->pid = pid;
3902 + P->state = PS_STOP;
3903 + P->asfd = -1;
3904 + P->ctlfd = -1;
3905 + P->statfd = -1;
3906 + P->agentctlfd = -1;
3907 + P->agentstatfd = -1;
3908 + Pinitsym(P);
3909 + P->data = data;
3910 + Pread_status(P);
3911 +
3912 + if (flags & PGRAB_INCORE) {
3913 + P->flags |= INCORE;
3914 + }
3915 +
3916 + return (P);
3730 3917 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX