Print this page
10132 smatch fixes for MDB
Reviewed by: Andy Fiddaman <andy@omniosce.org>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mdb/common/mdb/mdb_proc.c
+++ new/usr/src/cmd/mdb/common/mdb/mdb_proc.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]
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 /*
27 27 * Copyright 2018 Joyent, Inc.
28 28 * Copyright (c) 2014 by Delphix. All rights reserved.
29 29 */
30 30
31 31 /*
32 32 * User Process Target
33 33 *
34 34 * The user process target is invoked when the -u or -p command-line options
35 35 * are used, or when an ELF executable file or ELF core file is specified on
36 36 * the command-line. This target is also selected by default when no target
37 37 * options are present. In this case, it defaults the executable name to
38 38 * "a.out". If no process or core file is currently attached, the target
39 39 * functions as a kind of virtual /dev/zero (in accordance with adb(1)
40 40 * semantics); reads from the virtual address space return zeroes and writes
41 41 * fail silently. The proc target itself is designed as a wrapper around the
42 42 * services provided by libproc.so: t->t_pshandle is set to the struct
43 43 * ps_prochandle pointer returned as a handle by libproc. The target also
44 44 * opens the executable file itself using the MDB GElf services, for
45 45 * interpreting the .symtab and .dynsym if no libproc handle has been
46 46 * initialized, and for handling i/o to and from the object file. Currently,
47 47 * the only ISA-dependent portions of the proc target are the $r and ::fpregs
48 48 * dcmds, the callbacks for t_next() and t_step_out(), and the list of named
49 49 * registers; these are linked in from the proc_isadep.c file for each ISA and
50 50 * called from the common code in this file.
51 51 *
52 52 * The user process target implements complete user process control using the
53 53 * facilities provided by libproc.so. The MDB execution control model and
54 54 * an overview of software event management is described in mdb_target.c. The
55 55 * proc target implements breakpoints by replacing the instruction of interest
56 56 * with a trap instruction, and then restoring the original instruction to step
57 57 * over the breakpoint. The idea of replacing program text with instructions
58 58 * that transfer control to the debugger dates back as far as 1951 [1]. When
59 59 * the target stops, we replace each breakpoint with the original instruction
60 60 * as part of the disarm operation. This means that no special processing is
61 61 * required for t_vread() because the instrumented instructions will never be
62 62 * seen by the debugger once the target stops. Some debuggers have improved
63 63 * start/stop performance by leaving breakpoint traps in place and then
64 64 * handling a read from a breakpoint address as a special case. Although this
65 65 * improves efficiency for a source-level debugger, it runs somewhat contrary
66 66 * to the philosophy of the low-level debugger. Since we remove the
67 67 * instructions, users can apply other external debugging tools to the process
68 68 * once it has stopped (e.g. the proc(1) tools) and not be misled by MDB
69 69 * instrumentation. The tracing of faults, signals, system calls, and
70 70 * watchpoints and general process inspection is implemented directly using
71 71 * the mechanisms provided by /proc, as described originally in [2] and [3].
72 72 *
73 73 * References
74 74 *
75 75 * [1] S. Gill, "The Diagnosis Of Mistakes In Programmes on the EDSAC",
76 76 * Proceedings of the Royal Society Series A Mathematical and Physical
77 77 * Sciences, Cambridge University Press, 206(1087), May 1951, pp. 538-554.
78 78 *
79 79 * [2] T.J. Killian, "Processes as Files", Proceedings of the USENIX Association
80 80 * Summer Conference, Salt Lake City, June 1984, pp. 203-207.
81 81 *
82 82 * [3] Roger Faulkner and Ron Gomes, "The Process File System and Process
83 83 * Model in UNIX System V", Proceedings of the USENIX Association
84 84 * Winter Conference, Dallas, January 1991, pp. 243-252.
85 85 */
86 86
87 87 #include <mdb/mdb_proc.h>
88 88 #include <mdb/mdb_disasm.h>
89 89 #include <mdb/mdb_signal.h>
90 90 #include <mdb/mdb_string.h>
91 91 #include <mdb/mdb_module.h>
92 92 #include <mdb/mdb_debug.h>
93 93 #include <mdb/mdb_conf.h>
94 94 #include <mdb/mdb_err.h>
95 95 #include <mdb/mdb_types.h>
96 96 #include <mdb/mdb.h>
97 97
98 98 #include <sys/utsname.h>
99 99 #include <sys/wait.h>
100 100 #include <sys/stat.h>
101 101 #include <termio.h>
102 102 #include <signal.h>
103 103 #include <stdio_ext.h>
104 104 #include <stdlib.h>
105 105 #include <string.h>
106 106
107 107 #define PC_FAKE -1UL /* illegal pc value unequal 0 */
108 108 #define PANIC_BUFSIZE 1024
109 109
110 110 static const char PT_EXEC_PATH[] = "a.out"; /* Default executable */
111 111 static const char PT_CORE_PATH[] = "core"; /* Default core file */
112 112
113 113 static const pt_ptl_ops_t proc_lwp_ops;
114 114 static const pt_ptl_ops_t proc_tdb_ops;
115 115 static const mdb_se_ops_t proc_brkpt_ops;
116 116 static const mdb_se_ops_t proc_wapt_ops;
117 117
118 118 static int pt_setrun(mdb_tgt_t *, mdb_tgt_status_t *, int);
119 119 static void pt_activate_common(mdb_tgt_t *);
120 120 static mdb_tgt_vespec_f pt_ignore_sig;
121 121 static mdb_tgt_se_f pt_fork;
122 122 static mdb_tgt_se_f pt_exec;
123 123
124 124 static int pt_lookup_by_name_thr(mdb_tgt_t *, const char *,
125 125 const char *, GElf_Sym *, mdb_syminfo_t *, mdb_tgt_tid_t);
126 126 static int tlsbase(mdb_tgt_t *, mdb_tgt_tid_t, Lmid_t, const char *,
127 127 psaddr_t *);
128 128
129 129 /*
130 130 * When debugging postmortem, we don't resolve names as we may very well not
131 131 * be on a system on which those names resolve.
132 132 */
133 133 #define PT_LIBPROC_RESOLVE(P) \
134 134 (!(mdb.m_flags & MDB_FL_LMRAW) && Pstate(P) != PS_DEAD)
135 135
136 136 /*
137 137 * The Perror_printf() function interposes on the default, empty libproc
138 138 * definition. It will be called to report additional information on complex
139 139 * errors, such as a corrupt core file. We just pass the args to vwarn.
140 140 */
141 141 /*ARGSUSED*/
142 142 void
143 143 Perror_printf(struct ps_prochandle *P, const char *format, ...)
144 144 {
145 145 va_list alist;
146 146
147 147 va_start(alist, format);
148 148 vwarn(format, alist);
149 149 va_end(alist);
150 150 }
151 151
152 152 /*
153 153 * Open the specified i/o backend as the a.out executable file, and attempt to
154 154 * load its standard and dynamic symbol tables. Note that if mdb_gelf_create
155 155 * succeeds, io is assigned to p_fio and is automatically held by gelf_create.
156 156 */
157 157 static mdb_gelf_file_t *
158 158 pt_open_aout(mdb_tgt_t *t, mdb_io_t *io)
159 159 {
160 160 pt_data_t *pt = t->t_data;
161 161 GElf_Sym s1, s2;
162 162
163 163 if ((pt->p_file = mdb_gelf_create(io, ET_NONE, GF_FILE)) == NULL)
164 164 return (NULL);
165 165
166 166 pt->p_symtab = mdb_gelf_symtab_create_file(pt->p_file,
167 167 SHT_SYMTAB, MDB_TGT_SYMTAB);
168 168 pt->p_dynsym = mdb_gelf_symtab_create_file(pt->p_file,
169 169 SHT_DYNSYM, MDB_TGT_DYNSYM);
170 170
171 171 /*
172 172 * If we've got an _start symbol with a zero size, prime the private
173 173 * symbol table with a copy of _start with its size set to the distance
174 174 * between _mcount and _start. We do this because DevPro has shipped
175 175 * the Intel crt1.o without proper .size directives for years, which
176 176 * precludes proper identification of _start in stack traces.
177 177 */
178 178 if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_start", &s1,
179 179 NULL) == 0 && s1.st_size == 0 &&
180 180 GELF_ST_TYPE(s1.st_info) == STT_FUNC) {
181 181 if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_mcount",
182 182 &s2, NULL) == 0 && GELF_ST_TYPE(s2.st_info) == STT_FUNC) {
183 183 s1.st_size = s2.st_value - s1.st_value;
184 184 mdb_gelf_symtab_insert(mdb.m_prsym, "_start", &s1);
185 185 }
186 186 }
187 187
188 188 pt->p_fio = io;
189 189 return (pt->p_file);
190 190 }
191 191
192 192 /*
193 193 * Destroy the symbol tables and GElf file object associated with p_fio. Note
194 194 * that we do not need to explicitly free p_fio: its reference count is
195 195 * automatically decremented by mdb_gelf_destroy, which will free it if needed.
196 196 */
197 197 static void
198 198 pt_close_aout(mdb_tgt_t *t)
199 199 {
200 200 pt_data_t *pt = t->t_data;
201 201
202 202 if (pt->p_symtab != NULL) {
203 203 mdb_gelf_symtab_destroy(pt->p_symtab);
204 204 pt->p_symtab = NULL;
205 205 }
206 206
207 207 if (pt->p_dynsym != NULL) {
208 208 mdb_gelf_symtab_destroy(pt->p_dynsym);
209 209 pt->p_dynsym = NULL;
210 210 }
211 211
212 212 if (pt->p_file != NULL) {
213 213 mdb_gelf_destroy(pt->p_file);
214 214 pt->p_file = NULL;
215 215 }
216 216
217 217 mdb_gelf_symtab_delete(mdb.m_prsym, "_start", NULL);
218 218 pt->p_fio = NULL;
219 219 }
220 220
221 221 typedef struct tdb_mapping {
222 222 const char *tm_thr_lib;
223 223 const char *tm_db_dir;
224 224 const char *tm_db_name;
225 225 } tdb_mapping_t;
226 226
227 227 static const tdb_mapping_t tdb_map[] = {
228 228 { "/lwp/amd64/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
229 229 { "/lwp/sparcv9/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
230 230 { "/lwp/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
231 231 { "/libthread.so", "/lib/", "libthread_db.so" },
232 232 { "/libc_hwcap", "/lib/", "libc_db.so" },
233 233 { "/libc.so", "/lib/", "libc_db.so" }
234 234 };
235 235
236 236 /*
237 237 * Pobject_iter callback that we use to search for the presence of libthread in
238 238 * order to load the corresponding libthread_db support. We derive the
239 239 * libthread_db path dynamically based on the libthread path. If libthread is
240 240 * found, this function returns 1 (and thus Pobject_iter aborts and returns 1)
241 241 * regardless of whether it was successful in loading the libthread_db support.
242 242 * If we iterate over all objects and no libthread is found, 0 is returned.
243 243 * Since libthread_db support was then merged into libc_db, we load either
244 244 * libc_db or libthread_db, depending on which library we see first.
245 245 */
246 246 /*ARGSUSED*/
247 247 static int
248 248 thr_check(mdb_tgt_t *t, const prmap_t *pmp, const char *name)
249 249 {
250 250 pt_data_t *pt = t->t_data;
251 251 const mdb_tdb_ops_t *ops;
252 252 char *p;
253 253
254 254 char path[MAXPATHLEN];
255 255
256 256 int libn;
257 257
258 258 if (name == NULL)
259 259 return (0); /* no rtld_db object name; keep going */
260 260
261 261 for (libn = 0; libn < sizeof (tdb_map) / sizeof (tdb_map[0]); libn++) {
262 262 if ((p = strstr(name, tdb_map[libn].tm_thr_lib)) != NULL)
263 263 break;
264 264 }
265 265
266 266 if (p == NULL)
267 267 return (0); /* no match; keep going */
268 268
269 269 path[0] = '\0';
270 270 (void) strlcat(path, mdb.m_root, sizeof (path));
271 271 (void) strlcat(path, tdb_map[libn].tm_db_dir, sizeof (path));
272 272 #if !defined(_ILP32)
273 273 (void) strlcat(path, "64/", sizeof (path));
274 274 #endif /* !_ILP32 */
275 275 (void) strlcat(path, tdb_map[libn].tm_db_name, sizeof (path));
276 276
277 277 /* Append the trailing library version number. */
278 278 (void) strlcat(path, strrchr(name, '.'), sizeof (path));
279 279
280 280 if ((ops = mdb_tdb_load(path)) == NULL) {
281 281 if (libn != 0 || errno != ENOENT)
282 282 warn("failed to load %s", path);
283 283 goto err;
284 284 }
285 285
286 286 if (ops == pt->p_tdb_ops)
287 287 return (1); /* no changes needed */
288 288
289 289 PTL_DTOR(t);
290 290 pt->p_tdb_ops = ops;
291 291 pt->p_ptl_ops = &proc_tdb_ops;
292 292 pt->p_ptl_hdl = NULL;
293 293
294 294 if (PTL_CTOR(t) == -1) {
295 295 warn("failed to initialize %s", path);
296 296 goto err;
297 297 }
298 298
299 299 mdb_dprintf(MDB_DBG_TGT, "loaded %s for debugging %s\n", path, name);
300 300 (void) mdb_tgt_status(t, &t->t_status);
301 301 return (1);
302 302 err:
303 303 PTL_DTOR(t);
304 304 pt->p_tdb_ops = NULL;
305 305 pt->p_ptl_ops = &proc_lwp_ops;
306 306 pt->p_ptl_hdl = NULL;
307 307
308 308 if (libn != 0 || errno != ENOENT) {
309 309 warn("warning: debugger will only be able to "
310 310 "examine raw LWPs\n");
311 311 }
312 312
313 313 (void) mdb_tgt_status(t, &t->t_status);
314 314 return (1);
315 315 }
316 316
317 317 /*
318 318 * Whenever the link map is consistent following an add or delete event, we ask
319 319 * libproc to update its mappings, check to see if we need to load libthread_db,
320 320 * and then update breakpoints which have been mapped or unmapped.
321 321 */
322 322 /*ARGSUSED*/
323 323 static void
324 324 pt_rtld_event(mdb_tgt_t *t, int vid, void *private)
325 325 {
326 326 struct ps_prochandle *P = t->t_pshandle;
327 327 pt_data_t *pt = t->t_data;
328 328 rd_event_msg_t rdm;
329 329 int docontinue = 1;
330 330
331 331 if (rd_event_getmsg(pt->p_rtld, &rdm) == RD_OK) {
332 332
333 333 mdb_dprintf(MDB_DBG_TGT, "rtld event type 0x%x state 0x%x\n",
334 334 rdm.type, rdm.u.state);
335 335
336 336 if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_CONSISTENT) {
337 337 mdb_sespec_t *sep, *nsep = mdb_list_next(&t->t_active);
338 338 pt_brkpt_t *ptb;
339 339
340 340 Pupdate_maps(P);
341 341
342 342 if (Pobject_iter(P, (proc_map_f *)thr_check, t) == 0 &&
343 343 pt->p_ptl_ops != &proc_lwp_ops) {
344 344 mdb_dprintf(MDB_DBG_TGT, "unloading thread_db "
345 345 "support after dlclose\n");
346 346 PTL_DTOR(t);
347 347 pt->p_tdb_ops = NULL;
348 348 pt->p_ptl_ops = &proc_lwp_ops;
349 349 pt->p_ptl_hdl = NULL;
350 350 (void) mdb_tgt_status(t, &t->t_status);
351 351 }
352 352
353 353 for (sep = nsep; sep != NULL; sep = nsep) {
354 354 nsep = mdb_list_next(sep);
355 355 ptb = sep->se_data;
356 356
357 357 if (sep->se_ops == &proc_brkpt_ops &&
358 358 Paddr_to_map(P, ptb->ptb_addr) == NULL)
359 359 mdb_tgt_sespec_idle_one(t, sep,
360 360 EMDB_NOMAP);
361 361 }
362 362
363 363 if (!mdb_tgt_sespec_activate_all(t) &&
364 364 (mdb.m_flags & MDB_FL_BPTNOSYMSTOP) &&
365 365 pt->p_rtld_finished) {
366 366 /*
367 367 * We weren't able to activate the breakpoints.
368 368 * If so requested, we'll return without
369 369 * calling continue, thus throwing the user into
370 370 * the debugger.
371 371 */
372 372 docontinue = 0;
373 373 }
374 374
375 375 if (pt->p_rdstate == PT_RD_ADD)
376 376 pt->p_rdstate = PT_RD_CONSIST;
377 377 }
378 378
379 379 if (rdm.type == RD_PREINIT)
380 380 (void) mdb_tgt_sespec_activate_all(t);
381 381
382 382 if (rdm.type == RD_POSTINIT) {
383 383 pt->p_rtld_finished = TRUE;
384 384 if (!mdb_tgt_sespec_activate_all(t) &&
385 385 (mdb.m_flags & MDB_FL_BPTNOSYMSTOP)) {
386 386 /*
387 387 * Now that rtld has been initialized, we
388 388 * should be able to initialize all deferred
389 389 * breakpoints. If we can't, don't let the
390 390 * target continue.
391 391 */
392 392 docontinue = 0;
393 393 }
394 394 }
395 395
396 396 if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_ADD &&
397 397 pt->p_rtld_finished)
398 398 pt->p_rdstate = MAX(pt->p_rdstate, PT_RD_ADD);
399 399 }
400 400
401 401 if (docontinue)
402 402 (void) mdb_tgt_continue(t, NULL);
403 403 }
404 404
405 405 static void
406 406 pt_post_attach(mdb_tgt_t *t)
407 407 {
408 408 struct ps_prochandle *P = t->t_pshandle;
409 409 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
410 410 pt_data_t *pt = t->t_data;
411 411 int hflag = MDB_TGT_SPEC_HIDDEN;
412 412
413 413 mdb_dprintf(MDB_DBG_TGT, "attach pr_flags=0x%x pr_why=%d pr_what=%d\n",
414 414 psp->pr_flags, psp->pr_why, psp->pr_what);
415 415
416 416 /*
417 417 * When we grab a process, the initial setting of p_rtld_finished
418 418 * should be false if the process was just created by exec; otherwise
419 419 * we permit unscoped references to resolve because we do not know how
420 420 * far the process has proceeded through linker initialization.
421 421 */
422 422 if ((psp->pr_flags & PR_ISTOP) && psp->pr_why == PR_SYSEXIT &&
423 423 psp->pr_errno == 0 && psp->pr_what == SYS_execve) {
424 424 if (mdb.m_target == NULL) {
425 425 warn("target performed exec of %s\n",
426 426 IOP_NAME(pt->p_fio));
427 427 }
428 428 pt->p_rtld_finished = FALSE;
429 429 } else
430 430 pt->p_rtld_finished = TRUE;
431 431
432 432 /*
433 433 * When we grab a process, if it is stopped by job control and part of
434 434 * the same session (i.e. same controlling tty), set MDB_FL_JOBCTL so
435 435 * we will know to bring it to the foreground when we continue it.
436 436 */
437 437 if (mdb.m_term != NULL && (psp->pr_flags & PR_STOPPED) &&
438 438 psp->pr_why == PR_JOBCONTROL && getsid(0) == Pstatus(P)->pr_sid)
439 439 mdb.m_flags |= MDB_FL_JOBCTL;
440 440
441 441 /*
442 442 * When we grab control of a live process, set F_RDWR so that the
443 443 * target layer permits writes to the target's address space.
444 444 */
445 445 t->t_flags |= MDB_TGT_F_RDWR;
446 446
447 447 (void) Pfault(P, FLTBPT, TRUE); /* always trace breakpoints */
448 448 (void) Pfault(P, FLTWATCH, TRUE); /* always trace watchpoints */
449 449 (void) Pfault(P, FLTTRACE, TRUE); /* always trace single-step */
450 450
451 451 (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */
452 452 (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */
453 453 (void) Psetflags(P, PR_FORK); /* inherit tracing on fork */
454 454
455 455 /*
456 456 * Install event specifiers to track fork and exec activities:
457 457 */
458 458 (void) mdb_tgt_add_sysexit(t, SYS_vfork, hflag, pt_fork, NULL);
459 459 (void) mdb_tgt_add_sysexit(t, SYS_forksys, hflag, pt_fork, NULL);
460 460 (void) mdb_tgt_add_sysexit(t, SYS_execve, hflag, pt_exec, NULL);
461 461
462 462 /*
463 463 * Attempt to instantiate the librtld_db agent and set breakpoints
464 464 * to track rtld activity. We will legitimately fail to instantiate
465 465 * the rtld_db agent if the target is statically linked.
466 466 */
467 467 if (pt->p_rtld == NULL && (pt->p_rtld = Prd_agent(P)) != NULL) {
468 468 rd_notify_t rdn;
469 469 rd_err_e err;
470 470
471 471 if ((err = rd_event_enable(pt->p_rtld, TRUE)) != RD_OK) {
472 472 warn("failed to enable rtld_db event tracing: %s\n",
473 473 rd_errstr(err));
474 474 goto out;
475 475 }
476 476
477 477 if ((err = rd_event_addr(pt->p_rtld, RD_PREINIT,
478 478 &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
479 479 (void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
480 480 hflag, pt_rtld_event, NULL);
481 481 } else {
482 482 warn("failed to install rtld_db preinit tracing: %s\n",
483 483 rd_errstr(err));
484 484 }
485 485
486 486 if ((err = rd_event_addr(pt->p_rtld, RD_POSTINIT,
487 487 &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
488 488 (void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
489 489 hflag, pt_rtld_event, NULL);
490 490 } else {
491 491 warn("failed to install rtld_db postinit tracing: %s\n",
492 492 rd_errstr(err));
493 493 }
494 494
495 495 if ((err = rd_event_addr(pt->p_rtld, RD_DLACTIVITY,
496 496 &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
497 497 (void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
498 498 hflag, pt_rtld_event, NULL);
499 499 } else {
500 500 warn("failed to install rtld_db activity tracing: %s\n",
501 501 rd_errstr(err));
502 502 }
503 503 }
504 504 out:
505 505 Pupdate_maps(P);
506 506 Psync(P);
507 507
508 508 /*
509 509 * If librtld_db failed to initialize due to an error or because we are
510 510 * debugging a statically linked executable, allow unscoped references.
511 511 */
512 512 if (pt->p_rtld == NULL)
513 513 pt->p_rtld_finished = TRUE;
514 514
515 515 (void) mdb_tgt_sespec_activate_all(t);
516 516 }
517 517
518 518 /*ARGSUSED*/
519 519 static int
520 520 pt_vespec_delete(mdb_tgt_t *t, void *private, int id, void *data)
521 521 {
522 522 if (id < 0) {
523 523 ASSERT(data == NULL); /* we don't use any ve_data */
524 524 (void) mdb_tgt_vespec_delete(t, id);
525 525 }
526 526 return (0);
527 527 }
528 528
529 529 static void
530 530 pt_pre_detach(mdb_tgt_t *t, int clear_matched)
531 531 {
532 532 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
533 533 pt_data_t *pt = t->t_data;
534 534 long cmd = 0;
535 535
536 536 /*
537 537 * If we are about to release the process and it is stopped on a traced
538 538 * SIGINT, breakpoint fault, single-step fault, or watchpoint, make
539 539 * sure to clear this event prior to releasing the process so that it
540 540 * does not subsequently reissue the fault and die from SIGTRAP.
541 541 */
542 542 if (psp->pr_flags & PR_ISTOP) {
543 543 if (psp->pr_why == PR_FAULTED && (psp->pr_what == FLTBPT ||
544 544 psp->pr_what == FLTTRACE || psp->pr_what == FLTWATCH))
545 545 cmd = PCCFAULT;
546 546 else if (psp->pr_why == PR_SIGNALLED && psp->pr_what == SIGINT)
547 547 cmd = PCCSIG;
548 548
549 549 if (cmd != 0)
550 550 (void) write(Pctlfd(t->t_pshandle), &cmd, sizeof (cmd));
551 551 }
552 552
553 553 if (Pstate(t->t_pshandle) == PS_UNDEAD)
554 554 (void) waitpid(Pstatus(t->t_pshandle)->pr_pid, NULL, WNOHANG);
555 555
556 556 (void) mdb_tgt_vespec_iter(t, pt_vespec_delete, NULL);
557 557 mdb_tgt_sespec_idle_all(t, EMDB_NOPROC, clear_matched);
558 558
559 559 if (pt->p_fio != pt->p_aout_fio) {
560 560 pt_close_aout(t);
561 561 (void) pt_open_aout(t, pt->p_aout_fio);
562 562 }
563 563
564 564 PTL_DTOR(t);
565 565 pt->p_tdb_ops = NULL;
566 566 pt->p_ptl_ops = &proc_lwp_ops;
567 567 pt->p_ptl_hdl = NULL;
568 568
569 569 pt->p_rtld = NULL;
570 570 pt->p_signal = 0;
571 571 pt->p_rtld_finished = FALSE;
572 572 pt->p_rdstate = PT_RD_NONE;
573 573 }
574 574
575 575 static void
576 576 pt_release_parents(mdb_tgt_t *t)
577 577 {
578 578 struct ps_prochandle *P = t->t_pshandle;
579 579 pt_data_t *pt = t->t_data;
580 580
581 581 mdb_sespec_t *sep;
582 582 pt_vforkp_t *vfp;
583 583
584 584 while ((vfp = mdb_list_next(&pt->p_vforkp)) != NULL) {
585 585 mdb_dprintf(MDB_DBG_TGT, "releasing vfork parent %d\n",
586 586 (int)Pstatus(vfp->p_pshandle)->pr_pid);
587 587
588 588 /*
589 589 * To release vfork parents, we must also wipe out any armed
590 590 * events in the parent by switching t_pshandle and calling
591 591 * se_disarm(). Do not change states or lose the matched list.
592 592 */
593 593 t->t_pshandle = vfp->p_pshandle;
594 594
595 595 for (sep = mdb_list_next(&t->t_active); sep != NULL;
596 596 sep = mdb_list_next(sep)) {
597 597 if (sep->se_state == MDB_TGT_SPEC_ARMED)
598 598 (void) sep->se_ops->se_disarm(t, sep);
599 599 }
600 600
601 601 t->t_pshandle = P;
602 602
603 603 Prelease(vfp->p_pshandle, PRELEASE_CLEAR);
604 604 mdb_list_delete(&pt->p_vforkp, vfp);
605 605 mdb_free(vfp, sizeof (pt_vforkp_t));
606 606 }
607 607 }
608 608
609 609 /*ARGSUSED*/
610 610 static void
611 611 pt_fork(mdb_tgt_t *t, int vid, void *private)
612 612 {
613 613 struct ps_prochandle *P = t->t_pshandle;
614 614 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
615 615 pt_data_t *pt = t->t_data;
616 616 mdb_sespec_t *sep;
617 617
618 618 int follow_parent = mdb.m_forkmode != MDB_FM_CHILD;
619 619 int is_vfork = (psp->pr_what == SYS_vfork ||
620 620 (psp->pr_what == SYS_forksys && psp->pr_sysarg[0] == 2));
621 621
622 622 struct ps_prochandle *C;
623 623 const lwpstatus_t *csp;
624 624 char sysname[32];
625 625 int gcode;
626 626 char c;
627 627
628 628 mdb_dprintf(MDB_DBG_TGT, "parent %s: errno=%d rv1=%ld rv2=%ld\n",
629 629 proc_sysname(psp->pr_what, sysname, sizeof (sysname)),
630 630 psp->pr_errno, psp->pr_rval1, psp->pr_rval2);
631 631
632 632 if (psp->pr_errno != 0) {
633 633 (void) mdb_tgt_continue(t, NULL);
634 634 return; /* fork failed */
635 635 }
636 636
637 637 /*
638 638 * If forkmode is ASK and stdout is a terminal, then ask the user to
639 639 * explicitly set the fork behavior for this particular fork.
640 640 */
641 641 if (mdb.m_forkmode == MDB_FM_ASK && mdb.m_term != NULL) {
642 642 mdb_iob_printf(mdb.m_err, "%s: %s detected: follow (p)arent "
643 643 "or (c)hild? ", mdb.m_pname, sysname);
644 644 mdb_iob_flush(mdb.m_err);
645 645
646 646 while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
647 647 if (c == 'P' || c == 'p') {
648 648 mdb_iob_printf(mdb.m_err, "%c\n", c);
649 649 follow_parent = TRUE;
650 650 break;
651 651 } else if (c == 'C' || c == 'c') {
652 652 mdb_iob_printf(mdb.m_err, "%c\n", c);
653 653 follow_parent = FALSE;
654 654 break;
655 655 }
656 656 }
657 657 }
658 658
659 659 /*
660 660 * The parent is now stopped on exit from its fork call. We must now
661 661 * grab the child on its return from fork in order to manipulate it.
662 662 */
663 663 if ((C = Pgrab(psp->pr_rval1, PGRAB_RETAIN, &gcode)) == NULL) {
664 664 warn("failed to grab forked child process %ld: %s\n",
665 665 psp->pr_rval1, Pgrab_error(gcode));
666 666 return; /* just stop if we failed to grab the child */
667 667 }
668 668
669 669 /*
670 670 * We may have grabbed the child and stopped it prematurely before it
671 671 * stopped on exit from fork. If so, wait up to 1 sec for it to settle.
672 672 */
673 673 if (Pstatus(C)->pr_lwp.pr_why != PR_SYSEXIT)
674 674 (void) Pwait(C, MILLISEC);
675 675
676 676 csp = &Pstatus(C)->pr_lwp;
677 677
678 678 if (csp->pr_why != PR_SYSEXIT ||
679 679 (csp->pr_what != SYS_vfork && csp->pr_what != SYS_forksys)) {
680 680 warn("forked child process %ld did not stop on exit from "
681 681 "fork as expected\n", psp->pr_rval1);
682 682 }
683 683
684 684 warn("target forked child process %ld (debugger following %s)\n",
685 685 psp->pr_rval1, follow_parent ? "parent" : "child");
686 686
687 687 (void) Punsetflags(C, PR_ASYNC); /* require synchronous mode */
688 688 (void) Psetflags(C, PR_BPTADJ); /* always adjust eip on x86 */
689 689 (void) Prd_agent(C); /* initialize librtld_db */
690 690
691 691 /*
692 692 * At the time pt_fork() is called, the target event engine has already
693 693 * disarmed the specifiers on the active list, clearing out events in
694 694 * the parent process. However, this means that events that change
695 695 * the address space (e.g. breakpoints) have not been effectively
696 696 * disarmed in the child since its address space reflects the state of
697 697 * the process at the time of fork when events were armed. We must
698 698 * therefore handle this as a special case and re-invoke the disarm
699 699 * callback of each active specifier to clean out the child process.
700 700 */
701 701 if (!is_vfork) {
702 702 for (t->t_pshandle = C, sep = mdb_list_next(&t->t_active);
703 703 sep != NULL; sep = mdb_list_next(sep)) {
704 704 if (sep->se_state == MDB_TGT_SPEC_ACTIVE)
705 705 (void) sep->se_ops->se_disarm(t, sep);
706 706 }
707 707
708 708 t->t_pshandle = P; /* restore pshandle to parent */
709 709 }
710 710
711 711 /*
712 712 * If we're following the parent process, we need to temporarily change
713 713 * t_pshandle to refer to the child handle C so that we can clear out
714 714 * all the events in the child prior to releasing it below. If we are
715 715 * tracing a vfork, we also need to explicitly wait for the child to
716 716 * exec, exit, or die before we can reset and continue the parent. We
717 717 * avoid having to deal with the vfork child forking again by clearing
718 718 * PR_FORK and setting PR_RLC; if it does fork it will effectively be
719 719 * released from our control and we will continue following the parent.
720 720 */
721 721 if (follow_parent) {
722 722 if (is_vfork) {
723 723 mdb_tgt_status_t status;
724 724
725 725 ASSERT(psp->pr_flags & PR_VFORKP);
726 726 mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
727 727 t->t_pshandle = C;
728 728
729 729 (void) Psysexit(C, SYS_execve, TRUE);
730 730
731 731 (void) Punsetflags(C, PR_FORK | PR_KLC);
732 732 (void) Psetflags(C, PR_RLC);
733 733
734 734 do {
735 735 if (pt_setrun(t, &status, 0) == -1 ||
736 736 status.st_state == MDB_TGT_UNDEAD ||
737 737 status.st_state == MDB_TGT_LOST)
738 738 break; /* failure or process died */
739 739
740 740 } while (csp->pr_why != PR_SYSEXIT ||
741 741 csp->pr_errno != 0 || csp->pr_what != SYS_execve);
742 742 } else
743 743 t->t_pshandle = C;
744 744 }
745 745
746 746 /*
747 747 * If we are following the child, destroy any active libthread_db
748 748 * handle before we release the parent process.
749 749 */
750 750 if (!follow_parent) {
751 751 PTL_DTOR(t);
752 752 pt->p_tdb_ops = NULL;
753 753 pt->p_ptl_ops = &proc_lwp_ops;
754 754 pt->p_ptl_hdl = NULL;
755 755 }
756 756
757 757 /*
758 758 * Idle all events to make sure the address space and tracing flags are
759 759 * restored, and then release the process we are not tracing. If we
760 760 * are following the child of a vfork, we push the parent's pshandle
761 761 * on to a list of vfork parents to be released when we exec or exit.
762 762 */
763 763 if (is_vfork && !follow_parent) {
764 764 pt_vforkp_t *vfp = mdb_alloc(sizeof (pt_vforkp_t), UM_SLEEP);
765 765
766 766 ASSERT(psp->pr_flags & PR_VFORKP);
767 767 vfp->p_pshandle = P;
768 768 mdb_list_append(&pt->p_vforkp, vfp);
769 769 mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
770 770
771 771 } else {
772 772 mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
773 773 Prelease(t->t_pshandle, PRELEASE_CLEAR);
774 774 if (!follow_parent)
775 775 pt_release_parents(t);
776 776 }
777 777
778 778 /*
779 779 * Now that all the hard stuff is done, switch t_pshandle back to the
780 780 * process we are following and reset our events to the ACTIVE state.
781 781 * If we are following the child, reset the libthread_db handle as well
782 782 * as the rtld agent.
783 783 */
784 784 if (follow_parent)
785 785 t->t_pshandle = P;
786 786 else {
787 787 t->t_pshandle = C;
788 788 pt->p_rtld = Prd_agent(C);
789 789 (void) Pobject_iter(t->t_pshandle, (proc_map_f *)thr_check, t);
790 790 }
791 791
792 792 (void) mdb_tgt_sespec_activate_all(t);
793 793 (void) mdb_tgt_continue(t, NULL);
794 794 }
795 795
796 796 /*ARGSUSED*/
797 797 static void
798 798 pt_exec(mdb_tgt_t *t, int vid, void *private)
799 799 {
800 800 struct ps_prochandle *P = t->t_pshandle;
801 801 const pstatus_t *psp = Pstatus(P);
802 802 pt_data_t *pt = t->t_data;
803 803 int follow_exec = mdb.m_execmode == MDB_EM_FOLLOW;
804 804 pid_t pid = psp->pr_pid;
805 805
806 806 char execname[MAXPATHLEN];
807 807 mdb_sespec_t *sep, *nsep;
808 808 mdb_io_t *io;
809 809 char c;
810 810
811 811 mdb_dprintf(MDB_DBG_TGT, "exit from %s: errno=%d\n", proc_sysname(
812 812 psp->pr_lwp.pr_what, execname, sizeof (execname)),
813 813 psp->pr_lwp.pr_errno);
814 814
815 815 if (psp->pr_lwp.pr_errno != 0) {
816 816 (void) mdb_tgt_continue(t, NULL);
817 817 return; /* exec failed */
818 818 }
819 819
820 820 /*
821 821 * If execmode is ASK and stdout is a terminal, then ask the user to
822 822 * explicitly set the exec behavior for this particular exec. If
823 823 * Pstate() still shows PS_LOST, we are being called from pt_setrun()
824 824 * directly and therefore we must resume the terminal since it is still
825 825 * in the suspended state as far as tgt_continue() is concerned.
826 826 */
827 827 if (mdb.m_execmode == MDB_EM_ASK && mdb.m_term != NULL) {
828 828 if (Pstate(P) == PS_LOST)
829 829 IOP_RESUME(mdb.m_term);
830 830
831 831 mdb_iob_printf(mdb.m_err, "%s: %s detected: (f)ollow new "
832 832 "program or (s)top? ", mdb.m_pname, execname);
833 833 mdb_iob_flush(mdb.m_err);
834 834
835 835 while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
836 836 if (c == 'F' || c == 'f') {
837 837 mdb_iob_printf(mdb.m_err, "%c\n", c);
838 838 follow_exec = TRUE;
839 839 break;
840 840 } else if (c == 'S' || c == 's') {
841 841 mdb_iob_printf(mdb.m_err, "%c\n", c);
842 842 follow_exec = FALSE;
843 843 break;
844 844 }
845 845 }
846 846
847 847 if (Pstate(P) == PS_LOST)
848 848 IOP_SUSPEND(mdb.m_term);
849 849 }
850 850
851 851 pt_release_parents(t); /* release any waiting vfork parents */
852 852 pt_pre_detach(t, FALSE); /* remove our breakpoints and idle events */
853 853 Preset_maps(P); /* libproc must delete mappings and symtabs */
854 854 pt_close_aout(t); /* free pt symbol tables and GElf file data */
855 855
856 856 /*
857 857 * If we lost control of the process across the exec and are not able
858 858 * to reopen it, we have no choice but to clear the matched event list
859 859 * and wait for the user to quit or otherwise release the process.
860 860 */
861 861 if (Pstate(P) == PS_LOST && Preopen(P) == -1) {
862 862 int error = errno;
863 863
864 864 warn("lost control of PID %d due to exec of %s executable\n",
865 865 (int)pid, error == EOVERFLOW ? "64-bit" : "set-id");
866 866
867 867 for (sep = t->t_matched; sep != T_SE_END; sep = nsep) {
868 868 nsep = sep->se_matched;
869 869 sep->se_matched = NULL;
870 870 mdb_tgt_sespec_rele(t, sep);
871 871 }
872 872
873 873 if (error != EOVERFLOW)
874 874 return; /* just stop if we exec'd a set-id executable */
875 875 }
876 876
877 877 if (Pstate(P) != PS_LOST) {
878 878 if (Pexecname(P, execname, sizeof (execname)) == NULL) {
879 879 (void) mdb_iob_snprintf(execname, sizeof (execname),
880 880 "/proc/%d/object/a.out", (int)pid);
881 881 }
882 882
883 883 if (follow_exec == FALSE || psp->pr_dmodel == PR_MODEL_NATIVE)
884 884 warn("target performed exec of %s\n", execname);
885 885
886 886 io = mdb_fdio_create_path(NULL, execname, pt->p_oflags, 0);
887 887 if (io == NULL) {
888 888 warn("failed to open %s", execname);
889 889 warn("a.out symbol tables will not be available\n");
890 890 } else if (pt_open_aout(t, io) == NULL) {
891 891 (void) mdb_dis_select(pt_disasm(NULL));
892 892 mdb_io_destroy(io);
893 893 } else
894 894 (void) mdb_dis_select(pt_disasm(&pt->p_file->gf_ehdr));
895 895 }
896 896
897 897 /*
898 898 * We reset our libthread_db state here, but deliberately do NOT call
899 899 * PTL_DTOR because we do not want to call libthread_db's td_ta_delete.
900 900 * This interface is hopelessly broken in that it writes to the process
901 901 * address space (which we do not want it to do after an exec) and it
902 902 * doesn't bother deallocating any of its storage anyway.
903 903 */
904 904 pt->p_tdb_ops = NULL;
905 905 pt->p_ptl_ops = &proc_lwp_ops;
906 906 pt->p_ptl_hdl = NULL;
907 907
908 908 if (follow_exec && psp->pr_dmodel != PR_MODEL_NATIVE) {
909 909 const char *argv[3];
910 910 char *state, *env;
911 911 char pidarg[16];
912 912 size_t envlen;
913 913
914 914 if (realpath(getexecname(), execname) == NULL) {
915 915 warn("cannot follow PID %d -- failed to resolve "
916 916 "debugger pathname for re-exec", (int)pid);
917 917 return;
918 918 }
919 919
920 920 warn("restarting debugger to follow PID %d ...\n", (int)pid);
921 921 mdb_dprintf(MDB_DBG_TGT, "re-exec'ing %s\n", execname);
922 922
923 923 (void) mdb_snprintf(pidarg, sizeof (pidarg), "-p%d", (int)pid);
924 924
925 925 state = mdb_get_config();
926 926 envlen = strlen(MDB_CONFIG_ENV_VAR) + 1 + strlen(state) + 1;
927 927 env = mdb_alloc(envlen, UM_SLEEP);
928 928 (void) snprintf(env, envlen,
929 929 "%s=%s", MDB_CONFIG_ENV_VAR, state);
930 930
931 931 (void) putenv(env);
932 932
933 933 argv[0] = mdb.m_pname;
934 934 argv[1] = pidarg;
935 935 argv[2] = NULL;
936 936
937 937 if (mdb.m_term != NULL)
938 938 IOP_SUSPEND(mdb.m_term);
939 939
940 940 Prelease(P, PRELEASE_CLEAR | PRELEASE_HANG);
941 941 (void) execv(execname, (char *const *)argv);
942 942 warn("failed to re-exec debugger");
943 943
944 944 if (mdb.m_term != NULL)
945 945 IOP_RESUME(mdb.m_term);
946 946
947 947 t->t_pshandle = pt->p_idlehandle;
948 948 return;
949 949 }
950 950
951 951 pt_post_attach(t); /* install tracing flags and activate events */
952 952 pt_activate_common(t); /* initialize librtld_db and libthread_db */
953 953
954 954 if (psp->pr_dmodel != PR_MODEL_NATIVE && mdb.m_term != NULL) {
955 955 warn("loadable dcmds will not operate on non-native %d-bit "
956 956 "data model\n", psp->pr_dmodel == PR_MODEL_ILP32 ? 32 : 64);
957 957 warn("use ::release -a and then run mdb -p %d to restart "
958 958 "debugger\n", (int)pid);
959 959 }
960 960
961 961 if (follow_exec)
962 962 (void) mdb_tgt_continue(t, NULL);
963 963 }
964 964
965 965 static int
966 966 pt_setflags(mdb_tgt_t *t, int flags)
967 967 {
968 968 pt_data_t *pt = t->t_data;
969 969
970 970 if ((flags ^ t->t_flags) & MDB_TGT_F_RDWR) {
971 971 int mode = (flags & MDB_TGT_F_RDWR) ? O_RDWR : O_RDONLY;
972 972 mdb_io_t *io;
973 973
974 974 if (pt->p_fio == NULL)
975 975 return (set_errno(EMDB_NOEXEC));
976 976
977 977 io = mdb_fdio_create_path(NULL, IOP_NAME(pt->p_fio), mode, 0);
978 978
979 979 if (io == NULL)
980 980 return (-1); /* errno is set for us */
981 981
982 982 t->t_flags = (t->t_flags & ~MDB_TGT_F_RDWR) |
983 983 (flags & MDB_TGT_F_RDWR);
984 984
985 985 pt->p_fio = mdb_io_hold(io);
986 986 mdb_io_rele(pt->p_file->gf_io);
987 987 pt->p_file->gf_io = pt->p_fio;
988 988 }
989 989
990 990 if (flags & MDB_TGT_F_FORCE) {
991 991 t->t_flags |= MDB_TGT_F_FORCE;
992 992 pt->p_gflags |= PGRAB_FORCE;
993 993 }
994 994
995 995 return (0);
996 996 }
997 997
998 998 /*ARGSUSED*/
999 999 static int
1000 1000 pt_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
1001 1001 const mdb_tgt_gregset_t *gregs)
1002 1002 {
1003 1003 argc = MIN(argc, (uint_t)(uintptr_t)arglim);
1004 1004 mdb_printf("%a(", pc);
1005 1005
1006 1006 if (argc != 0) {
1007 1007 mdb_printf("%lr", *argv++);
1008 1008 for (argc--; argc != 0; argc--)
1009 1009 mdb_printf(", %lr", *argv++);
1010 1010 }
1011 1011
1012 1012 mdb_printf(")\n");
1013 1013 return (0);
1014 1014 }
1015 1015
1016 1016 static int
1017 1017 pt_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
1018 1018 const mdb_tgt_gregset_t *gregs)
1019 1019 {
1020 1020 argc = MIN(argc, (uint_t)(uintptr_t)arglim);
1021 1021 #if defined(__i386) || defined(__amd64)
1022 1022 mdb_printf("%0?lr %a(", gregs->gregs[R_FP], pc);
1023 1023 #else
1024 1024 mdb_printf("%0?lr %a(", gregs->gregs[R_SP], pc);
1025 1025 #endif
1026 1026 if (argc != 0) {
1027 1027 mdb_printf("%lr", *argv++);
1028 1028 for (argc--; argc != 0; argc--)
1029 1029 mdb_printf(", %lr", *argv++);
1030 1030 }
1031 1031
1032 1032 mdb_printf(")\n");
1033 1033 return (0);
1034 1034 }
1035 1035
1036 1036 static int
1037 1037 pt_framer(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
1038 1038 const mdb_tgt_gregset_t *gregs)
1039 1039 {
1040 1040 if (pt_frameregs(arglim, pc, argc, argv, gregs, pc == PC_FAKE) == -1) {
1041 1041 /*
1042 1042 * Use verbose format if register format is not supported.
1043 1043 */
1044 1044 return (pt_framev(arglim, pc, argc, argv, gregs));
1045 1045 }
1046 1046
1047 1047 return (0);
1048 1048 }
1049 1049
1050 1050 /*ARGSUSED*/
1051 1051 static int
1052 1052 pt_stack_common(uintptr_t addr, uint_t flags, int argc,
1053 1053 const mdb_arg_t *argv, mdb_tgt_stack_f *func, prgreg_t saved_pc)
1054 1054 {
1055 1055 void *arg = (void *)(uintptr_t)mdb.m_nargs;
1056 1056 mdb_tgt_t *t = mdb.m_target;
1057 1057 mdb_tgt_gregset_t gregs;
1058 1058
1059 1059 if (argc != 0) {
1060 1060 if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
1061 1061 return (DCMD_USAGE);
1062 1062
1063 1063 if (argv->a_type == MDB_TYPE_STRING)
1064 1064 arg = (void *)(uintptr_t)mdb_strtoull(argv->a_un.a_str);
1065 1065 else
1066 1066 arg = (void *)(uintptr_t)argv->a_un.a_val;
1067 1067 }
1068 1068
1069 1069 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
1070 1070 mdb_warn("no process active\n");
1071 1071 return (DCMD_ERR);
1072 1072 }
1073 1073
1074 1074 /*
1075 1075 * In the universe of sparcv7, sparcv9, ia32, and amd64 this code can be
1076 1076 * common: <sys/procfs_isa.h> conveniently #defines R_FP to be the
1077 1077 * appropriate register we need to set in order to perform a stack
1078 1078 * traceback from a given frame address.
1079 1079 */
1080 1080 if (flags & DCMD_ADDRSPEC) {
1081 1081 bzero(&gregs, sizeof (gregs));
1082 1082 gregs.gregs[R_FP] = addr;
1083 1083 #ifdef __sparc
1084 1084 gregs.gregs[R_I7] = saved_pc;
1085 1085 #endif /* __sparc */
1086 1086 } else if (PTL_GETREGS(t, PTL_TID(t), gregs.gregs) != 0) {
1087 1087 mdb_warn("failed to get current register set");
1088 1088 return (DCMD_ERR);
1089 1089 }
1090 1090
1091 1091 (void) mdb_tgt_stack_iter(t, &gregs, func, arg);
1092 1092 return (DCMD_OK);
1093 1093 }
1094 1094
1095 1095 static int
1096 1096 pt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1097 1097 {
1098 1098 return (pt_stack_common(addr, flags, argc, argv, pt_frame, 0));
1099 1099 }
1100 1100
1101 1101 static int
1102 1102 pt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1103 1103 {
1104 1104 return (pt_stack_common(addr, flags, argc, argv, pt_framev, 0));
1105 1105 }
1106 1106
1107 1107 static int
1108 1108 pt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1109 1109 {
1110 1110 /*
1111 1111 * Force printing of first register window, by setting the
1112 1112 * saved pc (%i7) to PC_FAKE.
1113 1113 */
1114 1114 return (pt_stack_common(addr, flags, argc, argv, pt_framer, PC_FAKE));
1115 1115 }
1116 1116
1117 1117 /*ARGSUSED*/
1118 1118 static int
1119 1119 pt_ignored(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1120 1120 {
1121 1121 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1122 1122 char buf[PRSIGBUFSZ];
1123 1123
1124 1124 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1125 1125 return (DCMD_USAGE);
1126 1126
1127 1127 if (P == NULL) {
1128 1128 mdb_warn("no process is currently active\n");
1129 1129 return (DCMD_ERR);
1130 1130 }
1131 1131
1132 1132 mdb_printf("%s\n", proc_sigset2str(&Pstatus(P)->pr_sigtrace, " ",
1133 1133 FALSE, buf, sizeof (buf)));
1134 1134
1135 1135 return (DCMD_OK);
1136 1136 }
1137 1137
1138 1138 /*ARGSUSED*/
1139 1139 static int
1140 1140 pt_lwpid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1141 1141 {
1142 1142 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1143 1143
1144 1144 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1145 1145 return (DCMD_USAGE);
1146 1146
1147 1147 if (P == NULL) {
1148 1148 mdb_warn("no process is currently active\n");
1149 1149 return (DCMD_ERR);
1150 1150 }
1151 1151
1152 1152 mdb_printf("%d\n", Pstatus(P)->pr_lwp.pr_lwpid);
1153 1153 return (DCMD_OK);
1154 1154 }
1155 1155
1156 1156 static int
1157 1157 pt_print_lwpid(int *n, const lwpstatus_t *psp)
1158 1158 {
1159 1159 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1160 1160 int nlwp = Pstatus(P)->pr_nlwp;
1161 1161
1162 1162 if (*n == nlwp - 2)
1163 1163 mdb_printf("%d and ", (int)psp->pr_lwpid);
1164 1164 else if (*n == nlwp - 1)
1165 1165 mdb_printf("%d are", (int)psp->pr_lwpid);
1166 1166 else
1167 1167 mdb_printf("%d, ", (int)psp->pr_lwpid);
1168 1168
1169 1169 (*n)++;
1170 1170 return (0);
1171 1171 }
1172 1172
1173 1173 /*ARGSUSED*/
1174 1174 static int
1175 1175 pt_lwpids(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1176 1176 {
1177 1177 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1178 1178 int n = 0;
1179 1179
1180 1180 if (P == NULL) {
1181 1181 mdb_warn("no process is currently active\n");
1182 1182 return (DCMD_ERR);
1183 1183 }
1184 1184
1185 1185 switch (Pstatus(P)->pr_nlwp) {
1186 1186 case 0:
1187 1187 mdb_printf("no lwps are");
1188 1188 break;
1189 1189 case 1:
1190 1190 mdb_printf("lwpid %d is the only lwp",
1191 1191 Pstatus(P)->pr_lwp.pr_lwpid);
1192 1192 break;
1193 1193 default:
1194 1194 mdb_printf("lwpids ");
1195 1195 (void) Plwp_iter(P, (proc_lwp_f *)pt_print_lwpid, &n);
1196 1196 }
1197 1197
1198 1198 switch (Pstate(P)) {
1199 1199 case PS_DEAD:
1200 1200 mdb_printf(" in core of process %d.\n", Pstatus(P)->pr_pid);
1201 1201 break;
1202 1202 case PS_IDLE:
1203 1203 mdb_printf(" in idle target.\n");
1204 1204 break;
1205 1205 default:
1206 1206 mdb_printf(" in process %d.\n", (int)Pstatus(P)->pr_pid);
1207 1207 break;
1208 1208 }
1209 1209
1210 1210 return (DCMD_OK);
1211 1211 }
1212 1212
1213 1213 /*ARGSUSED*/
1214 1214 static int
1215 1215 pt_ignore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1216 1216 {
1217 1217 pt_data_t *pt = mdb.m_target->t_data;
1218 1218
1219 1219 if (!(flags & DCMD_ADDRSPEC) || argc != 0)
1220 1220 return (DCMD_USAGE);
1221 1221
1222 1222 if (addr < 1 || addr > pt->p_maxsig) {
1223 1223 mdb_warn("invalid signal number -- 0t%lu\n", addr);
1224 1224 return (DCMD_ERR);
1225 1225 }
1226 1226
1227 1227 (void) mdb_tgt_vespec_iter(mdb.m_target, pt_ignore_sig, (void *)addr);
1228 1228 return (DCMD_OK);
1229 1229 }
1230 1230
1231 1231 /*ARGSUSED*/
1232 1232 static int
1233 1233 pt_attach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1234 1234 {
1235 1235 mdb_tgt_t *t = mdb.m_target;
1236 1236 pt_data_t *pt = t->t_data;
1237 1237 int state, perr;
1238 1238
1239 1239 if (!(flags & DCMD_ADDRSPEC) && argc == 0)
1240 1240 return (DCMD_USAGE);
1241 1241
1242 1242 if (((flags & DCMD_ADDRSPEC) && argc != 0) || argc > 1 ||
1243 1243 (argc != 0 && argv->a_type != MDB_TYPE_STRING))
1244 1244 return (DCMD_USAGE);
1245 1245
1246 1246 if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
1247 1247 mdb_warn("debugger is already attached to a %s\n",
1248 1248 (Pstate(t->t_pshandle) == PS_DEAD) ? "core" : "process");
1249 1249 return (DCMD_ERR);
1250 1250 }
1251 1251
1252 1252 if (pt->p_fio == NULL) {
1253 1253 mdb_warn("attach requires executable to be specified on "
1254 1254 "command-line (or use -p)\n");
1255 1255 return (DCMD_ERR);
1256 1256 }
1257 1257
1258 1258 if (flags & DCMD_ADDRSPEC)
1259 1259 t->t_pshandle = Pgrab((pid_t)addr, pt->p_gflags, &perr);
1260 1260 else
1261 1261 t->t_pshandle = proc_arg_grab(argv->a_un.a_str,
1262 1262 PR_ARG_ANY, pt->p_gflags, &perr);
1263 1263
1264 1264 if (t->t_pshandle == NULL) {
1265 1265 t->t_pshandle = pt->p_idlehandle;
1266 1266 mdb_warn("cannot attach: %s\n", Pgrab_error(perr));
1267 1267 return (DCMD_ERR);
1268 1268 }
1269 1269
1270 1270 state = Pstate(t->t_pshandle);
1271 1271 if (state != PS_DEAD && state != PS_IDLE) {
1272 1272 (void) Punsetflags(t->t_pshandle, PR_KLC);
1273 1273 (void) Psetflags(t->t_pshandle, PR_RLC);
1274 1274 pt_post_attach(t);
1275 1275 pt_activate_common(t);
1276 1276 }
1277 1277
1278 1278 (void) mdb_tgt_status(t, &t->t_status);
1279 1279 mdb_module_load_all(0);
1280 1280 return (DCMD_OK);
1281 1281 }
1282 1282
1283 1283 static int
1284 1284 pt_regstatus(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1285 1285 {
1286 1286 mdb_tgt_t *t = mdb.m_target;
1287 1287
1288 1288 if (t->t_pshandle != NULL) {
1289 1289 const pstatus_t *psp = Pstatus(t->t_pshandle);
1290 1290 int cursig = psp->pr_lwp.pr_cursig;
1291 1291 char signame[SIG2STR_MAX];
1292 1292 int state = Pstate(t->t_pshandle);
1293 1293
1294 1294 if (state != PS_DEAD && state != PS_IDLE)
1295 1295 mdb_printf("process id = %d\n", psp->pr_pid);
1296 1296 else
1297 1297 mdb_printf("no process\n");
1298 1298
1299 1299 if (cursig != 0 && sig2str(cursig, signame) == 0)
1300 1300 mdb_printf("SIG%s: %s\n", signame, strsignal(cursig));
1301 1301 }
1302 1302
1303 1303 return (pt_regs(addr, flags, argc, argv));
1304 1304 }
1305 1305
1306 1306 static void
1307 1307 pt_thread_name(mdb_tgt_t *t, mdb_tgt_tid_t tid, char *buf, size_t bufsize)
1308 1308 {
1309 1309 char name[THREAD_NAME_MAX];
1310 1310
1311 1311 buf[0] = '\0';
1312 1312
1313 1313 if (t->t_pshandle == NULL ||
1314 1314 Plwp_getname(t->t_pshandle, tid, name, sizeof (name)) != 0 ||
1315 1315 name[0] == '\0') {
1316 1316 (void) mdb_snprintf(buf, bufsize, "%lu", tid);
1317 1317 return;
1318 1318 }
1319 1319
1320 1320 (void) mdb_snprintf(buf, bufsize, "%lu [%s]", tid, name);
1321 1321 }
1322 1322
1323 1323 static int
1324 1324 pt_findstack(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
1325 1325 {
1326 1326 mdb_tgt_t *t = mdb.m_target;
1327 1327 mdb_tgt_gregset_t gregs;
1328 1328 int showargs = 0;
1329 1329 int count;
1330 1330 uintptr_t pc, sp;
1331 1331 char name[128];
1332 1332
1333 1333 if (!(flags & DCMD_ADDRSPEC))
1334 1334 return (DCMD_USAGE);
1335 1335
1336 1336 count = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &showargs,
1337 1337 NULL);
1338 1338 argc -= count;
1339 1339 argv += count;
1340 1340
1341 1341 if (argc > 1 || (argc == 1 && argv->a_type != MDB_TYPE_STRING))
1342 1342 return (DCMD_USAGE);
1343 1343
1344 1344 if (PTL_GETREGS(t, tid, gregs.gregs) != 0) {
1345 1345 mdb_warn("failed to get register set for thread %p", tid);
1346 1346 return (DCMD_ERR);
1347 1347 }
1348 1348
1349 1349 pc = gregs.gregs[R_PC];
1350 1350 #if defined(__i386) || defined(__amd64)
1351 1351 sp = gregs.gregs[R_FP];
1352 1352 #else
1353 1353 sp = gregs.gregs[R_SP];
1354 1354 #endif
1355 1355
1356 1356 pt_thread_name(t, tid, name, sizeof (name));
1357 1357
1358 1358 mdb_printf("stack pointer for thread %s: %p\n", name, sp);
1359 1359 if (pc != 0)
1360 1360 mdb_printf("[ %0?lr %a() ]\n", sp, pc);
1361 1361
1362 1362 (void) mdb_inc_indent(2);
1363 1363 mdb_set_dot(sp);
1364 1364
1365 1365 if (argc == 1)
1366 1366 (void) mdb_eval(argv->a_un.a_str);
1367 1367 else if (showargs)
1368 1368 (void) mdb_eval("<.$C");
1369 1369 else
1370 1370 (void) mdb_eval("<.$C0");
1371 1371
1372 1372 (void) mdb_dec_indent(2);
1373 1373 return (DCMD_OK);
1374 1374 }
1375 1375
1376 1376 /*ARGSUSED*/
1377 1377 static int
1378 1378 pt_gcore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1379 1379 {
1380 1380 mdb_tgt_t *t = mdb.m_target;
1381 1381 char *prefix = "core";
1382 1382 char *content_str = NULL;
1383 1383 core_content_t content = CC_CONTENT_DEFAULT;
1384 1384 size_t size;
1385 1385 char *fname;
1386 1386 pid_t pid;
1387 1387
1388 1388 if (flags & DCMD_ADDRSPEC)
1389 1389 return (DCMD_USAGE);
1390 1390
1391 1391 if (mdb_getopts(argc, argv,
1392 1392 'o', MDB_OPT_STR, &prefix,
1393 1393 'c', MDB_OPT_STR, &content_str, NULL) != argc)
1394 1394 return (DCMD_USAGE);
1395 1395
1396 1396 if (content_str != NULL &&
1397 1397 (proc_str2content(content_str, &content) != 0 ||
1398 1398 content == CC_CONTENT_INVALID)) {
1399 1399 mdb_warn("invalid content string '%s'\n", content_str);
1400 1400 return (DCMD_ERR);
1401 1401 }
1402 1402
1403 1403 if (t->t_pshandle == NULL) {
1404 1404 mdb_warn("no process active\n");
1405 1405 return (DCMD_ERR);
1406 1406 }
1407 1407
1408 1408 pid = Pstatus(t->t_pshandle)->pr_pid;
1409 1409 size = 1 + mdb_snprintf(NULL, 0, "%s.%d", prefix, (int)pid);
1410 1410 fname = mdb_alloc(size, UM_SLEEP | UM_GC);
1411 1411 (void) mdb_snprintf(fname, size, "%s.%d", prefix, (int)pid);
1412 1412
1413 1413 if (Pgcore(t->t_pshandle, fname, content) != 0) {
1414 1414 /*
1415 1415 * Short writes during dumping are specifically described by
1416 1416 * EBADE, just as ZFS uses this otherwise-unused code for
1417 1417 * checksum errors. Translate to and mdb errno.
1418 1418 */
1419 1419 if (errno == EBADE)
1420 1420 (void) set_errno(EMDB_SHORTWRITE);
1421 1421 mdb_warn("couldn't dump core");
1422 1422 return (DCMD_ERR);
1423 1423 }
1424 1424
1425 1425 mdb_warn("%s dumped\n", fname);
1426 1426
1427 1427 return (DCMD_OK);
1428 1428 }
1429 1429
1430 1430 /*ARGSUSED*/
1431 1431 static int
1432 1432 pt_kill(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1433 1433 {
1434 1434 mdb_tgt_t *t = mdb.m_target;
1435 1435 pt_data_t *pt = t->t_data;
1436 1436 int state;
1437 1437
1438 1438 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1439 1439 return (DCMD_USAGE);
1440 1440
1441 1441 if (t->t_pshandle != NULL &&
1442 1442 (state = Pstate(t->t_pshandle)) != PS_DEAD && state != PS_IDLE) {
1443 1443 mdb_warn("victim process PID %d forcibly terminated\n",
1444 1444 (int)Pstatus(t->t_pshandle)->pr_pid);
1445 1445 pt_pre_detach(t, TRUE);
1446 1446 pt_release_parents(t);
1447 1447 Prelease(t->t_pshandle, PRELEASE_KILL);
1448 1448 t->t_pshandle = pt->p_idlehandle;
1449 1449 (void) mdb_tgt_status(t, &t->t_status);
1450 1450 mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
1451 1451 } else
1452 1452 mdb_warn("no victim process is currently under control\n");
1453 1453
1454 1454 return (DCMD_OK);
1455 1455 }
1456 1456
1457 1457 /*ARGSUSED*/
1458 1458 static int
1459 1459 pt_detach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1460 1460 {
1461 1461 mdb_tgt_t *t = mdb.m_target;
1462 1462 pt_data_t *pt = t->t_data;
1463 1463 int rflags = pt->p_rflags;
1464 1464
1465 1465 if (argc != 0 && argv->a_type == MDB_TYPE_STRING &&
1466 1466 strcmp(argv->a_un.a_str, "-a") == 0) {
1467 1467 rflags = PRELEASE_HANG | PRELEASE_CLEAR;
1468 1468 argv++;
1469 1469 argc--;
1470 1470 }
1471 1471
1472 1472 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1473 1473 return (DCMD_USAGE);
1474 1474
1475 1475 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
1476 1476 mdb_warn("debugger is not currently attached to a process "
1477 1477 "or core file\n");
1478 1478 return (DCMD_ERR);
1479 1479 }
1480 1480
1481 1481 pt_pre_detach(t, TRUE);
1482 1482 pt_release_parents(t);
1483 1483 Prelease(t->t_pshandle, rflags);
1484 1484 t->t_pshandle = pt->p_idlehandle;
1485 1485 (void) mdb_tgt_status(t, &t->t_status);
1486 1486 mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
1487 1487
1488 1488 return (DCMD_OK);
1489 1489 }
1490 1490
1491 1491 static uintmax_t
1492 1492 reg_disc_get(const mdb_var_t *v)
1493 1493 {
1494 1494 mdb_tgt_t *t = MDB_NV_COOKIE(v);
1495 1495 mdb_tgt_tid_t tid = PTL_TID(t);
1496 1496 mdb_tgt_reg_t r = 0;
1497 1497
1498 1498 if (tid != (mdb_tgt_tid_t)-1L)
1499 1499 (void) mdb_tgt_getareg(t, tid, mdb_nv_get_name(v), &r);
1500 1500
1501 1501 return (r);
1502 1502 }
1503 1503
1504 1504 static void
1505 1505 reg_disc_set(mdb_var_t *v, uintmax_t r)
1506 1506 {
1507 1507 mdb_tgt_t *t = MDB_NV_COOKIE(v);
1508 1508 mdb_tgt_tid_t tid = PTL_TID(t);
1509 1509
1510 1510 if (tid != (mdb_tgt_tid_t)-1L && mdb_tgt_putareg(t, tid,
1511 1511 mdb_nv_get_name(v), r) == -1)
1512 1512 mdb_warn("failed to modify %%%s register", mdb_nv_get_name(v));
1513 1513 }
1514 1514
1515 1515 static void
1516 1516 pt_print_reason(const lwpstatus_t *psp)
1517 1517 {
1518 1518 char name[SIG2STR_MAX + 4]; /* enough for SIG+name+\0, syscall or flt */
1519 1519 const char *desc;
1520 1520
1521 1521 switch (psp->pr_why) {
1522 1522 case PR_REQUESTED:
1523 1523 mdb_printf("stopped by debugger");
1524 1524 break;
1525 1525 case PR_SIGNALLED:
1526 1526 mdb_printf("stopped on %s (%s)", proc_signame(psp->pr_what,
1527 1527 name, sizeof (name)), strsignal(psp->pr_what));
1528 1528 break;
1529 1529 case PR_SYSENTRY:
1530 1530 mdb_printf("stopped on entry to %s system call",
1531 1531 proc_sysname(psp->pr_what, name, sizeof (name)));
1532 1532 break;
1533 1533 case PR_SYSEXIT:
1534 1534 mdb_printf("stopped on exit from %s system call",
1535 1535 proc_sysname(psp->pr_what, name, sizeof (name)));
1536 1536 break;
1537 1537 case PR_JOBCONTROL:
1538 1538 mdb_printf("stopped by job control");
1539 1539 break;
1540 1540 case PR_FAULTED:
1541 1541 if (psp->pr_what == FLTBPT) {
1542 1542 mdb_printf("stopped on a breakpoint");
1543 1543 } else if (psp->pr_what == FLTWATCH) {
1544 1544 switch (psp->pr_info.si_code) {
1545 1545 case TRAP_RWATCH:
1546 1546 desc = "read";
1547 1547 break;
1548 1548 case TRAP_WWATCH:
1549 1549 desc = "write";
1550 1550 break;
1551 1551 case TRAP_XWATCH:
1552 1552 desc = "execute";
1553 1553 break;
1554 1554 default:
1555 1555 desc = "unknown";
1556 1556 }
1557 1557 mdb_printf("stopped %s a watchpoint (%s access to %p)",
1558 1558 psp->pr_info.si_trapafter ? "after" : "on",
1559 1559 desc, psp->pr_info.si_addr);
1560 1560 } else if (psp->pr_what == FLTTRACE) {
1561 1561 mdb_printf("stopped after a single-step");
1562 1562 } else {
1563 1563 mdb_printf("stopped on a %s fault",
1564 1564 proc_fltname(psp->pr_what, name, sizeof (name)));
1565 1565 }
1566 1566 break;
1567 1567 case PR_SUSPENDED:
1568 1568 case PR_CHECKPOINT:
1569 1569 mdb_printf("suspended by the kernel");
1570 1570 break;
1571 1571 default:
1572 1572 mdb_printf("stopped for unknown reason (%d/%d)",
1573 1573 psp->pr_why, psp->pr_what);
1574 1574 }
1575 1575 }
1576 1576
1577 1577 /*ARGSUSED*/
1578 1578 static int
1579 1579 pt_status_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1580 1580 {
1581 1581 mdb_tgt_t *t = mdb.m_target;
1582 1582 struct ps_prochandle *P = t->t_pshandle;
1583 1583 pt_data_t *pt = t->t_data;
1584 1584
1585 1585 if (P != NULL) {
1586 1586 const psinfo_t *pip = Ppsinfo(P);
1587 1587 const pstatus_t *psp = Pstatus(P);
1588 1588 int cursig = 0, bits = 0, coredump = 0;
1589 1589 int state;
1590 1590 GElf_Sym sym;
1591 1591 uintptr_t panicstr;
1592 1592 char *panicbuf = mdb_alloc(PANIC_BUFSIZE, UM_SLEEP);
1593 1593 const siginfo_t *sip = &(psp->pr_lwp.pr_info);
1594 1594
1595 1595 char execname[MAXPATHLEN], buf[BUFSIZ];
1596 1596 char signame[SIG2STR_MAX + 4]; /* enough for SIG+name+\0 */
1597 1597
1598 1598 mdb_tgt_spec_desc_t desc;
1599 1599 mdb_sespec_t *sep;
1600 1600
1601 1601 struct utsname uts;
1602 1602 prcred_t cred;
1603 1603 psinfo_t pi;
1604 1604
1605 1605 (void) strcpy(uts.nodename, "unknown machine");
1606 1606 (void) Puname(P, &uts);
1607 1607
1608 1608 if (pip != NULL) {
1609 1609 bcopy(pip, &pi, sizeof (psinfo_t));
1610 1610 proc_unctrl_psinfo(&pi);
1611 1611 } else
1612 1612 bzero(&pi, sizeof (psinfo_t));
1613 1613
1614 1614 bits = pi.pr_dmodel == PR_MODEL_ILP32 ? 32 : 64;
1615 1615
1616 1616 state = Pstate(P);
1617 1617 if (psp != NULL && state != PS_UNDEAD && state != PS_IDLE)
1618 1618 cursig = psp->pr_lwp.pr_cursig;
1619 1619
1620 1620 if (state == PS_DEAD && pip != NULL) {
1621 1621 mdb_printf("debugging core file of %s (%d-bit) "
1622 1622 "from %s\n", pi.pr_fname, bits, uts.nodename);
1623 1623
1624 1624 } else if (state == PS_DEAD) {
1625 1625 mdb_printf("debugging core file\n");
1626 1626
1627 1627 } else if (state == PS_IDLE) {
1628 1628 const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
1629 1629
1630 1630 mdb_printf("debugging %s file (%d-bit)\n",
1631 1631 ehp->e_type == ET_EXEC ? "executable" : "object",
1632 1632 ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
1633 1633
1634 1634 } else if (state == PS_UNDEAD && pi.pr_pid == 0) {
1635 1635 mdb_printf("debugging defunct process\n");
1636 1636
1637 1637 } else {
1638 1638 mdb_printf("debugging PID %d (%d-bit)\n",
1639 1639 pi.pr_pid, bits);
1640 1640 }
1641 1641
1642 1642 if (Pexecname(P, execname, sizeof (execname)) != NULL)
1643 1643 mdb_printf("file: %s\n", execname);
1644 1644
1645 1645 if (pip != NULL && state == PS_DEAD)
1646 1646 mdb_printf("initial argv: %s\n", pi.pr_psargs);
1647 1647
1648 1648 if (state != PS_UNDEAD && state != PS_IDLE) {
1649 1649 mdb_printf("threading model: ");
1650 1650 if (pt->p_ptl_ops == &proc_lwp_ops)
1651 1651 mdb_printf("raw lwps\n");
1652 1652 else
1653 1653 mdb_printf("native threads\n");
1654 1654 }
1655 1655
1656 1656 mdb_printf("status: ");
1657 1657 switch (state) {
1658 1658 case PS_RUN:
1659 1659 ASSERT(!(psp->pr_flags & PR_STOPPED));
1660 1660 mdb_printf("process is running");
1661 1661 if (psp->pr_flags & PR_DSTOP)
1662 1662 mdb_printf(", debugger stop directive pending");
1663 1663 mdb_printf("\n");
1664 1664 break;
1665 1665
1666 1666 case PS_STOP:
1667 1667 ASSERT(psp->pr_flags & PR_STOPPED);
1668 1668 pt_print_reason(&psp->pr_lwp);
1669 1669
1670 1670 if (psp->pr_flags & PR_DSTOP)
1671 1671 mdb_printf(", debugger stop directive pending");
1672 1672 if (psp->pr_flags & PR_ASLEEP)
1673 1673 mdb_printf(", sleeping in %s system call",
1674 1674 proc_sysname(psp->pr_lwp.pr_syscall,
1675 1675 signame, sizeof (signame)));
1676 1676
1677 1677 mdb_printf("\n");
1678 1678
1679 1679 for (sep = t->t_matched; sep != T_SE_END;
1680 1680 sep = sep->se_matched) {
1681 1681 mdb_printf("event: %s\n", sep->se_ops->se_info(
1682 1682 t, sep, mdb_list_next(&sep->se_velist),
1683 1683 &desc, buf, sizeof (buf)));
1684 1684 }
1685 1685 break;
1686 1686
1687 1687 case PS_LOST:
1688 1688 mdb_printf("debugger lost control of process\n");
1689 1689 break;
1690 1690
1691 1691 case PS_UNDEAD:
1692 1692 coredump = WIFSIGNALED(pi.pr_wstat) &&
1693 1693 WCOREDUMP(pi.pr_wstat);
1694 1694 /*FALLTHRU*/
1695 1695
1696 1696 case PS_DEAD:
1697 1697 if (cursig == 0 && WIFSIGNALED(pi.pr_wstat))
1698 1698 cursig = WTERMSIG(pi.pr_wstat);
1699 1699 /*
1700 1700 * We can only use pr_wstat == 0 as a test for gcore if
1701 1701 * an NT_PRCRED note is present; these features were
1702 1702 * added at the same time in Solaris 8.
1703 1703 */
1704 1704 if (pi.pr_wstat == 0 && Pstate(P) == PS_DEAD &&
1705 1705 Pcred(P, &cred, 1) == 0) {
1706 1706 mdb_printf("process core file generated "
1707 1707 "with gcore(1)\n");
1708 1708 } else if (cursig != 0) {
1709 1709 mdb_printf("process terminated by %s (%s)",
1710 1710 proc_signame(cursig, signame,
1711 1711 sizeof (signame)), strsignal(cursig));
1712 1712
1713 1713 if (sip->si_signo != 0 && SI_FROMUSER(sip) &&
1714 1714 sip->si_pid != 0) {
1715 1715 mdb_printf(", pid=%d uid=%u",
1716 1716 (int)sip->si_pid, sip->si_uid);
1717 1717 if (sip->si_code != 0) {
1718 1718 mdb_printf(" code=%d",
1719 1719 sip->si_code);
1720 1720 }
1721 1721 } else {
1722 1722 switch (sip->si_signo) {
1723 1723 case SIGILL:
1724 1724 case SIGTRAP:
1725 1725 case SIGFPE:
1726 1726 case SIGSEGV:
1727 1727 case SIGBUS:
1728 1728 case SIGEMT:
1729 1729 mdb_printf(", addr=%p",
1730 1730 sip->si_addr);
1731 1731 default:
1732 1732 break;
1733 1733 }
1734 1734 }
1735 1735
1736 1736 if (coredump)
1737 1737 mdb_printf(" - core file dumped");
1738 1738 mdb_printf("\n");
1739 1739 } else {
1740 1740 mdb_printf("process terminated with exit "
1741 1741 "status %d\n", WEXITSTATUS(pi.pr_wstat));
1742 1742 }
1743 1743
1744 1744 if (Plookup_by_name(t->t_pshandle, "libc.so",
1745 1745 "panicstr", &sym) == 0 &&
1746 1746 Pread(t->t_pshandle, &panicstr, sizeof (panicstr),
1747 1747 sym.st_value) == sizeof (panicstr) &&
1748 1748 Pread_string(t->t_pshandle, panicbuf,
1749 1749 PANIC_BUFSIZE, panicstr) > 0) {
1750 1750 mdb_printf("panic message: %s",
1751 1751 panicbuf);
1752 1752 }
1753 1753
1754 1754
1755 1755 break;
1756 1756
1757 1757 case PS_IDLE:
1758 1758 mdb_printf("idle\n");
1759 1759 break;
1760 1760
1761 1761 default:
1762 1762 mdb_printf("unknown libproc Pstate: %d\n", Pstate(P));
1763 1763 }
1764 1764 mdb_free(panicbuf, PANIC_BUFSIZE);
1765 1765
1766 1766 } else if (pt->p_file != NULL) {
1767 1767 const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
1768 1768
1769 1769 mdb_printf("debugging %s file (%d-bit)\n",
1770 1770 ehp->e_type == ET_EXEC ? "executable" : "object",
1771 1771 ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
1772 1772 mdb_printf("executable file: %s\n", IOP_NAME(pt->p_fio));
1773 1773 mdb_printf("status: idle\n");
1774 1774 }
1775 1775
1776 1776 return (DCMD_OK);
1777 1777 }
1778 1778
1779 1779 static int
1780 1780 pt_tls(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
1781 1781 {
1782 1782 const char *name;
1783 1783 const char *object;
1784 1784 GElf_Sym sym;
1785 1785 mdb_syminfo_t si;
1786 1786 mdb_tgt_t *t = mdb.m_target;
1787 1787
1788 1788 if (!(flags & DCMD_ADDRSPEC) || argc > 1)
1789 1789 return (DCMD_USAGE);
1790 1790
1791 1791 if (argc == 0) {
1792 1792 psaddr_t b;
1793 1793
1794 1794 if (tlsbase(t, tid, PR_LMID_EVERY, MDB_TGT_OBJ_EXEC, &b) != 0) {
1795 1795 mdb_warn("failed to lookup tlsbase for %r", tid);
1796 1796 return (DCMD_ERR);
1797 1797 }
1798 1798
1799 1799 mdb_printf("%lr\n", b);
1800 1800 mdb_set_dot(b);
1801 1801
1802 1802 return (DCMD_OK);
1803 1803 }
1804 1804
1805 1805 name = argv[0].a_un.a_str;
1806 1806 object = MDB_TGT_OBJ_EVERY;
1807 1807
1808 1808 if (pt_lookup_by_name_thr(t, object, name, &sym, &si, tid) != 0) {
1809 1809 mdb_warn("failed to lookup %s", name);
1810 1810 return (DCMD_ABORT); /* avoid repeated failure */
1811 1811 }
1812 1812
1813 1813 if (GELF_ST_TYPE(sym.st_info) != STT_TLS && DCMD_HDRSPEC(flags))
1814 1814 mdb_warn("%s does not refer to thread local storage\n", name);
1815 1815
1816 1816 mdb_printf("%llr\n", sym.st_value);
1817 1817 mdb_set_dot(sym.st_value);
1818 1818
1819 1819 return (DCMD_OK);
1820 1820 }
1821 1821
1822 1822 /*ARGSUSED*/
1823 1823 static int
1824 1824 pt_tmodel(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1825 1825 {
1826 1826 mdb_tgt_t *t = mdb.m_target;
1827 1827 pt_data_t *pt = t->t_data;
1828 1828 const pt_ptl_ops_t *ptl_ops;
1829 1829
1830 1830 if (argc != 1 || argv->a_type != MDB_TYPE_STRING)
1831 1831 return (DCMD_USAGE);
1832 1832
1833 1833 if (strcmp(argv->a_un.a_str, "thread") == 0)
1834 1834 ptl_ops = &proc_tdb_ops;
1835 1835 else if (strcmp(argv->a_un.a_str, "lwp") == 0)
1836 1836 ptl_ops = &proc_lwp_ops;
1837 1837 else
1838 1838 return (DCMD_USAGE);
1839 1839
1840 1840 if (t->t_pshandle != NULL && pt->p_ptl_ops != ptl_ops) {
1841 1841 PTL_DTOR(t);
1842 1842 pt->p_tdb_ops = NULL;
1843 1843 pt->p_ptl_ops = &proc_lwp_ops;
1844 1844 pt->p_ptl_hdl = NULL;
1845 1845
1846 1846 if (ptl_ops == &proc_tdb_ops) {
1847 1847 (void) Pobject_iter(t->t_pshandle, (proc_map_f *)
1848 1848 thr_check, t);
1849 1849 }
1850 1850 }
1851 1851
1852 1852 (void) mdb_tgt_status(t, &t->t_status);
1853 1853 return (DCMD_OK);
1854 1854 }
1855 1855
1856 1856 static const char *
1857 1857 env_match(const char *cmp, const char *nameval)
1858 1858 {
1859 1859 const char *loc;
1860 1860 size_t cmplen = strlen(cmp);
1861 1861
1862 1862 loc = strchr(nameval, '=');
1863 1863 if (loc != NULL && (loc - nameval) == cmplen &&
1864 1864 strncmp(nameval, cmp, cmplen) == 0) {
1865 1865 return (loc + 1);
1866 1866 }
1867 1867
1868 1868 return (NULL);
1869 1869 }
1870 1870
1871 1871 /*ARGSUSED*/
1872 1872 static int
1873 1873 print_env(void *data, struct ps_prochandle *P, uintptr_t addr,
1874 1874 const char *nameval)
1875 1875 {
1876 1876 const char *value;
1877 1877
1878 1878 if (nameval == NULL) {
1879 1879 mdb_printf("<0x%p>\n", addr);
1880 1880 } else {
1881 1881 if (data == NULL)
1882 1882 mdb_printf("%s\n", nameval);
1883 1883 else if ((value = env_match(data, nameval)) != NULL)
1884 1884 mdb_printf("%s\n", value);
1885 1885 }
1886 1886
1887 1887 return (0);
1888 1888 }
1889 1889
1890 1890 /*ARGSUSED*/
1891 1891 static int
1892 1892 pt_getenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1893 1893 {
1894 1894 mdb_tgt_t *t = mdb.m_target;
1895 1895 pt_data_t *pt = t->t_data;
1896 1896 int i;
1897 1897 uint_t opt_t = 0;
1898 1898 mdb_var_t *v;
1899 1899
1900 1900 i = mdb_getopts(argc, argv,
1901 1901 't', MDB_OPT_SETBITS, TRUE, &opt_t, NULL);
1902 1902
1903 1903 argc -= i;
1904 1904 argv += i;
1905 1905
1906 1906 if ((flags & DCMD_ADDRSPEC) || argc > 1)
1907 1907 return (DCMD_USAGE);
1908 1908
1909 1909 if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
1910 1910 return (DCMD_USAGE);
1911 1911
1912 1912 if (opt_t && t->t_pshandle == NULL) {
1913 1913 mdb_warn("no process active\n");
1914 1914 return (DCMD_ERR);
1915 1915 }
1916 1916
1917 1917 if (opt_t && (Pstate(t->t_pshandle) == PS_IDLE ||
1918 1918 Pstate(t->t_pshandle) == PS_UNDEAD)) {
1919 1919 mdb_warn("-t option requires target to be running\n");
1920 1920 return (DCMD_ERR);
1921 1921 }
1922 1922
1923 1923 if (opt_t != 0) {
1924 1924 if (Penv_iter(t->t_pshandle, print_env,
1925 1925 argc == 0 ? NULL : (void *)argv->a_un.a_str) != 0)
1926 1926 return (DCMD_ERR);
1927 1927 } else if (argc == 1) {
1928 1928 if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) == NULL)
1929 1929 return (DCMD_ERR);
1930 1930
1931 1931 ASSERT(strchr(mdb_nv_get_cookie(v), '=') != NULL);
1932 1932 mdb_printf("%s\n", strchr(mdb_nv_get_cookie(v), '=') + 1);
1933 1933 } else {
1934 1934
1935 1935 mdb_nv_rewind(&pt->p_env);
1936 1936 while ((v = mdb_nv_advance(&pt->p_env)) != NULL)
1937 1937 mdb_printf("%s\n", mdb_nv_get_cookie(v));
1938 1938 }
1939 1939
1940 1940 return (DCMD_OK);
1941 1941 }
1942 1942
1943 1943 /*
1944 1944 * Function to set a variable in the internal environment, which is used when
1945 1945 * creating new processes. Note that it is possible that 'nameval' can refer to
1946 1946 * read-only memory, if mdb calls putenv() on an existing value before calling
1947 1947 * this function. While we should avoid this situation, this function is
1948 1948 * designed to be robust in the face of such changes.
1949 1949 */
1950 1950 static void
1951 1951 pt_env_set(pt_data_t *pt, const char *nameval)
1952 1952 {
1953 1953 mdb_var_t *v;
1954 1954 char *equals, *val;
1955 1955 const char *name;
1956 1956 size_t len;
1957 1957
1958 1958 if ((equals = strchr(nameval, '=')) != NULL) {
1959 1959 val = strdup(nameval);
1960 1960 equals = val + (equals - nameval);
1961 1961 } else {
1962 1962 /*
1963 1963 * nameval doesn't contain an equals character. Convert this to
1964 1964 * be 'nameval='.
1965 1965 */
1966 1966 len = strlen(nameval);
1967 1967 val = mdb_alloc(len + 2, UM_SLEEP);
1968 1968 (void) mdb_snprintf(val, len + 2, "%s=", nameval);
1969 1969 equals = val + len;
1970 1970 }
1971 1971
1972 1972 /* temporary truncate the string for lookup/insert */
1973 1973 *equals = '\0';
1974 1974 v = mdb_nv_lookup(&pt->p_env, val);
1975 1975
1976 1976 if (v != NULL) {
1977 1977 char *old = mdb_nv_get_cookie(v);
1978 1978 mdb_free(old, strlen(old) + 1);
1979 1979 name = mdb_nv_get_name(v);
1980 1980 } else {
1981 1981 /*
1982 1982 * The environment is created using MDB_NV_EXTNAME, so we must
↓ open down ↓ |
1982 lines elided |
↑ open up ↑ |
1983 1983 * provide external storage for the variable names.
1984 1984 */
1985 1985 name = strdup(val);
1986 1986 }
1987 1987
1988 1988 *equals = '=';
1989 1989
1990 1990 (void) mdb_nv_insert(&pt->p_env, name, NULL, (uintptr_t)val,
1991 1991 MDB_NV_EXTNAME);
1992 1992
1993 - if (equals)
1994 - *equals = '=';
1993 + *equals = '=';
1995 1994 }
1996 1995
1997 1996 /*
1998 1997 * Clears the internal environment.
1999 1998 */
2000 1999 static void
2001 2000 pt_env_clear(pt_data_t *pt)
2002 2001 {
2003 2002 mdb_var_t *v;
2004 2003 char *val, *name;
2005 2004
2006 2005 mdb_nv_rewind(&pt->p_env);
2007 2006 while ((v = mdb_nv_advance(&pt->p_env)) != NULL) {
2008 2007
2009 2008 name = (char *)mdb_nv_get_name(v);
2010 2009 val = mdb_nv_get_cookie(v);
2011 2010
2012 2011 mdb_nv_remove(&pt->p_env, v);
2013 2012
2014 2013 mdb_free(name, strlen(name) + 1);
2015 2014 mdb_free(val, strlen(val) + 1);
2016 2015 }
2017 2016 }
2018 2017
2019 2018 /*ARGSUSED*/
2020 2019 static int
2021 2020 pt_setenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2022 2021 {
2023 2022 mdb_tgt_t *t = mdb.m_target;
2024 2023 pt_data_t *pt = t->t_data;
2025 2024 char *nameval;
2026 2025 size_t len;
2027 2026 int alloc;
2028 2027
2029 2028 if ((flags & DCMD_ADDRSPEC) || argc == 0 || argc > 2)
2030 2029 return (DCMD_USAGE);
2031 2030
2032 2031 if ((argc > 0 && argv[0].a_type != MDB_TYPE_STRING) ||
2033 2032 (argc > 1 && argv[1].a_type != MDB_TYPE_STRING))
2034 2033 return (DCMD_USAGE);
2035 2034
2036 2035 if (t->t_pshandle == NULL) {
2037 2036 mdb_warn("no process active\n");
2038 2037 return (DCMD_ERR);
2039 2038 }
2040 2039
2041 2040 /*
2042 2041 * If the process is in some sort of running state, warn the user that
2043 2042 * changes won't immediately take effect.
2044 2043 */
2045 2044 if (Pstate(t->t_pshandle) == PS_RUN ||
2046 2045 Pstate(t->t_pshandle) == PS_STOP) {
2047 2046 mdb_warn("warning: changes will not take effect until process"
2048 2047 " is restarted\n");
2049 2048 }
2050 2049
2051 2050 /*
2052 2051 * We allow two forms of operation. The first is the usual "name=value"
2053 2052 * parameter. We also allow the user to specify two arguments, where
2054 2053 * the first is the name of the variable, and the second is the value.
2055 2054 */
2056 2055 alloc = 0;
2057 2056 if (argc == 1) {
2058 2057 nameval = (char *)argv->a_un.a_str;
2059 2058 } else {
2060 2059 len = strlen(argv[0].a_un.a_str) +
2061 2060 strlen(argv[1].a_un.a_str) + 2;
2062 2061 nameval = mdb_alloc(len, UM_SLEEP);
2063 2062 (void) mdb_snprintf(nameval, len, "%s=%s", argv[0].a_un.a_str,
2064 2063 argv[1].a_un.a_str);
2065 2064 alloc = 1;
2066 2065 }
2067 2066
2068 2067 pt_env_set(pt, nameval);
2069 2068
2070 2069 if (alloc)
2071 2070 mdb_free(nameval, strlen(nameval) + 1);
2072 2071
2073 2072 return (DCMD_OK);
2074 2073 }
2075 2074
2076 2075 /*ARGSUSED*/
2077 2076 static int
2078 2077 pt_unsetenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2079 2078 {
2080 2079 mdb_tgt_t *t = mdb.m_target;
2081 2080 pt_data_t *pt = t->t_data;
2082 2081 mdb_var_t *v;
2083 2082 char *value, *name;
2084 2083
2085 2084 if ((flags & DCMD_ADDRSPEC) || argc > 1)
2086 2085 return (DCMD_USAGE);
2087 2086
2088 2087 if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
2089 2088 return (DCMD_USAGE);
2090 2089
2091 2090 if (t->t_pshandle == NULL) {
2092 2091 mdb_warn("no process active\n");
2093 2092 return (DCMD_ERR);
2094 2093 }
2095 2094
2096 2095 /*
2097 2096 * If the process is in some sort of running state, warn the user that
2098 2097 * changes won't immediately take effect.
2099 2098 */
2100 2099 if (Pstate(t->t_pshandle) == PS_RUN ||
2101 2100 Pstate(t->t_pshandle) == PS_STOP) {
2102 2101 mdb_warn("warning: changes will not take effect until process"
2103 2102 " is restarted\n");
2104 2103 }
2105 2104
2106 2105 if (argc == 0) {
2107 2106 pt_env_clear(pt);
2108 2107 } else {
2109 2108 if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) != NULL) {
2110 2109 name = (char *)mdb_nv_get_name(v);
2111 2110 value = mdb_nv_get_cookie(v);
2112 2111
2113 2112 mdb_nv_remove(&pt->p_env, v);
2114 2113
2115 2114 mdb_free(name, strlen(name) + 1);
2116 2115 mdb_free(value, strlen(value) + 1);
2117 2116 }
2118 2117 }
2119 2118
2120 2119 return (DCMD_OK);
2121 2120 }
2122 2121
2123 2122 void
2124 2123 getenv_help(void)
2125 2124 {
2126 2125 mdb_printf("-t show current process environment"
2127 2126 " instead of initial environment.\n");
2128 2127 }
2129 2128
2130 2129 static const mdb_dcmd_t pt_dcmds[] = {
2131 2130 { "$c", "?[cnt]", "print stack backtrace", pt_stack },
2132 2131 { "$C", "?[cnt]", "print stack backtrace", pt_stackv },
2133 2132 { "$i", NULL, "print signals that are ignored", pt_ignored },
2134 2133 { "$l", NULL, "print the representative thread's lwp id", pt_lwpid },
2135 2134 { "$L", NULL, "print list of the active lwp ids", pt_lwpids },
2136 2135 { "$r", "?[-u]", "print general-purpose registers", pt_regs },
2137 2136 { "$x", "?", "print floating point registers", pt_fpregs },
2138 2137 { "$X", "?", "print floating point registers", pt_fpregs },
2139 2138 { "$y", "?", "print floating point registers", pt_fpregs },
2140 2139 { "$Y", "?", "print floating point registers", pt_fpregs },
2141 2140 { "$?", "?", "print status and registers", pt_regstatus },
2142 2141 { ":A", "?[core|pid]", "attach to process or core file", pt_attach },
2143 2142 { ":i", ":", "ignore signal (delete all matching events)", pt_ignore },
2144 2143 { ":k", NULL, "forcibly kill and release target", pt_kill },
2145 2144 { ":R", "[-a]", "release the previously attached process", pt_detach },
2146 2145 { "attach", "?[core|pid]",
2147 2146 "attach to process or core file", pt_attach },
2148 2147 { "findstack", ":[-v]", "find user thread stack", pt_findstack },
2149 2148 { "gcore", "[-o prefix] [-c content]",
2150 2149 "produce a core file for the attached process", pt_gcore },
2151 2150 { "getenv", "[-t] [name]", "display an environment variable",
2152 2151 pt_getenv, getenv_help },
2153 2152 { "kill", NULL, "forcibly kill and release target", pt_kill },
2154 2153 { "release", "[-a]",
2155 2154 "release the previously attached process", pt_detach },
2156 2155 { "regs", "?[-u]", "print general-purpose registers", pt_regs },
2157 2156 { "fpregs", "?[-dqs]", "print floating point registers", pt_fpregs },
2158 2157 { "setenv", "name=value", "set an environment variable", pt_setenv },
2159 2158 { "stack", "?[cnt]", "print stack backtrace", pt_stack },
2160 2159 { "stackregs", "?", "print stack backtrace and registers", pt_stackr },
2161 2160 { "status", NULL, "print summary of current target", pt_status_dcmd },
2162 2161 { "tls", ":symbol",
2163 2162 "lookup TLS data in the context of a given thread", pt_tls },
2164 2163 { "tmodel", "{thread|lwp}", NULL, pt_tmodel },
2165 2164 { "unsetenv", "[name]", "clear an environment variable", pt_unsetenv },
2166 2165 { NULL }
2167 2166 };
2168 2167
2169 2168 static void
2170 2169 pt_thr_walk_fini(mdb_walk_state_t *wsp)
2171 2170 {
2172 2171 mdb_addrvec_destroy(wsp->walk_data);
2173 2172 mdb_free(wsp->walk_data, sizeof (mdb_addrvec_t));
2174 2173 }
2175 2174
2176 2175 static int
2177 2176 pt_thr_walk_init(mdb_walk_state_t *wsp)
2178 2177 {
2179 2178 wsp->walk_data = mdb_zalloc(sizeof (mdb_addrvec_t), UM_SLEEP);
2180 2179 mdb_addrvec_create(wsp->walk_data);
2181 2180
2182 2181 if (PTL_ITER(mdb.m_target, wsp->walk_data) == -1) {
2183 2182 mdb_warn("failed to iterate over threads");
2184 2183 pt_thr_walk_fini(wsp);
2185 2184 return (WALK_ERR);
2186 2185 }
2187 2186
2188 2187 return (WALK_NEXT);
2189 2188 }
2190 2189
2191 2190 static int
2192 2191 pt_thr_walk_step(mdb_walk_state_t *wsp)
2193 2192 {
2194 2193 if (mdb_addrvec_length(wsp->walk_data) != 0) {
2195 2194 return (wsp->walk_callback(mdb_addrvec_shift(wsp->walk_data),
2196 2195 NULL, wsp->walk_cbdata));
2197 2196 }
2198 2197 return (WALK_DONE);
2199 2198 }
2200 2199
2201 2200 static const mdb_walker_t pt_walkers[] = {
2202 2201 { "thread", "walk list of valid thread identifiers",
2203 2202 pt_thr_walk_init, pt_thr_walk_step, pt_thr_walk_fini },
2204 2203 { NULL }
2205 2204 };
2206 2205
2207 2206 static int
2208 2207 pt_agent_check(boolean_t *agent, const lwpstatus_t *psp)
2209 2208 {
2210 2209 if (psp->pr_flags & PR_AGENT)
2211 2210 *agent = B_TRUE;
2212 2211
2213 2212 return (0);
2214 2213 }
2215 2214
2216 2215 static void
2217 2216 pt_activate_common(mdb_tgt_t *t)
2218 2217 {
2219 2218 pt_data_t *pt = t->t_data;
2220 2219 boolean_t hasagent = B_FALSE;
2221 2220 GElf_Sym sym;
2222 2221
2223 2222 /*
2224 2223 * If we have a libproc handle and AT_BASE is set, the process or core
2225 2224 * is dynamically linked. We call Prd_agent() to force libproc to
2226 2225 * try to initialize librtld_db, and issue a warning if that fails.
2227 2226 */
2228 2227 if (t->t_pshandle != NULL && Pgetauxval(t->t_pshandle,
2229 2228 AT_BASE) != -1L && Prd_agent(t->t_pshandle) == NULL) {
2230 2229 mdb_warn("warning: librtld_db failed to initialize; shared "
2231 2230 "library information will not be available\n");
2232 2231 }
2233 2232
2234 2233 if (t->t_pshandle != NULL) {
2235 2234 (void) Plwp_iter(t->t_pshandle,
2236 2235 (proc_lwp_f *)pt_agent_check, &hasagent);
2237 2236 }
2238 2237
2239 2238 if (hasagent) {
2240 2239 mdb_warn("agent lwp detected; forcing "
2241 2240 "lwp thread model (use ::tmodel to change)\n");
2242 2241 } else if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
2243 2242 /*
2244 2243 * If we have a libproc handle and we do not have an agent LWP,
2245 2244 * look for the correct thread debugging library. (If we have
2246 2245 * an agent LWP, we leave the model as the raw LWP model to
2247 2246 * allow the agent LWP to be visible to the debugger.)
2248 2247 */
2249 2248 (void) Pobject_iter(t->t_pshandle, (proc_map_f *)thr_check, t);
2250 2249 }
2251 2250
2252 2251 /*
2253 2252 * If there's a global object named '_mdb_abort_info', assuming we're
2254 2253 * debugging mdb itself and load the developer support module.
2255 2254 */
2256 2255 if (mdb_gelf_symtab_lookup_by_name(pt->p_symtab, "_mdb_abort_info",
2257 2256 &sym, NULL) == 0 && GELF_ST_TYPE(sym.st_info) == STT_OBJECT) {
2258 2257 if (mdb_module_load("mdb_ds", MDB_MOD_SILENT) < 0)
2259 2258 mdb_warn("warning: failed to load developer support\n");
2260 2259 }
2261 2260
2262 2261 mdb_tgt_elf_export(pt->p_file);
2263 2262 }
2264 2263
2265 2264 static void
2266 2265 pt_activate(mdb_tgt_t *t)
2267 2266 {
2268 2267 static const mdb_nv_disc_t reg_disc = { reg_disc_set, reg_disc_get };
2269 2268
2270 2269 pt_data_t *pt = t->t_data;
2271 2270 struct utsname u1, u2;
2272 2271 mdb_var_t *v;
2273 2272 core_content_t content;
2274 2273
2275 2274 if (t->t_pshandle) {
2276 2275 mdb_prop_postmortem = (Pstate(t->t_pshandle) == PS_DEAD);
2277 2276 mdb_prop_kernel = FALSE;
2278 2277 } else
2279 2278 mdb_prop_kernel = mdb_prop_postmortem = FALSE;
2280 2279
2281 2280 mdb_prop_datamodel = MDB_TGT_MODEL_NATIVE;
2282 2281
2283 2282 /*
2284 2283 * If we're examining a core file that doesn't contain program text,
2285 2284 * and uname(2) doesn't match the NT_UTSNAME note recorded in the
2286 2285 * core file, issue a warning.
2287 2286 */
2288 2287 if (mdb_prop_postmortem == TRUE &&
2289 2288 ((content = Pcontent(t->t_pshandle)) == CC_CONTENT_INVALID ||
2290 2289 !(content & CC_CONTENT_TEXT)) &&
2291 2290 uname(&u1) >= 0 && Puname(t->t_pshandle, &u2) == 0 &&
2292 2291 (strcmp(u1.release, u2.release) != 0 ||
2293 2292 strcmp(u1.version, u2.version) != 0)) {
2294 2293 mdb_warn("warning: core file is from %s %s %s; shared text "
2295 2294 "mappings may not match installed libraries\n",
2296 2295 u2.sysname, u2.release, u2.version);
2297 2296 }
2298 2297
2299 2298 /*
2300 2299 * Perform the common initialization tasks -- these are shared with
2301 2300 * the pt_exec() and pt_run() subroutines.
2302 2301 */
2303 2302 pt_activate_common(t);
2304 2303
2305 2304 (void) mdb_tgt_register_dcmds(t, &pt_dcmds[0], MDB_MOD_FORCE);
2306 2305 (void) mdb_tgt_register_walkers(t, &pt_walkers[0], MDB_MOD_FORCE);
2307 2306
2308 2307 /*
2309 2308 * Iterate through our register description list and export
2310 2309 * each register as a named variable.
2311 2310 */
2312 2311 mdb_nv_rewind(&pt->p_regs);
2313 2312 while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
2314 2313 ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
2315 2314
2316 2315 if (!(rd_flags & MDB_TGT_R_EXPORT))
2317 2316 continue; /* Don't export register as a variable */
2318 2317
2319 2318 (void) mdb_nv_insert(&mdb.m_nv, mdb_nv_get_name(v), ®_disc,
2320 2319 (uintptr_t)t, MDB_NV_PERSIST);
2321 2320 }
2322 2321 }
2323 2322
2324 2323 static void
2325 2324 pt_deactivate(mdb_tgt_t *t)
2326 2325 {
2327 2326 pt_data_t *pt = t->t_data;
2328 2327 const mdb_dcmd_t *dcp;
2329 2328 const mdb_walker_t *wp;
2330 2329 mdb_var_t *v, *w;
2331 2330
2332 2331 mdb_nv_rewind(&pt->p_regs);
2333 2332 while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
2334 2333 ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
2335 2334
2336 2335 if (!(rd_flags & MDB_TGT_R_EXPORT))
2337 2336 continue; /* Didn't export register as a variable */
2338 2337
2339 2338 if (w = mdb_nv_lookup(&mdb.m_nv, mdb_nv_get_name(v))) {
2340 2339 w->v_flags &= ~MDB_NV_PERSIST;
2341 2340 mdb_nv_remove(&mdb.m_nv, w);
2342 2341 }
2343 2342 }
2344 2343
2345 2344 for (wp = &pt_walkers[0]; wp->walk_name != NULL; wp++) {
2346 2345 if (mdb_module_remove_walker(t->t_module, wp->walk_name) == -1)
2347 2346 warn("failed to remove walk %s", wp->walk_name);
2348 2347 }
2349 2348
2350 2349 for (dcp = &pt_dcmds[0]; dcp->dc_name != NULL; dcp++) {
2351 2350 if (mdb_module_remove_dcmd(t->t_module, dcp->dc_name) == -1)
2352 2351 warn("failed to remove dcmd %s", dcp->dc_name);
2353 2352 }
2354 2353
2355 2354 mdb_prop_postmortem = FALSE;
2356 2355 mdb_prop_kernel = FALSE;
2357 2356 mdb_prop_datamodel = MDB_TGT_MODEL_UNKNOWN;
2358 2357 }
2359 2358
2360 2359 static void
2361 2360 pt_periodic(mdb_tgt_t *t)
2362 2361 {
2363 2362 pt_data_t *pt = t->t_data;
2364 2363
2365 2364 if (pt->p_rdstate == PT_RD_CONSIST) {
2366 2365 if (t->t_pshandle != NULL && Pstate(t->t_pshandle) < PS_LOST &&
2367 2366 !(mdb.m_flags & MDB_FL_NOMODS)) {
2368 2367 mdb_printf("%s: You've got symbols!\n", mdb.m_pname);
2369 2368 mdb_module_load_all(0);
2370 2369 }
2371 2370 pt->p_rdstate = PT_RD_NONE;
2372 2371 }
2373 2372 }
2374 2373
2375 2374 static void
2376 2375 pt_destroy(mdb_tgt_t *t)
2377 2376 {
2378 2377 pt_data_t *pt = t->t_data;
2379 2378
2380 2379 if (pt->p_idlehandle != NULL && pt->p_idlehandle != t->t_pshandle)
2381 2380 Prelease(pt->p_idlehandle, 0);
2382 2381
2383 2382 if (t->t_pshandle != NULL) {
2384 2383 PTL_DTOR(t);
2385 2384 pt_release_parents(t);
2386 2385 pt_pre_detach(t, TRUE);
2387 2386 Prelease(t->t_pshandle, pt->p_rflags);
2388 2387 }
2389 2388
2390 2389 mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
2391 2390 pt_close_aout(t);
2392 2391
2393 2392 if (pt->p_aout_fio != NULL)
2394 2393 mdb_io_rele(pt->p_aout_fio);
2395 2394
2396 2395 pt_env_clear(pt);
2397 2396 mdb_nv_destroy(&pt->p_env);
2398 2397
2399 2398 mdb_nv_destroy(&pt->p_regs);
2400 2399 mdb_free(pt, sizeof (pt_data_t));
2401 2400 }
2402 2401
2403 2402 /*ARGSUSED*/
2404 2403 static const char *
2405 2404 pt_name(mdb_tgt_t *t)
2406 2405 {
2407 2406 return ("proc");
2408 2407 }
2409 2408
2410 2409 static const char *
2411 2410 pt_platform(mdb_tgt_t *t)
2412 2411 {
2413 2412 pt_data_t *pt = t->t_data;
2414 2413
2415 2414 if (t->t_pshandle != NULL &&
2416 2415 Pplatform(t->t_pshandle, pt->p_platform, MAXNAMELEN) != NULL)
2417 2416 return (pt->p_platform);
2418 2417
2419 2418 return (mdb_conf_platform());
2420 2419 }
2421 2420
2422 2421 static int
2423 2422 pt_uname(mdb_tgt_t *t, struct utsname *utsp)
2424 2423 {
2425 2424 if (t->t_pshandle != NULL)
2426 2425 return (Puname(t->t_pshandle, utsp));
2427 2426
2428 2427 return (uname(utsp) >= 0 ? 0 : -1);
2429 2428 }
2430 2429
2431 2430 static int
2432 2431 pt_dmodel(mdb_tgt_t *t)
2433 2432 {
2434 2433 if (t->t_pshandle == NULL)
2435 2434 return (MDB_TGT_MODEL_NATIVE);
2436 2435
2437 2436 switch (Pstatus(t->t_pshandle)->pr_dmodel) {
2438 2437 case PR_MODEL_ILP32:
2439 2438 return (MDB_TGT_MODEL_ILP32);
2440 2439 case PR_MODEL_LP64:
2441 2440 return (MDB_TGT_MODEL_LP64);
2442 2441 }
2443 2442
2444 2443 return (MDB_TGT_MODEL_UNKNOWN);
2445 2444 }
2446 2445
2447 2446 static ssize_t
2448 2447 pt_vread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
2449 2448 {
2450 2449 ssize_t n;
2451 2450
2452 2451 /*
2453 2452 * If no handle is open yet, reads from virtual addresses are
2454 2453 * allowed to succeed but return zero-filled memory.
2455 2454 */
2456 2455 if (t->t_pshandle == NULL) {
2457 2456 bzero(buf, nbytes);
2458 2457 return (nbytes);
2459 2458 }
2460 2459
2461 2460 if ((n = Pread(t->t_pshandle, buf, nbytes, addr)) <= 0)
2462 2461 return (set_errno(EMDB_NOMAP));
2463 2462
2464 2463 return (n);
2465 2464 }
2466 2465
2467 2466 static ssize_t
2468 2467 pt_vwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2469 2468 {
2470 2469 ssize_t n;
2471 2470
2472 2471 /*
2473 2472 * If no handle is open yet, writes to virtual addresses are
2474 2473 * allowed to succeed but do not actually modify anything.
2475 2474 */
2476 2475 if (t->t_pshandle == NULL)
2477 2476 return (nbytes);
2478 2477
2479 2478 n = Pwrite(t->t_pshandle, buf, nbytes, addr);
2480 2479
2481 2480 if (n == -1 && errno == EIO)
2482 2481 return (set_errno(EMDB_NOMAP));
2483 2482
2484 2483 return (n);
2485 2484 }
2486 2485
2487 2486 static ssize_t
2488 2487 pt_fread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
2489 2488 {
2490 2489 pt_data_t *pt = t->t_data;
2491 2490
2492 2491 if (pt->p_file != NULL) {
2493 2492 return (mdb_gelf_rw(pt->p_file, buf, nbytes, addr,
2494 2493 IOPF_READ(pt->p_fio), GIO_READ));
2495 2494 }
2496 2495
2497 2496 bzero(buf, nbytes);
2498 2497 return (nbytes);
2499 2498 }
2500 2499
2501 2500 static ssize_t
2502 2501 pt_fwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2503 2502 {
2504 2503 pt_data_t *pt = t->t_data;
2505 2504
2506 2505 if (pt->p_file != NULL) {
2507 2506 return (mdb_gelf_rw(pt->p_file, (void *)buf, nbytes, addr,
2508 2507 IOPF_WRITE(pt->p_fio), GIO_WRITE));
2509 2508 }
2510 2509
2511 2510 return (nbytes);
2512 2511 }
2513 2512
2514 2513 static const char *
2515 2514 pt_resolve_lmid(const char *object, Lmid_t *lmidp)
2516 2515 {
2517 2516 Lmid_t lmid = PR_LMID_EVERY;
2518 2517 const char *p;
2519 2518
2520 2519 if (object == MDB_TGT_OBJ_EVERY || object == MDB_TGT_OBJ_EXEC)
2521 2520 lmid = LM_ID_BASE; /* restrict scope to a.out's link map */
2522 2521 else if (object != MDB_TGT_OBJ_RTLD && strncmp(object, "LM", 2) == 0 &&
2523 2522 (p = strchr(object, '`')) != NULL) {
2524 2523 object += 2; /* skip past initial "LM" prefix */
2525 2524 lmid = strntoul(object, (size_t)(p - object), mdb.m_radix);
2526 2525 object = p + 1; /* skip past link map specifier */
2527 2526 }
2528 2527
2529 2528 *lmidp = lmid;
2530 2529 return (object);
2531 2530 }
2532 2531
2533 2532 static int
2534 2533 tlsbase(mdb_tgt_t *t, mdb_tgt_tid_t tid, Lmid_t lmid, const char *object,
2535 2534 psaddr_t *basep)
2536 2535 {
2537 2536 pt_data_t *pt = t->t_data;
2538 2537 const rd_loadobj_t *loadobjp;
2539 2538 td_thrhandle_t th;
2540 2539 td_err_e err;
2541 2540
2542 2541 if (object == MDB_TGT_OBJ_EVERY)
2543 2542 return (set_errno(EINVAL));
2544 2543
2545 2544 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE)
2546 2545 return (set_errno(EMDB_NOPROC));
2547 2546
2548 2547 if (pt->p_tdb_ops == NULL)
2549 2548 return (set_errno(EMDB_TDB));
2550 2549
2551 2550 err = pt->p_tdb_ops->td_ta_map_id2thr(pt->p_ptl_hdl, tid, &th);
2552 2551 if (err != TD_OK)
2553 2552 return (set_errno(tdb_to_errno(err)));
2554 2553
2555 2554 /*
2556 2555 * If this fails, rtld_db has failed to initialize properly.
2557 2556 */
2558 2557 if ((loadobjp = Plmid_to_loadobj(t->t_pshandle, lmid, object)) == NULL)
2559 2558 return (set_errno(EMDB_NORTLD));
2560 2559
2561 2560 /*
2562 2561 * This will fail if the TLS block has not been allocated for the
2563 2562 * object that contains the TLS symbol in question.
2564 2563 */
2565 2564 err = pt->p_tdb_ops->td_thr_tlsbase(&th, loadobjp->rl_tlsmodid, basep);
2566 2565 if (err != TD_OK)
2567 2566 return (set_errno(tdb_to_errno(err)));
2568 2567
2569 2568 return (0);
2570 2569 }
2571 2570
2572 2571 typedef struct {
2573 2572 mdb_tgt_t *pl_tgt;
2574 2573 const char *pl_name;
2575 2574 Lmid_t pl_lmid;
2576 2575 GElf_Sym *pl_symp;
2577 2576 mdb_syminfo_t *pl_sip;
2578 2577 mdb_tgt_tid_t pl_tid;
2579 2578 mdb_bool_t pl_found;
2580 2579 } pt_lookup_t;
2581 2580
2582 2581 /*ARGSUSED*/
2583 2582 static int
2584 2583 pt_lookup_cb(void *data, const prmap_t *pmp, const char *object)
2585 2584 {
2586 2585 pt_lookup_t *plp = data;
2587 2586 struct ps_prochandle *P = plp->pl_tgt->t_pshandle;
2588 2587 prsyminfo_t si;
2589 2588 GElf_Sym sym;
2590 2589
2591 2590 if (Pxlookup_by_name(P, plp->pl_lmid, object, plp->pl_name, &sym,
2592 2591 &si) != 0)
2593 2592 return (0);
2594 2593
2595 2594 /*
2596 2595 * If we encounter a match with SHN_UNDEF, keep looking for a
2597 2596 * better match. Return the first match with SHN_UNDEF set if no
2598 2597 * better match is found.
2599 2598 */
2600 2599 if (sym.st_shndx == SHN_UNDEF) {
2601 2600 if (!plp->pl_found) {
2602 2601 plp->pl_found = TRUE;
2603 2602 *plp->pl_symp = sym;
2604 2603 plp->pl_sip->sym_table = si.prs_table;
2605 2604 plp->pl_sip->sym_id = si.prs_id;
2606 2605 }
2607 2606
2608 2607 return (0);
2609 2608 }
2610 2609
2611 2610 /*
2612 2611 * Note that if the symbol's st_shndx is SHN_UNDEF we don't have the
2613 2612 * TLS offset anyway, so adding in the tlsbase would be worthless.
2614 2613 */
2615 2614 if (GELF_ST_TYPE(sym.st_info) == STT_TLS &&
2616 2615 plp->pl_tid != (mdb_tgt_tid_t)-1) {
2617 2616 psaddr_t base;
2618 2617
2619 2618 if (tlsbase(plp->pl_tgt, plp->pl_tid, plp->pl_lmid, object,
2620 2619 &base) != 0)
2621 2620 return (-1); /* errno is set for us */
2622 2621
2623 2622 sym.st_value += base;
2624 2623 }
2625 2624
2626 2625 plp->pl_found = TRUE;
2627 2626 *plp->pl_symp = sym;
2628 2627 plp->pl_sip->sym_table = si.prs_table;
2629 2628 plp->pl_sip->sym_id = si.prs_id;
2630 2629
2631 2630 return (1);
2632 2631 }
2633 2632
2634 2633 /*
2635 2634 * Lookup the symbol with a thread context so that we can adjust TLS symbols
2636 2635 * to get the values as they would appear in the context of the given thread.
2637 2636 */
2638 2637 static int
2639 2638 pt_lookup_by_name_thr(mdb_tgt_t *t, const char *object,
2640 2639 const char *name, GElf_Sym *symp, mdb_syminfo_t *sip, mdb_tgt_tid_t tid)
2641 2640 {
2642 2641 struct ps_prochandle *P = t->t_pshandle;
2643 2642 pt_data_t *pt = t->t_data;
2644 2643 Lmid_t lmid;
2645 2644 uint_t i;
2646 2645 const rd_loadobj_t *aout_lop;
2647 2646
2648 2647 object = pt_resolve_lmid(object, &lmid);
2649 2648
2650 2649 if (P != NULL) {
2651 2650 pt_lookup_t pl;
2652 2651
2653 2652 pl.pl_tgt = t;
2654 2653 pl.pl_name = name;
2655 2654 pl.pl_lmid = lmid;
2656 2655 pl.pl_symp = symp;
2657 2656 pl.pl_sip = sip;
2658 2657 pl.pl_tid = tid;
2659 2658 pl.pl_found = FALSE;
2660 2659
2661 2660 if (object == MDB_TGT_OBJ_EVERY) {
2662 2661 if (Pobject_iter_resolved(P, pt_lookup_cb, &pl) == -1)
2663 2662 return (-1); /* errno is set for us */
2664 2663 if ((!pl.pl_found) &&
2665 2664 (Pobject_iter(P, pt_lookup_cb, &pl) == -1))
2666 2665 return (-1); /* errno is set for us */
2667 2666 } else {
2668 2667 const prmap_t *pmp;
2669 2668
2670 2669 /*
2671 2670 * This can fail either due to an invalid lmid or
2672 2671 * an invalid object. To determine which is
2673 2672 * faulty, we test the lmid against known valid
2674 2673 * lmids and then see if using a wild-card lmid
2675 2674 * improves ths situation.
2676 2675 */
2677 2676 if ((pmp = Plmid_to_map(P, lmid, object)) == NULL) {
2678 2677 if (lmid != PR_LMID_EVERY &&
2679 2678 lmid != LM_ID_BASE &&
2680 2679 lmid != LM_ID_LDSO &&
2681 2680 Plmid_to_map(P, PR_LMID_EVERY, object)
2682 2681 != NULL)
2683 2682 return (set_errno(EMDB_NOLMID));
2684 2683 else
2685 2684 return (set_errno(EMDB_NOOBJ));
2686 2685 }
2687 2686
2688 2687 if (pt_lookup_cb(&pl, pmp, object) == -1)
2689 2688 return (-1); /* errno is set for us */
2690 2689 }
2691 2690
2692 2691 if (pl.pl_found)
2693 2692 return (0);
2694 2693 }
2695 2694
2696 2695 /*
2697 2696 * If libproc doesn't have the symbols for rtld, we're cooked --
2698 2697 * mdb doesn't have those symbols either.
2699 2698 */
2700 2699 if (object == MDB_TGT_OBJ_RTLD)
2701 2700 return (set_errno(EMDB_NOSYM));
2702 2701
2703 2702 if (object != MDB_TGT_OBJ_EXEC && object != MDB_TGT_OBJ_EVERY) {
2704 2703 int status = mdb_gelf_symtab_lookup_by_file(pt->p_symtab,
2705 2704 object, name, symp, &sip->sym_id);
2706 2705
2707 2706 if (status != 0) {
2708 2707 if (P != NULL &&
2709 2708 Plmid_to_map(P, PR_LMID_EVERY, object) != NULL)
2710 2709 return (set_errno(EMDB_NOSYM));
2711 2710 else
2712 2711 return (-1); /* errno set from lookup_by_file */
2713 2712 }
2714 2713
2715 2714 goto found;
2716 2715 }
2717 2716
2718 2717 if (mdb_gelf_symtab_lookup_by_name(pt->p_symtab, name, symp, &i) == 0) {
2719 2718 sip->sym_table = MDB_TGT_SYMTAB;
2720 2719 sip->sym_id = i;
2721 2720 goto local_found;
2722 2721 }
2723 2722
2724 2723 if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, name, symp, &i) == 0) {
2725 2724 sip->sym_table = MDB_TGT_DYNSYM;
2726 2725 sip->sym_id = i;
2727 2726 goto local_found;
2728 2727 }
2729 2728
2730 2729 return (set_errno(EMDB_NOSYM));
2731 2730
2732 2731 local_found:
2733 2732 if (pt->p_file != NULL &&
2734 2733 pt->p_file->gf_ehdr.e_type == ET_DYN &&
2735 2734 P != NULL &&
2736 2735 (aout_lop = Pname_to_loadobj(P, PR_OBJ_EXEC)) != NULL)
2737 2736 symp->st_value += aout_lop->rl_base;
2738 2737
2739 2738 found:
2740 2739 /*
2741 2740 * If the symbol has type TLS, libproc should have found the symbol
2742 2741 * if it exists and has been allocated.
2743 2742 */
2744 2743 if (GELF_ST_TYPE(symp->st_info) == STT_TLS)
2745 2744 return (set_errno(EMDB_TLS));
2746 2745
2747 2746 return (0);
2748 2747 }
2749 2748
2750 2749 static int
2751 2750 pt_lookup_by_name(mdb_tgt_t *t, const char *object,
2752 2751 const char *name, GElf_Sym *symp, mdb_syminfo_t *sip)
2753 2752 {
2754 2753 return (pt_lookup_by_name_thr(t, object, name, symp, sip, PTL_TID(t)));
2755 2754 }
2756 2755
2757 2756 static int
2758 2757 pt_lookup_by_addr(mdb_tgt_t *t, uintptr_t addr, uint_t flags,
2759 2758 char *buf, size_t nbytes, GElf_Sym *symp, mdb_syminfo_t *sip)
2760 2759 {
2761 2760 struct ps_prochandle *P = t->t_pshandle;
2762 2761 pt_data_t *pt = t->t_data;
2763 2762 rd_plt_info_t rpi = { 0 };
2764 2763
2765 2764 const char *pltsym;
2766 2765 int rv, match, i;
2767 2766
2768 2767 mdb_gelf_symtab_t *gsts[3]; /* mdb.m_prsym, .symtab, .dynsym */
2769 2768 int gstc = 0; /* number of valid gsts[] entries */
2770 2769
2771 2770 mdb_gelf_symtab_t *gst = NULL; /* set if 'sym' is from a gst */
2772 2771 const prmap_t *pmp = NULL; /* set if 'sym' is from libproc */
2773 2772 GElf_Sym sym; /* best symbol found so far if !exact */
2774 2773 prsyminfo_t si;
2775 2774
2776 2775 /*
2777 2776 * Fill in our array of symbol table pointers with the private symbol
2778 2777 * table, static symbol table, and dynamic symbol table if applicable.
2779 2778 * These are done in order of precedence so that if we match and
2780 2779 * MDB_TGT_SYM_EXACT is set, we need not look any further.
2781 2780 */
2782 2781 if (mdb.m_prsym != NULL)
2783 2782 gsts[gstc++] = mdb.m_prsym;
2784 2783 if (P == NULL && pt->p_symtab != NULL)
2785 2784 gsts[gstc++] = pt->p_symtab;
2786 2785 if (P == NULL && pt->p_dynsym != NULL)
2787 2786 gsts[gstc++] = pt->p_dynsym;
2788 2787
2789 2788 /*
2790 2789 * Loop through our array attempting to match the address. If we match
2791 2790 * and we're in exact mode, we're done. Otherwise save the symbol in
2792 2791 * the local sym variable if it is closer than our previous match.
2793 2792 * We explicitly watch for zero-valued symbols since DevPro insists
2794 2793 * on storing __fsr_init_value's value as the symbol value instead
2795 2794 * of storing it in a constant integer.
2796 2795 */
2797 2796 for (i = 0; i < gstc; i++) {
2798 2797 if (mdb_gelf_symtab_lookup_by_addr(gsts[i], addr, flags, buf,
2799 2798 nbytes, symp, &sip->sym_id) != 0 || symp->st_value == 0)
2800 2799 continue;
2801 2800
2802 2801 if (flags & MDB_TGT_SYM_EXACT) {
2803 2802 gst = gsts[i];
2804 2803 goto found;
2805 2804 }
2806 2805
2807 2806 if (gst == NULL || mdb_gelf_sym_closer(symp, &sym, addr)) {
2808 2807 gst = gsts[i];
2809 2808 sym = *symp;
2810 2809 }
2811 2810 }
2812 2811
2813 2812 /*
2814 2813 * If we have no libproc handle active, we're done: fail if gst is
2815 2814 * NULL; otherwise copy out our best symbol and skip to the end.
2816 2815 * We also skip to found if gst is the private symbol table: we
2817 2816 * want this to always take precedence over PLT re-vectoring.
2818 2817 */
2819 2818 if (P == NULL || (gst != NULL && gst == mdb.m_prsym)) {
2820 2819 if (gst == NULL)
2821 2820 return (set_errno(EMDB_NOSYMADDR));
2822 2821 *symp = sym;
2823 2822 goto found;
2824 2823 }
2825 2824
2826 2825 /*
2827 2826 * Check to see if the address is in a PLT: if it is, use librtld_db to
2828 2827 * attempt to resolve the PLT entry. If the entry is bound, reset addr
2829 2828 * to the bound address, add a special prefix to the caller's buf,
2830 2829 * forget our previous guess, and then continue using the new addr.
2831 2830 * If the entry is not bound, copy the corresponding symbol name into
2832 2831 * buf and return a fake symbol for the given address.
2833 2832 */
2834 2833 if ((pltsym = Ppltdest(P, addr)) != NULL) {
2835 2834 const rd_loadobj_t *rlp;
2836 2835 rd_agent_t *rap;
2837 2836
2838 2837 if ((rap = Prd_agent(P)) != NULL &&
2839 2838 (rlp = Paddr_to_loadobj(P, addr)) != NULL &&
2840 2839 rd_plt_resolution(rap, addr, Pstatus(P)->pr_lwp.pr_lwpid,
2841 2840 rlp->rl_plt_base, &rpi) == RD_OK &&
2842 2841 (rpi.pi_flags & RD_FLG_PI_PLTBOUND)) {
2843 2842 size_t n;
2844 2843 n = mdb_iob_snprintf(buf, nbytes, "PLT=");
2845 2844 addr = rpi.pi_baddr;
2846 2845 if (n > nbytes) {
2847 2846 buf += nbytes;
2848 2847 nbytes = 0;
2849 2848 } else {
2850 2849 buf += n;
2851 2850 nbytes -= n;
2852 2851 }
2853 2852 gst = NULL;
2854 2853 } else {
2855 2854 (void) mdb_iob_snprintf(buf, nbytes, "PLT:%s", pltsym);
2856 2855 bzero(symp, sizeof (GElf_Sym));
2857 2856 symp->st_value = addr;
2858 2857 symp->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
2859 2858 return (0);
2860 2859 }
2861 2860 }
2862 2861
2863 2862 /*
2864 2863 * Ask libproc to convert the address to the closest symbol for us.
2865 2864 * Once we get the closest symbol, we perform the EXACT match or
2866 2865 * smart-mode or absolute distance check ourself:
2867 2866 */
2868 2867 if (PT_LIBPROC_RESOLVE(P)) {
2869 2868 rv = Pxlookup_by_addr_resolved(P, addr, buf, nbytes,
2870 2869 symp, &si);
2871 2870 } else {
2872 2871 rv = Pxlookup_by_addr(P, addr, buf, nbytes,
2873 2872 symp, &si);
2874 2873 }
2875 2874 if ((rv == 0) && (symp->st_value != 0) &&
2876 2875 (gst == NULL || mdb_gelf_sym_closer(symp, &sym, addr))) {
2877 2876
2878 2877 if (flags & MDB_TGT_SYM_EXACT)
2879 2878 match = (addr == symp->st_value);
2880 2879 else if (mdb.m_symdist == 0)
2881 2880 match = (addr >= symp->st_value &&
2882 2881 addr < symp->st_value + symp->st_size);
2883 2882 else
2884 2883 match = (addr >= symp->st_value &&
2885 2884 addr < symp->st_value + mdb.m_symdist);
2886 2885
2887 2886 if (match) {
2888 2887 pmp = Paddr_to_map(P, addr);
2889 2888 gst = NULL;
2890 2889 sip->sym_table = si.prs_table;
2891 2890 sip->sym_id = si.prs_id;
2892 2891 goto found;
2893 2892 }
2894 2893 }
2895 2894
2896 2895 /*
2897 2896 * If we get here, Plookup_by_addr has failed us. If we have no
2898 2897 * previous best symbol (gst == NULL), we've failed completely.
2899 2898 * Otherwise we copy out that symbol and continue on to 'found'.
2900 2899 */
2901 2900 if (gst == NULL)
2902 2901 return (set_errno(EMDB_NOSYMADDR));
2903 2902 *symp = sym;
2904 2903 found:
2905 2904 /*
2906 2905 * Once we've found something, copy the final name into the caller's
2907 2906 * buffer and prefix it with the mapping name if appropriate.
2908 2907 */
2909 2908 if (pmp != NULL && pmp != Pname_to_map(P, PR_OBJ_EXEC)) {
2910 2909 const char *prefix = pmp->pr_mapname;
2911 2910 Lmid_t lmid;
2912 2911
2913 2912 if (PT_LIBPROC_RESOLVE(P)) {
2914 2913 if (Pobjname_resolved(P, addr, pt->p_objname,
2915 2914 MDB_TGT_MAPSZ))
2916 2915 prefix = pt->p_objname;
2917 2916 } else {
2918 2917 if (Pobjname(P, addr, pt->p_objname, MDB_TGT_MAPSZ))
2919 2918 prefix = pt->p_objname;
2920 2919 }
2921 2920
2922 2921 if (buf != NULL && nbytes > 1) {
2923 2922 (void) strncpy(pt->p_symname, buf, MDB_TGT_SYM_NAMLEN);
2924 2923 pt->p_symname[MDB_TGT_SYM_NAMLEN - 1] = '\0';
2925 2924 } else {
2926 2925 pt->p_symname[0] = '\0';
2927 2926 }
2928 2927
2929 2928 if (prefix == pt->p_objname && Plmid(P, addr, &lmid) == 0 && (
2930 2929 (lmid != LM_ID_BASE && lmid != LM_ID_LDSO) ||
2931 2930 (mdb.m_flags & MDB_FL_SHOWLMID))) {
2932 2931 (void) mdb_iob_snprintf(buf, nbytes, "LM%lr`%s`%s",
2933 2932 lmid, strbasename(prefix), pt->p_symname);
2934 2933 } else {
2935 2934 (void) mdb_iob_snprintf(buf, nbytes, "%s`%s",
2936 2935 strbasename(prefix), pt->p_symname);
2937 2936 }
2938 2937
2939 2938 } else if (gst != NULL && buf != NULL && nbytes > 0) {
2940 2939 (void) strncpy(buf, mdb_gelf_sym_name(gst, symp), nbytes);
2941 2940 buf[nbytes - 1] = '\0';
2942 2941 }
2943 2942
2944 2943 return (0);
2945 2944 }
2946 2945
2947 2946
2948 2947 static int
2949 2948 pt_symbol_iter_cb(void *arg, const GElf_Sym *sym, const char *name,
2950 2949 const prsyminfo_t *sip)
2951 2950 {
2952 2951 pt_symarg_t *psp = arg;
2953 2952
2954 2953 psp->psym_info.sym_id = sip->prs_id;
2955 2954
2956 2955 return (psp->psym_func(psp->psym_private, sym, name, &psp->psym_info,
2957 2956 psp->psym_obj));
2958 2957 }
2959 2958
2960 2959 static int
2961 2960 pt_objsym_iter(void *arg, const prmap_t *pmp, const char *object)
2962 2961 {
2963 2962 Lmid_t lmid = PR_LMID_EVERY;
2964 2963 pt_symarg_t *psp = arg;
2965 2964
2966 2965 psp->psym_obj = object;
2967 2966
2968 2967 (void) Plmid(psp->psym_targ->t_pshandle, pmp->pr_vaddr, &lmid);
2969 2968 (void) Pxsymbol_iter(psp->psym_targ->t_pshandle, lmid, object,
2970 2969 psp->psym_which, psp->psym_type, pt_symbol_iter_cb, arg);
2971 2970
2972 2971 return (0);
2973 2972 }
2974 2973
2975 2974 static int
2976 2975 pt_symbol_filt(void *arg, const GElf_Sym *sym, const char *name, uint_t id)
2977 2976 {
2978 2977 pt_symarg_t *psp = arg;
2979 2978
2980 2979 if (mdb_tgt_sym_match(sym, psp->psym_type)) {
2981 2980 psp->psym_info.sym_id = id;
2982 2981 return (psp->psym_func(psp->psym_private, sym, name,
2983 2982 &psp->psym_info, psp->psym_obj));
2984 2983 }
2985 2984
2986 2985 return (0);
2987 2986 }
2988 2987
2989 2988 static int
2990 2989 pt_symbol_iter(mdb_tgt_t *t, const char *object, uint_t which,
2991 2990 uint_t type, mdb_tgt_sym_f *func, void *private)
2992 2991 {
2993 2992 pt_data_t *pt = t->t_data;
2994 2993 mdb_gelf_symtab_t *gst;
2995 2994 pt_symarg_t ps;
2996 2995 Lmid_t lmid;
2997 2996
2998 2997 object = pt_resolve_lmid(object, &lmid);
2999 2998
3000 2999 ps.psym_targ = t;
3001 3000 ps.psym_which = which;
3002 3001 ps.psym_type = type;
3003 3002 ps.psym_func = func;
3004 3003 ps.psym_private = private;
3005 3004 ps.psym_obj = object;
3006 3005
3007 3006 if (t->t_pshandle != NULL) {
3008 3007 if (object != MDB_TGT_OBJ_EVERY) {
3009 3008 if (Plmid_to_map(t->t_pshandle, lmid, object) == NULL)
3010 3009 return (set_errno(EMDB_NOOBJ));
3011 3010 (void) Pxsymbol_iter(t->t_pshandle, lmid, object,
3012 3011 which, type, pt_symbol_iter_cb, &ps);
3013 3012 return (0);
3014 3013 } else if (Prd_agent(t->t_pshandle) != NULL) {
3015 3014 if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3016 3015 (void) Pobject_iter_resolved(t->t_pshandle,
3017 3016 pt_objsym_iter, &ps);
3018 3017 } else {
3019 3018 (void) Pobject_iter(t->t_pshandle,
3020 3019 pt_objsym_iter, &ps);
3021 3020 }
3022 3021 return (0);
3023 3022 }
3024 3023 }
3025 3024
3026 3025 if (lmid != LM_ID_BASE && lmid != PR_LMID_EVERY)
3027 3026 return (set_errno(EMDB_NOLMID));
3028 3027
3029 3028 if (object != MDB_TGT_OBJ_EXEC && object != MDB_TGT_OBJ_EVERY &&
3030 3029 pt->p_fio != NULL &&
3031 3030 strcmp(object, IOP_NAME(pt->p_fio)) != 0)
3032 3031 return (set_errno(EMDB_NOOBJ));
3033 3032
3034 3033 if (which == MDB_TGT_SYMTAB)
3035 3034 gst = pt->p_symtab;
3036 3035 else
3037 3036 gst = pt->p_dynsym;
3038 3037
3039 3038 if (gst != NULL) {
3040 3039 ps.psym_info.sym_table = gst->gst_tabid;
3041 3040 mdb_gelf_symtab_iter(gst, pt_symbol_filt, &ps);
3042 3041 }
3043 3042
3044 3043 return (0);
3045 3044 }
3046 3045
3047 3046 static const mdb_map_t *
3048 3047 pt_prmap_to_mdbmap(mdb_tgt_t *t, const prmap_t *prp, mdb_map_t *mp)
3049 3048 {
3050 3049 struct ps_prochandle *P = t->t_pshandle;
3051 3050 char *rv, name[MAXPATHLEN];
3052 3051 Lmid_t lmid;
3053 3052
3054 3053 if (PT_LIBPROC_RESOLVE(P)) {
3055 3054 rv = Pobjname_resolved(P, prp->pr_vaddr, name, sizeof (name));
3056 3055 } else {
3057 3056 rv = Pobjname(P, prp->pr_vaddr, name, sizeof (name));
3058 3057 }
3059 3058
3060 3059 if (rv != NULL) {
3061 3060 if (Plmid(P, prp->pr_vaddr, &lmid) == 0 && (
3062 3061 (lmid != LM_ID_BASE && lmid != LM_ID_LDSO) ||
3063 3062 (mdb.m_flags & MDB_FL_SHOWLMID))) {
3064 3063 (void) mdb_iob_snprintf(mp->map_name, MDB_TGT_MAPSZ,
3065 3064 "LM%lr`%s", lmid, name);
3066 3065 } else {
3067 3066 (void) strncpy(mp->map_name, name, MDB_TGT_MAPSZ - 1);
3068 3067 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3069 3068 }
3070 3069 } else {
3071 3070 (void) strncpy(mp->map_name, prp->pr_mapname,
3072 3071 MDB_TGT_MAPSZ - 1);
3073 3072 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3074 3073 }
3075 3074
3076 3075 mp->map_base = prp->pr_vaddr;
3077 3076 mp->map_size = prp->pr_size;
3078 3077 mp->map_flags = 0;
3079 3078
3080 3079 if (prp->pr_mflags & MA_READ)
3081 3080 mp->map_flags |= MDB_TGT_MAP_R;
3082 3081 if (prp->pr_mflags & MA_WRITE)
3083 3082 mp->map_flags |= MDB_TGT_MAP_W;
3084 3083 if (prp->pr_mflags & MA_EXEC)
3085 3084 mp->map_flags |= MDB_TGT_MAP_X;
3086 3085
3087 3086 if (prp->pr_mflags & MA_SHM)
3088 3087 mp->map_flags |= MDB_TGT_MAP_SHMEM;
3089 3088 if (prp->pr_mflags & MA_BREAK)
3090 3089 mp->map_flags |= MDB_TGT_MAP_HEAP;
3091 3090 if (prp->pr_mflags & MA_STACK)
3092 3091 mp->map_flags |= MDB_TGT_MAP_STACK;
3093 3092 if (prp->pr_mflags & MA_ANON)
3094 3093 mp->map_flags |= MDB_TGT_MAP_ANON;
3095 3094
3096 3095 return (mp);
3097 3096 }
3098 3097
3099 3098 /*ARGSUSED*/
3100 3099 static int
3101 3100 pt_map_apply(void *arg, const prmap_t *prp, const char *name)
3102 3101 {
3103 3102 pt_maparg_t *pmp = arg;
3104 3103 mdb_map_t map;
3105 3104
3106 3105 return (pmp->pmap_func(pmp->pmap_private,
3107 3106 pt_prmap_to_mdbmap(pmp->pmap_targ, prp, &map), map.map_name));
3108 3107 }
3109 3108
3110 3109 static int
3111 3110 pt_mapping_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
3112 3111 {
3113 3112 if (t->t_pshandle != NULL) {
3114 3113 pt_maparg_t pm;
3115 3114
3116 3115 pm.pmap_targ = t;
3117 3116 pm.pmap_func = func;
3118 3117 pm.pmap_private = private;
3119 3118
3120 3119 if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3121 3120 (void) Pmapping_iter_resolved(t->t_pshandle,
3122 3121 pt_map_apply, &pm);
3123 3122 } else {
3124 3123 (void) Pmapping_iter(t->t_pshandle,
3125 3124 pt_map_apply, &pm);
3126 3125 }
3127 3126 return (0);
3128 3127 }
3129 3128
3130 3129 return (set_errno(EMDB_NOPROC));
3131 3130 }
3132 3131
3133 3132 static int
3134 3133 pt_object_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
3135 3134 {
3136 3135 pt_data_t *pt = t->t_data;
3137 3136
3138 3137 /*
3139 3138 * If we have a libproc handle, we can just call Pobject_iter to
3140 3139 * iterate over its list of load object information.
3141 3140 */
3142 3141 if (t->t_pshandle != NULL) {
3143 3142 pt_maparg_t pm;
3144 3143
3145 3144 pm.pmap_targ = t;
3146 3145 pm.pmap_func = func;
3147 3146 pm.pmap_private = private;
3148 3147
3149 3148 if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3150 3149 (void) Pobject_iter_resolved(t->t_pshandle,
3151 3150 pt_map_apply, &pm);
3152 3151 } else {
3153 3152 (void) Pobject_iter(t->t_pshandle,
3154 3153 pt_map_apply, &pm);
3155 3154 }
3156 3155 return (0);
3157 3156 }
3158 3157
3159 3158 /*
3160 3159 * If we're examining an executable or other ELF file but we have no
3161 3160 * libproc handle, fake up some information based on DT_NEEDED entries.
3162 3161 */
3163 3162 if (pt->p_dynsym != NULL && pt->p_file->gf_dyns != NULL &&
3164 3163 pt->p_fio != NULL) {
3165 3164 mdb_gelf_sect_t *gsp = pt->p_dynsym->gst_ssect;
3166 3165 GElf_Dyn *dynp = pt->p_file->gf_dyns;
3167 3166 mdb_map_t *mp = &pt->p_map;
3168 3167 const char *s = IOP_NAME(pt->p_fio);
3169 3168 size_t i;
3170 3169
3171 3170 (void) strncpy(mp->map_name, s, MDB_TGT_MAPSZ);
3172 3171 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3173 3172 mp->map_flags = MDB_TGT_MAP_R | MDB_TGT_MAP_X;
3174 3173 mp->map_base = NULL;
3175 3174 mp->map_size = 0;
3176 3175
3177 3176 if (func(private, mp, s) != 0)
3178 3177 return (0);
3179 3178
3180 3179 for (i = 0; i < pt->p_file->gf_ndyns; i++, dynp++) {
3181 3180 if (dynp->d_tag == DT_NEEDED) {
3182 3181 s = (char *)gsp->gs_data + dynp->d_un.d_val;
3183 3182 (void) strncpy(mp->map_name, s, MDB_TGT_MAPSZ);
3184 3183 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3185 3184 if (func(private, mp, s) != 0)
3186 3185 return (0);
3187 3186 }
3188 3187 }
3189 3188
3190 3189 return (0);
3191 3190 }
3192 3191
3193 3192 return (set_errno(EMDB_NOPROC));
3194 3193 }
3195 3194
3196 3195 static const mdb_map_t *
3197 3196 pt_addr_to_map(mdb_tgt_t *t, uintptr_t addr)
3198 3197 {
3199 3198 pt_data_t *pt = t->t_data;
3200 3199 const prmap_t *pmp;
3201 3200
3202 3201 if (t->t_pshandle == NULL) {
3203 3202 (void) set_errno(EMDB_NOPROC);
3204 3203 return (NULL);
3205 3204 }
3206 3205
3207 3206 if ((pmp = Paddr_to_map(t->t_pshandle, addr)) == NULL) {
3208 3207 (void) set_errno(EMDB_NOMAP);
3209 3208 return (NULL);
3210 3209 }
3211 3210
3212 3211 return (pt_prmap_to_mdbmap(t, pmp, &pt->p_map));
3213 3212 }
3214 3213
3215 3214 static const mdb_map_t *
3216 3215 pt_name_to_map(mdb_tgt_t *t, const char *object)
3217 3216 {
3218 3217 pt_data_t *pt = t->t_data;
3219 3218 const prmap_t *pmp;
3220 3219 Lmid_t lmid;
3221 3220
3222 3221 if (t->t_pshandle == NULL) {
3223 3222 (void) set_errno(EMDB_NOPROC);
3224 3223 return (NULL);
3225 3224 }
3226 3225
3227 3226 object = pt_resolve_lmid(object, &lmid);
3228 3227
3229 3228 if ((pmp = Plmid_to_map(t->t_pshandle, lmid, object)) == NULL) {
3230 3229 (void) set_errno(EMDB_NOOBJ);
3231 3230 return (NULL);
3232 3231 }
3233 3232
3234 3233 return (pt_prmap_to_mdbmap(t, pmp, &pt->p_map));
3235 3234 }
3236 3235
3237 3236 static ctf_file_t *
3238 3237 pt_addr_to_ctf(mdb_tgt_t *t, uintptr_t addr)
3239 3238 {
3240 3239 ctf_file_t *ret;
3241 3240
3242 3241 if (t->t_pshandle == NULL) {
3243 3242 (void) set_errno(EMDB_NOPROC);
3244 3243 return (NULL);
3245 3244 }
3246 3245
3247 3246 if ((ret = Paddr_to_ctf(t->t_pshandle, addr)) == NULL) {
3248 3247 (void) set_errno(EMDB_NOOBJ);
3249 3248 return (NULL);
3250 3249 }
3251 3250
3252 3251 return (ret);
3253 3252 }
3254 3253
3255 3254 static ctf_file_t *
3256 3255 pt_name_to_ctf(mdb_tgt_t *t, const char *name)
3257 3256 {
3258 3257 ctf_file_t *ret;
3259 3258
3260 3259 if (t->t_pshandle == NULL) {
3261 3260 (void) set_errno(EMDB_NOPROC);
3262 3261 return (NULL);
3263 3262 }
3264 3263
3265 3264 if ((ret = Pname_to_ctf(t->t_pshandle, name)) == NULL) {
3266 3265 (void) set_errno(EMDB_NOOBJ);
3267 3266 return (NULL);
3268 3267 }
3269 3268
3270 3269 return (ret);
3271 3270 }
3272 3271
3273 3272 static int
3274 3273 pt_status(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
3275 3274 {
3276 3275 const pstatus_t *psp;
3277 3276 prgregset_t gregs;
3278 3277 int state;
3279 3278
3280 3279 bzero(tsp, sizeof (mdb_tgt_status_t));
3281 3280
3282 3281 if (t->t_pshandle == NULL) {
3283 3282 tsp->st_state = MDB_TGT_IDLE;
3284 3283 return (0);
3285 3284 }
3286 3285
3287 3286 switch (state = Pstate(t->t_pshandle)) {
3288 3287 case PS_RUN:
3289 3288 tsp->st_state = MDB_TGT_RUNNING;
3290 3289 break;
3291 3290
3292 3291 case PS_STOP:
3293 3292 tsp->st_state = MDB_TGT_STOPPED;
3294 3293 psp = Pstatus(t->t_pshandle);
3295 3294
3296 3295 tsp->st_tid = PTL_TID(t);
3297 3296 if (PTL_GETREGS(t, tsp->st_tid, gregs) == 0)
3298 3297 tsp->st_pc = gregs[R_PC];
3299 3298
3300 3299 if (psp->pr_flags & PR_ISTOP)
3301 3300 tsp->st_flags |= MDB_TGT_ISTOP;
3302 3301 if (psp->pr_flags & PR_DSTOP)
3303 3302 tsp->st_flags |= MDB_TGT_DSTOP;
3304 3303
3305 3304 break;
3306 3305
3307 3306 case PS_LOST:
3308 3307 tsp->st_state = MDB_TGT_LOST;
3309 3308 break;
3310 3309 case PS_UNDEAD:
3311 3310 tsp->st_state = MDB_TGT_UNDEAD;
3312 3311 break;
3313 3312 case PS_DEAD:
3314 3313 tsp->st_state = MDB_TGT_DEAD;
3315 3314 break;
3316 3315 case PS_IDLE:
3317 3316 tsp->st_state = MDB_TGT_IDLE;
3318 3317 break;
3319 3318 default:
3320 3319 fail("unknown libproc state (%d)\n", state);
3321 3320 }
3322 3321
3323 3322 if (t->t_flags & MDB_TGT_F_BUSY)
3324 3323 tsp->st_flags |= MDB_TGT_BUSY;
3325 3324
3326 3325 return (0);
3327 3326 }
3328 3327
3329 3328 static void
3330 3329 pt_dupfd(const char *file, int oflags, mode_t mode, int dfd)
3331 3330 {
3332 3331 int fd;
3333 3332
3334 3333 if ((fd = open(file, oflags, mode)) >= 0) {
3335 3334 (void) fcntl(fd, F_DUP2FD, dfd);
3336 3335 (void) close(fd);
3337 3336 } else
3338 3337 warn("failed to open %s as descriptor %d", file, dfd);
3339 3338 }
3340 3339
3341 3340 /*
3342 3341 * The Pcreate_callback() function interposes on the default, empty libproc
3343 3342 * definition. It will be called following a fork of a new child process by
3344 3343 * Pcreate() below, but before the exec of the new process image. We use this
3345 3344 * callback to optionally redirect stdin and stdout and reset the dispositions
3346 3345 * of SIGPIPE and SIGQUIT from SIG_IGN back to SIG_DFL.
3347 3346 */
3348 3347 /*ARGSUSED*/
3349 3348 void
3350 3349 Pcreate_callback(struct ps_prochandle *P)
3351 3350 {
3352 3351 pt_data_t *pt = mdb.m_target->t_data;
3353 3352
3354 3353 if (pt->p_stdin != NULL)
3355 3354 pt_dupfd(pt->p_stdin, O_RDWR, 0, STDIN_FILENO);
3356 3355 if (pt->p_stdout != NULL)
3357 3356 pt_dupfd(pt->p_stdout, O_CREAT | O_WRONLY, 0666, STDOUT_FILENO);
3358 3357
3359 3358 (void) mdb_signal_sethandler(SIGPIPE, SIG_DFL, NULL);
3360 3359 (void) mdb_signal_sethandler(SIGQUIT, SIG_DFL, NULL);
3361 3360 }
3362 3361
3363 3362 static int
3364 3363 pt_run(mdb_tgt_t *t, int argc, const mdb_arg_t *argv)
3365 3364 {
3366 3365 pt_data_t *pt = t->t_data;
3367 3366 struct ps_prochandle *P;
3368 3367 char execname[MAXPATHLEN];
3369 3368 const char **pargv;
3370 3369 int pargc = 0;
3371 3370 int i, perr;
3372 3371 char **penv;
3373 3372 mdb_var_t *v;
3374 3373
3375 3374 if (pt->p_aout_fio == NULL) {
3376 3375 warn("run requires executable to be specified on "
3377 3376 "command-line\n");
3378 3377 return (set_errno(EMDB_TGT));
3379 3378 }
3380 3379
3381 3380 pargv = mdb_alloc(sizeof (char *) * (argc + 2), UM_SLEEP);
3382 3381 pargv[pargc++] = strbasename(IOP_NAME(pt->p_aout_fio));
3383 3382
3384 3383 for (i = 0; i < argc; i++) {
3385 3384 if (argv[i].a_type != MDB_TYPE_STRING) {
3386 3385 mdb_free(pargv, sizeof (char *) * (argc + 2));
3387 3386 return (set_errno(EINVAL));
3388 3387 }
3389 3388 if (argv[i].a_un.a_str[0] == '<')
3390 3389 pt->p_stdin = argv[i].a_un.a_str + 1;
3391 3390 else if (argv[i].a_un.a_str[0] == '>')
3392 3391 pt->p_stdout = argv[i].a_un.a_str + 1;
3393 3392 else
3394 3393 pargv[pargc++] = argv[i].a_un.a_str;
3395 3394 }
3396 3395 pargv[pargc] = NULL;
3397 3396
3398 3397 /*
3399 3398 * Since Pcreate() uses execvp() and "." may not be present in $PATH,
3400 3399 * we must manually prepend "./" when the executable is a simple name.
3401 3400 */
3402 3401 if (strchr(IOP_NAME(pt->p_aout_fio), '/') == NULL) {
3403 3402 (void) snprintf(execname, sizeof (execname), "./%s",
3404 3403 IOP_NAME(pt->p_aout_fio));
3405 3404 } else {
3406 3405 (void) snprintf(execname, sizeof (execname), "%s",
3407 3406 IOP_NAME(pt->p_aout_fio));
3408 3407 }
3409 3408
3410 3409 penv = mdb_alloc((mdb_nv_size(&pt->p_env)+ 1) * sizeof (char *),
3411 3410 UM_SLEEP);
3412 3411 for (mdb_nv_rewind(&pt->p_env), i = 0;
3413 3412 (v = mdb_nv_advance(&pt->p_env)) != NULL; i++)
3414 3413 penv[i] = mdb_nv_get_cookie(v);
3415 3414 penv[i] = NULL;
3416 3415
3417 3416 P = Pxcreate(execname, (char **)pargv, penv, &perr, NULL, 0);
3418 3417 mdb_free(pargv, sizeof (char *) * (argc + 2));
3419 3418 pt->p_stdin = pt->p_stdout = NULL;
3420 3419
3421 3420 mdb_free(penv, i * sizeof (char *));
3422 3421
3423 3422 if (P == NULL) {
3424 3423 warn("failed to create process: %s\n", Pcreate_error(perr));
3425 3424 return (set_errno(EMDB_TGT));
3426 3425 }
3427 3426
3428 3427 if (t->t_pshandle != NULL) {
3429 3428 pt_pre_detach(t, TRUE);
3430 3429 if (t->t_pshandle != pt->p_idlehandle)
3431 3430 Prelease(t->t_pshandle, pt->p_rflags);
3432 3431 }
3433 3432
3434 3433 (void) Punsetflags(P, PR_RLC); /* make sure run-on-last-close is off */
3435 3434 (void) Psetflags(P, PR_KLC); /* kill on last close by debugger */
3436 3435 pt->p_rflags = PRELEASE_KILL; /* kill on debugger Prelease */
3437 3436 t->t_pshandle = P;
3438 3437
3439 3438 pt_post_attach(t);
3440 3439 pt_activate_common(t);
3441 3440 (void) mdb_tgt_status(t, &t->t_status);
3442 3441 mdb.m_flags |= MDB_FL_VCREATE;
3443 3442
3444 3443 return (0);
3445 3444 }
3446 3445
3447 3446 /*
3448 3447 * Forward a signal to the victim process in order to force it to stop or die.
3449 3448 * Refer to the comments above pt_setrun(), below, for more info.
3450 3449 */
3451 3450 /*ARGSUSED*/
3452 3451 static void
3453 3452 pt_sigfwd(int sig, siginfo_t *sip, ucontext_t *ucp, mdb_tgt_t *t)
3454 3453 {
3455 3454 struct ps_prochandle *P = t->t_pshandle;
3456 3455 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
3457 3456 pid_t pid = Pstatus(P)->pr_pid;
3458 3457 long ctl[2];
3459 3458
3460 3459 if (getpgid(pid) != mdb.m_pgid) {
3461 3460 mdb_dprintf(MDB_DBG_TGT, "fwd SIG#%d to %d\n", sig, (int)pid);
3462 3461 (void) kill(pid, sig);
3463 3462 }
3464 3463
3465 3464 if (Pwait(P, 1) == 0 && (psp->pr_flags & PR_STOPPED) &&
3466 3465 psp->pr_why == PR_JOBCONTROL && Pdstop(P) == 0) {
3467 3466 /*
3468 3467 * If we're job control stopped and our DSTOP is pending, the
3469 3468 * victim will never see our signal, so undo the kill() and
3470 3469 * then send SIGCONT the victim to kick it out of the job
3471 3470 * control stop and force our DSTOP to take effect.
3472 3471 */
3473 3472 if ((psp->pr_flags & PR_DSTOP) &&
3474 3473 prismember(&Pstatus(P)->pr_sigpend, sig)) {
3475 3474 ctl[0] = PCUNKILL;
3476 3475 ctl[1] = sig;
3477 3476 (void) write(Pctlfd(P), ctl, sizeof (ctl));
3478 3477 }
3479 3478
3480 3479 mdb_dprintf(MDB_DBG_TGT, "fwd SIGCONT to %d\n", (int)pid);
3481 3480 (void) kill(pid, SIGCONT);
3482 3481 }
3483 3482 }
3484 3483
3485 3484 /*
3486 3485 * Common code for step and continue: if no victim process has been created,
3487 3486 * call pt_run() to create one. Then set the victim running, clearing any
3488 3487 * pending fault. One special case is that if the victim was previously
3489 3488 * stopped on reception of SIGINT, we know that SIGINT was traced and the user
3490 3489 * requested the victim to stop, so clear this signal before continuing.
3491 3490 * For all other traced signals, the signal will be delivered on continue.
3492 3491 *
3493 3492 * Once the victim process is running, we wait for it to stop on an event of
3494 3493 * interest. Although libproc provides the basic primitive to wait for the
3495 3494 * victim, we must be careful in our handling of signals. We want to allow the
3496 3495 * user to issue a SIGINT or SIGQUIT using the designated terminal control
3497 3496 * character (typically ^C and ^\), and have these signals stop the target and
3498 3497 * return control to the debugger if the signals are traced. There are three
3499 3498 * cases to be considered in our implementation:
3500 3499 *
3501 3500 * (1) If the debugger and victim are in the same process group, both receive
3502 3501 * the signal from the terminal driver. The debugger returns from Pwait() with
3503 3502 * errno = EINTR, so we want to loop back and continue waiting until the victim
3504 3503 * stops on receipt of its SIGINT or SIGQUIT.
3505 3504 *
3506 3505 * (2) If the debugger and victim are in different process groups, and the
3507 3506 * victim is a member of the foreground process group, it will receive the
3508 3507 * signal from the terminal driver and the debugger will not. As such, we
3509 3508 * will remain blocked in Pwait() until the victim stops on its signal.
3510 3509 *
3511 3510 * (3) If the debugger and victim are in different process groups, and the
3512 3511 * debugger is a member of the foreground process group, it will receive the
3513 3512 * signal from the terminal driver, and the victim will not. The debugger
3514 3513 * returns from Pwait() with errno = EINTR, so we need to forward the signal
3515 3514 * to the victim process directly and then Pwait() again for it to stop.
3516 3515 *
3517 3516 * We can observe that all three cases are handled by simply calling Pwait()
3518 3517 * repeatedly if it fails with EINTR, and forwarding SIGINT and SIGQUIT to
3519 3518 * the victim if it is in a different process group, using pt_sigfwd() above.
3520 3519 *
3521 3520 * An additional complication is that the process may not be able to field
3522 3521 * the signal if it is currently stopped by job control. In this case, we
3523 3522 * also DSTOP the process, and then send it a SIGCONT to wake it up from
3524 3523 * job control and force it to re-enter stop() under the control of /proc.
3525 3524 *
3526 3525 * Finally, we would like to allow the user to suspend the process using the
3527 3526 * terminal suspend character (typically ^Z) if both are in the same session.
3528 3527 * We again employ pt_sigfwd() to forward SIGTSTP to the victim, wait for it to
3529 3528 * stop from job control, and then capture it using /proc. Once the process
3530 3529 * has stopped, normal SIGTSTP processing is restored and the user can issue
3531 3530 * another ^Z in order to suspend the debugger and return to the parent shell.
3532 3531 */
3533 3532 static int
3534 3533 pt_setrun(mdb_tgt_t *t, mdb_tgt_status_t *tsp, int flags)
3535 3534 {
3536 3535 struct ps_prochandle *P = t->t_pshandle;
3537 3536 pt_data_t *pt = t->t_data;
3538 3537 pid_t old_pgid = -1;
3539 3538
3540 3539 mdb_signal_f *intf, *quitf, *tstpf;
3541 3540 const lwpstatus_t *psp;
3542 3541 void *intd, *quitd, *tstpd;
3543 3542
3544 3543 int sig = pt->p_signal;
3545 3544 int error = 0;
3546 3545 int pgid = -1;
3547 3546
3548 3547 pt->p_signal = 0; /* clear pending signal */
3549 3548
3550 3549 if (P == NULL && pt_run(t, 0, NULL) == -1)
3551 3550 return (-1); /* errno is set for us */
3552 3551
3553 3552 P = t->t_pshandle;
3554 3553 psp = &Pstatus(P)->pr_lwp;
3555 3554
3556 3555 if (sig == 0 && psp->pr_why == PR_SIGNALLED && psp->pr_what == SIGINT)
3557 3556 flags |= PRCSIG; /* clear pending SIGINT */
3558 3557 else
3559 3558 flags |= PRCFAULT; /* clear any pending fault (e.g. BPT) */
3560 3559
3561 3560 intf = mdb_signal_gethandler(SIGINT, &intd);
3562 3561 quitf = mdb_signal_gethandler(SIGQUIT, &quitd);
3563 3562 tstpf = mdb_signal_gethandler(SIGTSTP, &tstpd);
3564 3563
3565 3564 (void) mdb_signal_sethandler(SIGINT, (mdb_signal_f *)pt_sigfwd, t);
3566 3565 (void) mdb_signal_sethandler(SIGQUIT, (mdb_signal_f *)pt_sigfwd, t);
3567 3566 (void) mdb_signal_sethandler(SIGTSTP, (mdb_signal_f *)pt_sigfwd, t);
3568 3567
3569 3568 if (sig != 0 && Pstate(P) == PS_RUN &&
3570 3569 kill(Pstatus(P)->pr_pid, sig) == -1) {
3571 3570 error = errno;
3572 3571 goto out;
3573 3572 }
3574 3573
3575 3574 /*
3576 3575 * If we attached to a job stopped background process in the same
3577 3576 * session, make its pgid the foreground process group before running
3578 3577 * it. Ignore SIGTTOU while doing this to avoid being suspended.
3579 3578 */
3580 3579 if (mdb.m_flags & MDB_FL_JOBCTL) {
3581 3580 (void) mdb_signal_sethandler(SIGTTOU, SIG_IGN, NULL);
3582 3581 (void) IOP_CTL(mdb.m_term, TIOCGPGRP, &old_pgid);
3583 3582 (void) IOP_CTL(mdb.m_term, TIOCSPGRP,
3584 3583 (void *)&Pstatus(P)->pr_pgid);
3585 3584 (void) mdb_signal_sethandler(SIGTTOU, SIG_DFL, NULL);
3586 3585 }
3587 3586
3588 3587 if (Pstate(P) != PS_RUN && Psetrun(P, sig, flags) == -1) {
3589 3588 error = errno;
3590 3589 goto out;
3591 3590 }
3592 3591
3593 3592 /*
3594 3593 * If the process is stopped on job control, resume its process group
3595 3594 * by sending it a SIGCONT if we are in the same session. Otherwise
3596 3595 * we have no choice but to wait for someone else to foreground it.
3597 3596 */
3598 3597 if (psp->pr_why == PR_JOBCONTROL) {
3599 3598 if (mdb.m_flags & MDB_FL_JOBCTL)
3600 3599 (void) kill(-Pstatus(P)->pr_pgid, SIGCONT);
3601 3600 else if (mdb.m_term != NULL)
3602 3601 warn("process is still suspended by job control ...\n");
3603 3602 }
3604 3603
3605 3604 /*
3606 3605 * Wait for the process to stop. As described above, we loop around if
3607 3606 * we are interrupted (EINTR). If we lose control, attempt to re-open
3608 3607 * the process, or call pt_exec() if that fails to handle a re-exec.
3609 3608 * If the process dies (ENOENT) or Pwait() fails, break out of the loop.
3610 3609 */
3611 3610 while (Pwait(P, 0) == -1) {
3612 3611 if (errno != EINTR) {
3613 3612 if (Pstate(P) == PS_LOST) {
3614 3613 if (Preopen(P) == 0)
3615 3614 continue; /* Pwait() again */
3616 3615 else
3617 3616 pt_exec(t, 0, NULL);
3618 3617 } else if (errno != ENOENT)
3619 3618 warn("failed to wait for event");
3620 3619 break;
3621 3620 }
3622 3621 }
3623 3622
3624 3623 /*
3625 3624 * If we changed the foreground process group, restore the old pgid
3626 3625 * while ignoring SIGTTOU so we are not accidentally suspended.
3627 3626 */
3628 3627 if (old_pgid != -1) {
3629 3628 (void) mdb_signal_sethandler(SIGTTOU, SIG_IGN, NULL);
3630 3629 (void) IOP_CTL(mdb.m_term, TIOCSPGRP, &pgid);
3631 3630 (void) mdb_signal_sethandler(SIGTTOU, SIG_DFL, NULL);
3632 3631 }
3633 3632
3634 3633 /*
3635 3634 * If we're now stopped on exit from a successful exec, release any
3636 3635 * vfork parents and clean out their address space before returning
3637 3636 * to tgt_continue() and perturbing the list of armed event specs.
3638 3637 * If we're stopped for any other reason, just update the mappings.
3639 3638 */
3640 3639 switch (Pstate(P)) {
3641 3640 case PS_STOP:
3642 3641 if (psp->pr_why == PR_SYSEXIT && psp->pr_errno == 0 &&
3643 3642 psp->pr_what == SYS_execve)
3644 3643 pt_release_parents(t);
3645 3644 else
3646 3645 Pupdate_maps(P);
3647 3646 break;
3648 3647
3649 3648 case PS_UNDEAD:
3650 3649 case PS_LOST:
3651 3650 pt_release_parents(t);
3652 3651 break;
3653 3652 }
3654 3653
3655 3654 out:
3656 3655 (void) mdb_signal_sethandler(SIGINT, intf, intd);
3657 3656 (void) mdb_signal_sethandler(SIGQUIT, quitf, quitd);
3658 3657 (void) mdb_signal_sethandler(SIGTSTP, tstpf, tstpd);
3659 3658 (void) pt_status(t, tsp);
3660 3659
3661 3660 return (error ? set_errno(error) : 0);
3662 3661 }
3663 3662
3664 3663 static int
3665 3664 pt_step(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
3666 3665 {
3667 3666 return (pt_setrun(t, tsp, PRSTEP));
3668 3667 }
3669 3668
3670 3669 static int
3671 3670 pt_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
3672 3671 {
3673 3672 return (pt_setrun(t, tsp, 0));
3674 3673 }
3675 3674
3676 3675 static int
3677 3676 pt_signal(mdb_tgt_t *t, int sig)
3678 3677 {
3679 3678 pt_data_t *pt = t->t_data;
3680 3679
3681 3680 if (sig > 0 && sig <= pt->p_maxsig) {
3682 3681 pt->p_signal = sig; /* pending until next pt_setrun */
3683 3682 return (0);
3684 3683 }
3685 3684
3686 3685 return (set_errno(EMDB_BADSIGNUM));
3687 3686 }
3688 3687
3689 3688 static int
3690 3689 pt_sysenter_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3691 3690 {
3692 3691 struct ps_prochandle *P = t->t_pshandle;
3693 3692
3694 3693 if (P != NULL && Pstate(P) < PS_LOST) {
3695 3694 sep->se_data = args; /* data is raw system call number */
3696 3695 return (Psysentry(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
3697 3696 }
3698 3697
3699 3698 return (set_errno(EMDB_NOPROC));
3700 3699 }
3701 3700
3702 3701 static void
3703 3702 pt_sysenter_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3704 3703 {
3705 3704 (void) Psysentry(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
3706 3705 }
3707 3706
3708 3707 /*ARGSUSED*/
3709 3708 static char *
3710 3709 pt_sysenter_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
3711 3710 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
3712 3711 {
3713 3712 char name[32];
3714 3713 int sysnum;
3715 3714
3716 3715 if (vep != NULL)
3717 3716 sysnum = (intptr_t)vep->ve_args;
3718 3717 else
3719 3718 sysnum = (intptr_t)sep->se_data;
3720 3719
3721 3720 (void) proc_sysname(sysnum, name, sizeof (name));
3722 3721 (void) mdb_iob_snprintf(buf, nbytes, "stop on entry to %s", name);
3723 3722
3724 3723 return (buf);
3725 3724 }
3726 3725
3727 3726 /*ARGSUSED*/
3728 3727 static int
3729 3728 pt_sysenter_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
3730 3729 {
3731 3730 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
3732 3731 int sysnum = (intptr_t)sep->se_data;
3733 3732
3734 3733 return (psp->pr_why == PR_SYSENTRY && psp->pr_what == sysnum);
3735 3734 }
3736 3735
3737 3736 static const mdb_se_ops_t proc_sysenter_ops = {
3738 3737 pt_sysenter_ctor, /* se_ctor */
3739 3738 pt_sysenter_dtor, /* se_dtor */
3740 3739 pt_sysenter_info, /* se_info */
3741 3740 no_se_secmp, /* se_secmp */
3742 3741 no_se_vecmp, /* se_vecmp */
3743 3742 no_se_arm, /* se_arm */
3744 3743 no_se_disarm, /* se_disarm */
3745 3744 no_se_cont, /* se_cont */
3746 3745 pt_sysenter_match /* se_match */
3747 3746 };
3748 3747
3749 3748 static int
3750 3749 pt_sysexit_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3751 3750 {
3752 3751 struct ps_prochandle *P = t->t_pshandle;
3753 3752
3754 3753 if (P != NULL && Pstate(P) < PS_LOST) {
3755 3754 sep->se_data = args; /* data is raw system call number */
3756 3755 return (Psysexit(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
3757 3756 }
3758 3757
3759 3758 return (set_errno(EMDB_NOPROC));
3760 3759 }
3761 3760
3762 3761 static void
3763 3762 pt_sysexit_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3764 3763 {
3765 3764 (void) Psysexit(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
3766 3765 }
3767 3766
3768 3767 /*ARGSUSED*/
3769 3768 static char *
3770 3769 pt_sysexit_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
3771 3770 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
3772 3771 {
3773 3772 char name[32];
3774 3773 int sysnum;
3775 3774
3776 3775 if (vep != NULL)
3777 3776 sysnum = (intptr_t)vep->ve_args;
3778 3777 else
3779 3778 sysnum = (intptr_t)sep->se_data;
3780 3779
3781 3780 (void) proc_sysname(sysnum, name, sizeof (name));
3782 3781 (void) mdb_iob_snprintf(buf, nbytes, "stop on exit from %s", name);
3783 3782
3784 3783 return (buf);
3785 3784 }
3786 3785
3787 3786 /*ARGSUSED*/
3788 3787 static int
3789 3788 pt_sysexit_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
3790 3789 {
3791 3790 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
3792 3791 int sysnum = (intptr_t)sep->se_data;
3793 3792
3794 3793 return (psp->pr_why == PR_SYSEXIT && psp->pr_what == sysnum);
3795 3794 }
3796 3795
3797 3796 static const mdb_se_ops_t proc_sysexit_ops = {
3798 3797 pt_sysexit_ctor, /* se_ctor */
3799 3798 pt_sysexit_dtor, /* se_dtor */
3800 3799 pt_sysexit_info, /* se_info */
3801 3800 no_se_secmp, /* se_secmp */
3802 3801 no_se_vecmp, /* se_vecmp */
3803 3802 no_se_arm, /* se_arm */
3804 3803 no_se_disarm, /* se_disarm */
3805 3804 no_se_cont, /* se_cont */
3806 3805 pt_sysexit_match /* se_match */
3807 3806 };
3808 3807
3809 3808 static int
3810 3809 pt_signal_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3811 3810 {
3812 3811 struct ps_prochandle *P = t->t_pshandle;
3813 3812
3814 3813 if (P != NULL && Pstate(P) < PS_LOST) {
3815 3814 sep->se_data = args; /* data is raw signal number */
3816 3815 return (Psignal(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
3817 3816 }
3818 3817
3819 3818 return (set_errno(EMDB_NOPROC));
3820 3819 }
3821 3820
3822 3821 static void
3823 3822 pt_signal_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3824 3823 {
3825 3824 (void) Psignal(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
3826 3825 }
3827 3826
3828 3827 /*ARGSUSED*/
3829 3828 static char *
3830 3829 pt_signal_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
3831 3830 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
3832 3831 {
3833 3832 char name[SIG2STR_MAX];
3834 3833 int signum;
3835 3834
3836 3835 if (vep != NULL)
3837 3836 signum = (intptr_t)vep->ve_args;
3838 3837 else
3839 3838 signum = (intptr_t)sep->se_data;
3840 3839
3841 3840 (void) proc_signame(signum, name, sizeof (name));
3842 3841 (void) mdb_iob_snprintf(buf, nbytes, "stop on %s", name);
3843 3842
3844 3843 return (buf);
3845 3844 }
3846 3845
3847 3846 /*ARGSUSED*/
3848 3847 static int
3849 3848 pt_signal_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
3850 3849 {
3851 3850 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
3852 3851 int signum = (intptr_t)sep->se_data;
3853 3852
3854 3853 return (psp->pr_why == PR_SIGNALLED && psp->pr_what == signum);
3855 3854 }
3856 3855
3857 3856 static const mdb_se_ops_t proc_signal_ops = {
3858 3857 pt_signal_ctor, /* se_ctor */
3859 3858 pt_signal_dtor, /* se_dtor */
3860 3859 pt_signal_info, /* se_info */
3861 3860 no_se_secmp, /* se_secmp */
3862 3861 no_se_vecmp, /* se_vecmp */
3863 3862 no_se_arm, /* se_arm */
3864 3863 no_se_disarm, /* se_disarm */
3865 3864 no_se_cont, /* se_cont */
3866 3865 pt_signal_match /* se_match */
3867 3866 };
3868 3867
3869 3868 static int
3870 3869 pt_fault_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3871 3870 {
3872 3871 struct ps_prochandle *P = t->t_pshandle;
3873 3872
3874 3873 if (P != NULL && Pstate(P) < PS_LOST) {
3875 3874 sep->se_data = args; /* data is raw fault number */
3876 3875 return (Pfault(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
3877 3876 }
3878 3877
3879 3878 return (set_errno(EMDB_NOPROC));
3880 3879 }
3881 3880
3882 3881 static void
3883 3882 pt_fault_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3884 3883 {
3885 3884 int fault = (intptr_t)sep->se_data;
3886 3885
3887 3886 if (fault != FLTBPT && fault != FLTTRACE && fault != FLTWATCH)
3888 3887 (void) Pfault(t->t_pshandle, fault, FALSE);
3889 3888 }
3890 3889
3891 3890 /*ARGSUSED*/
3892 3891 static char *
3893 3892 pt_fault_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
3894 3893 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
3895 3894 {
3896 3895 char name[32];
3897 3896 int fltnum;
3898 3897
3899 3898 if (vep != NULL)
3900 3899 fltnum = (intptr_t)vep->ve_args;
3901 3900 else
3902 3901 fltnum = (intptr_t)sep->se_data;
3903 3902
3904 3903 (void) proc_fltname(fltnum, name, sizeof (name));
3905 3904 (void) mdb_iob_snprintf(buf, nbytes, "stop on %s", name);
3906 3905
3907 3906 return (buf);
3908 3907 }
3909 3908
3910 3909 /*ARGSUSED*/
3911 3910 static int
3912 3911 pt_fault_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
3913 3912 {
3914 3913 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
3915 3914 int fltnum = (intptr_t)sep->se_data;
3916 3915
3917 3916 return (psp->pr_why == PR_FAULTED && psp->pr_what == fltnum);
3918 3917 }
3919 3918
3920 3919 static const mdb_se_ops_t proc_fault_ops = {
3921 3920 pt_fault_ctor, /* se_ctor */
3922 3921 pt_fault_dtor, /* se_dtor */
3923 3922 pt_fault_info, /* se_info */
3924 3923 no_se_secmp, /* se_secmp */
3925 3924 no_se_vecmp, /* se_vecmp */
3926 3925 no_se_arm, /* se_arm */
3927 3926 no_se_disarm, /* se_disarm */
3928 3927 no_se_cont, /* se_cont */
3929 3928 pt_fault_match /* se_match */
3930 3929 };
3931 3930
3932 3931 /*
3933 3932 * Callback for pt_ignore() dcmd above: for each VID, determine if it
3934 3933 * corresponds to a vespec that traces the specified signal, and delete it.
3935 3934 */
3936 3935 /*ARGSUSED*/
3937 3936 static int
3938 3937 pt_ignore_sig(mdb_tgt_t *t, void *sig, int vid, void *data)
3939 3938 {
3940 3939 mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, vid);
3941 3940
3942 3941 if (vep->ve_se->se_ops == &proc_signal_ops && vep->ve_args == sig)
3943 3942 (void) mdb_tgt_vespec_delete(t, vid);
3944 3943
3945 3944 return (0);
3946 3945 }
3947 3946
3948 3947 static int
3949 3948 pt_brkpt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3950 3949 {
3951 3950 pt_data_t *pt = t->t_data;
3952 3951 pt_bparg_t *pta = args;
3953 3952 pt_brkpt_t *ptb;
3954 3953 GElf_Sym s;
3955 3954
3956 3955 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) >= PS_LOST)
3957 3956 return (set_errno(EMDB_NOPROC));
3958 3957
3959 3958 if (pta->pta_symbol != NULL) {
3960 3959 if (!pt->p_rtld_finished &&
3961 3960 strchr(pta->pta_symbol, '`') == NULL)
3962 3961 return (set_errno(EMDB_NOSYM));
3963 3962 if (mdb_tgt_lookup_by_scope(t, pta->pta_symbol, &s,
3964 3963 NULL) == -1) {
3965 3964 if (errno != EMDB_NOOBJ && !(errno == EMDB_NOSYM &&
3966 3965 (!(mdb.m_flags & MDB_FL_BPTNOSYMSTOP) ||
3967 3966 !pt->p_rtld_finished))) {
3968 3967 warn("breakpoint %s activation failed",
3969 3968 pta->pta_symbol);
3970 3969 }
3971 3970 return (-1); /* errno is set for us */
3972 3971 }
3973 3972
3974 3973 pta->pta_addr = (uintptr_t)s.st_value;
3975 3974 }
3976 3975
3977 3976 #ifdef __sparc
3978 3977 if (pta->pta_addr & 3)
3979 3978 return (set_errno(EMDB_BPALIGN));
3980 3979 #endif
3981 3980
3982 3981 if (Paddr_to_map(t->t_pshandle, pta->pta_addr) == NULL)
3983 3982 return (set_errno(EMDB_NOMAP));
3984 3983
3985 3984 ptb = mdb_alloc(sizeof (pt_brkpt_t), UM_SLEEP);
3986 3985 ptb->ptb_addr = pta->pta_addr;
3987 3986 ptb->ptb_instr = NULL;
3988 3987 sep->se_data = ptb;
3989 3988
3990 3989 return (0);
3991 3990 }
3992 3991
3993 3992 /*ARGSUSED*/
3994 3993 static void
3995 3994 pt_brkpt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3996 3995 {
3997 3996 mdb_free(sep->se_data, sizeof (pt_brkpt_t));
3998 3997 }
3999 3998
4000 3999 /*ARGSUSED*/
4001 4000 static char *
4002 4001 pt_brkpt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
4003 4002 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
4004 4003 {
4005 4004 uintptr_t addr = NULL;
4006 4005
4007 4006 if (vep != NULL) {
4008 4007 pt_bparg_t *pta = vep->ve_args;
4009 4008
4010 4009 if (pta->pta_symbol != NULL) {
4011 4010 (void) mdb_iob_snprintf(buf, nbytes, "stop at %s",
4012 4011 pta->pta_symbol);
4013 4012 } else {
4014 4013 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a",
4015 4014 pta->pta_addr);
4016 4015 addr = pta->pta_addr;
4017 4016 }
4018 4017
4019 4018 } else {
4020 4019 addr = ((pt_brkpt_t *)sep->se_data)->ptb_addr;
4021 4020 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a", addr);
4022 4021 }
4023 4022
4024 4023 sp->spec_base = addr;
4025 4024 sp->spec_size = sizeof (instr_t);
4026 4025
4027 4026 return (buf);
4028 4027 }
4029 4028
4030 4029 static int
4031 4030 pt_brkpt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4032 4031 {
4033 4032 pt_brkpt_t *ptb = sep->se_data;
4034 4033 pt_bparg_t *pta = args;
4035 4034 GElf_Sym sym;
4036 4035
4037 4036 if (pta->pta_symbol != NULL) {
4038 4037 return (mdb_tgt_lookup_by_scope(t, pta->pta_symbol,
4039 4038 &sym, NULL) == 0 && sym.st_value == ptb->ptb_addr);
4040 4039 }
4041 4040
4042 4041 return (pta->pta_addr == ptb->ptb_addr);
4043 4042 }
4044 4043
4045 4044 /*ARGSUSED*/
4046 4045 static int
4047 4046 pt_brkpt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
4048 4047 {
4049 4048 pt_bparg_t *pta1 = vep->ve_args;
4050 4049 pt_bparg_t *pta2 = args;
4051 4050
4052 4051 if (pta1->pta_symbol != NULL && pta2->pta_symbol != NULL)
4053 4052 return (strcmp(pta1->pta_symbol, pta2->pta_symbol) == 0);
4054 4053
4055 4054 if (pta1->pta_symbol == NULL && pta2->pta_symbol == NULL)
4056 4055 return (pta1->pta_addr == pta2->pta_addr);
4057 4056
4058 4057 return (0); /* fail if one is symbolic, other is an explicit address */
4059 4058 }
4060 4059
4061 4060 static int
4062 4061 pt_brkpt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
4063 4062 {
4064 4063 pt_brkpt_t *ptb = sep->se_data;
4065 4064 return (Psetbkpt(t->t_pshandle, ptb->ptb_addr, &ptb->ptb_instr));
4066 4065 }
4067 4066
4068 4067 /*
4069 4068 * In order to disarm a breakpoint, we replace the trap instruction at ptb_addr
4070 4069 * with the saved instruction. However, if we have stopped after a successful
4071 4070 * exec(2), we do not want to restore ptb_instr because the address space has
4072 4071 * now been replaced with the text of a different executable, and so restoring
4073 4072 * the saved instruction would be incorrect. The exec itself has effectively
4074 4073 * removed all breakpoint trap instructions for us, so we can just return.
4075 4074 */
4076 4075 static int
4077 4076 pt_brkpt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
4078 4077 {
4079 4078 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4080 4079 pt_brkpt_t *ptb = sep->se_data;
4081 4080
4082 4081 if (psp->pr_why == PR_SYSEXIT && psp->pr_errno == 0 &&
4083 4082 psp->pr_what == SYS_execve)
4084 4083 return (0); /* do not restore saved instruction */
4085 4084
4086 4085 return (Pdelbkpt(t->t_pshandle, ptb->ptb_addr, ptb->ptb_instr));
4087 4086 }
4088 4087
4089 4088 /*
4090 4089 * Determine whether the specified sespec is an armed watchpoint that overlaps
4091 4090 * with the given breakpoint and has the given flags set. We use this to find
4092 4091 * conflicts with breakpoints, below.
4093 4092 */
4094 4093 static int
4095 4094 pt_wp_overlap(mdb_sespec_t *sep, pt_brkpt_t *ptb, int flags)
4096 4095 {
4097 4096 const prwatch_t *wp = sep->se_data;
4098 4097
4099 4098 return (sep->se_state == MDB_TGT_SPEC_ARMED &&
4100 4099 sep->se_ops == &proc_wapt_ops && (wp->pr_wflags & flags) &&
4101 4100 ptb->ptb_addr - wp->pr_vaddr < wp->pr_size);
4102 4101 }
4103 4102
4104 4103 /*
4105 4104 * We step over breakpoints using Pxecbkpt() in libproc. If a conflicting
4106 4105 * watchpoint is present, we must temporarily remove it before stepping over
4107 4106 * the breakpoint so we do not immediately re-trigger the watchpoint. We know
4108 4107 * the watchpoint has already triggered on our trap instruction as part of
4109 4108 * fetching it. Before we return, we must re-install any disabled watchpoints.
4110 4109 */
4111 4110 static int
4112 4111 pt_brkpt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4113 4112 {
4114 4113 pt_brkpt_t *ptb = sep->se_data;
4115 4114 int status = -1;
4116 4115 int error;
4117 4116 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4118 4117
4119 4118 /*
4120 4119 * If the PC no longer matches our original address, then the user has
4121 4120 * changed it while we have been stopped. In this case, it no longer
4122 4121 * makes any sense to continue over this breakpoint. We return as if we
4123 4122 * continued normally.
4124 4123 */
4125 4124 if ((uintptr_t)psp->pr_info.si_addr != psp->pr_reg[R_PC])
4126 4125 return (pt_status(t, tsp));
4127 4126
4128 4127 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
4129 4128 if (pt_wp_overlap(sep, ptb, WA_EXEC))
4130 4129 (void) Pdelwapt(t->t_pshandle, sep->se_data);
4131 4130 }
4132 4131
4133 4132 if (Pxecbkpt(t->t_pshandle, ptb->ptb_instr) == 0 &&
4134 4133 Pdelbkpt(t->t_pshandle, ptb->ptb_addr, ptb->ptb_instr) == 0)
4135 4134 status = pt_status(t, tsp);
4136 4135
4137 4136 error = errno; /* save errno from Pxecbkpt, Pdelbkpt, or pt_status */
4138 4137
4139 4138 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
4140 4139 if (pt_wp_overlap(sep, ptb, WA_EXEC) &&
4141 4140 Psetwapt(t->t_pshandle, sep->se_data) == -1) {
4142 4141 sep->se_state = MDB_TGT_SPEC_ERROR;
4143 4142 sep->se_errno = errno;
4144 4143 }
4145 4144 }
4146 4145
4147 4146 (void) set_errno(error);
4148 4147 return (status);
4149 4148 }
4150 4149
4151 4150 /*ARGSUSED*/
4152 4151 static int
4153 4152 pt_brkpt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4154 4153 {
4155 4154 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4156 4155 pt_brkpt_t *ptb = sep->se_data;
4157 4156
4158 4157 return (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT &&
4159 4158 psp->pr_reg[R_PC] == ptb->ptb_addr);
4160 4159 }
4161 4160
4162 4161 static const mdb_se_ops_t proc_brkpt_ops = {
4163 4162 pt_brkpt_ctor, /* se_ctor */
4164 4163 pt_brkpt_dtor, /* se_dtor */
4165 4164 pt_brkpt_info, /* se_info */
4166 4165 pt_brkpt_secmp, /* se_secmp */
4167 4166 pt_brkpt_vecmp, /* se_vecmp */
4168 4167 pt_brkpt_arm, /* se_arm */
4169 4168 pt_brkpt_disarm, /* se_disarm */
4170 4169 pt_brkpt_cont, /* se_cont */
4171 4170 pt_brkpt_match /* se_match */
4172 4171 };
4173 4172
4174 4173 static int
4175 4174 pt_wapt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4176 4175 {
4177 4176 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) >= PS_LOST)
4178 4177 return (set_errno(EMDB_NOPROC));
4179 4178
4180 4179 sep->se_data = mdb_alloc(sizeof (prwatch_t), UM_SLEEP);
4181 4180 bcopy(args, sep->se_data, sizeof (prwatch_t));
4182 4181 return (0);
4183 4182 }
4184 4183
4185 4184 /*ARGSUSED*/
4186 4185 static void
4187 4186 pt_wapt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
4188 4187 {
4189 4188 mdb_free(sep->se_data, sizeof (prwatch_t));
4190 4189 }
4191 4190
4192 4191 /*ARGSUSED*/
4193 4192 static char *
4194 4193 pt_wapt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
4195 4194 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
4196 4195 {
4197 4196 prwatch_t *wp = vep != NULL ? vep->ve_args : sep->se_data;
4198 4197 char desc[24];
4199 4198
4200 4199 ASSERT(wp->pr_wflags != 0);
4201 4200 desc[0] = '\0';
4202 4201
4203 4202 switch (wp->pr_wflags) {
4204 4203 case WA_READ:
4205 4204 (void) strcat(desc, "/read");
4206 4205 break;
4207 4206 case WA_WRITE:
4208 4207 (void) strcat(desc, "/write");
4209 4208 break;
4210 4209 case WA_EXEC:
4211 4210 (void) strcat(desc, "/exec");
4212 4211 break;
4213 4212 default:
4214 4213 if (wp->pr_wflags & WA_READ)
4215 4214 (void) strcat(desc, "/r");
4216 4215 if (wp->pr_wflags & WA_WRITE)
4217 4216 (void) strcat(desc, "/w");
4218 4217 if (wp->pr_wflags & WA_EXEC)
4219 4218 (void) strcat(desc, "/x");
4220 4219 }
4221 4220
4222 4221 (void) mdb_iob_snprintf(buf, nbytes, "stop on %s of [%la, %la)",
4223 4222 desc + 1, wp->pr_vaddr, wp->pr_vaddr + wp->pr_size);
4224 4223
4225 4224 sp->spec_base = wp->pr_vaddr;
4226 4225 sp->spec_size = wp->pr_size;
4227 4226
4228 4227 return (buf);
4229 4228 }
4230 4229
4231 4230 /*ARGSUSED*/
4232 4231 static int
4233 4232 pt_wapt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4234 4233 {
4235 4234 prwatch_t *wp1 = sep->se_data;
4236 4235 prwatch_t *wp2 = args;
4237 4236
4238 4237 return (wp1->pr_vaddr == wp2->pr_vaddr &&
4239 4238 wp1->pr_size == wp2->pr_size && wp1->pr_wflags == wp2->pr_wflags);
4240 4239 }
4241 4240
4242 4241 /*ARGSUSED*/
4243 4242 static int
4244 4243 pt_wapt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
4245 4244 {
4246 4245 prwatch_t *wp1 = vep->ve_args;
4247 4246 prwatch_t *wp2 = args;
4248 4247
4249 4248 return (wp1->pr_vaddr == wp2->pr_vaddr &&
4250 4249 wp1->pr_size == wp2->pr_size && wp1->pr_wflags == wp2->pr_wflags);
4251 4250 }
4252 4251
4253 4252 static int
4254 4253 pt_wapt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
4255 4254 {
4256 4255 return (Psetwapt(t->t_pshandle, sep->se_data));
4257 4256 }
4258 4257
4259 4258 static int
4260 4259 pt_wapt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
4261 4260 {
4262 4261 return (Pdelwapt(t->t_pshandle, sep->se_data));
4263 4262 }
4264 4263
4265 4264 /*
4266 4265 * Determine whether the specified sespec is an armed breakpoint at the
4267 4266 * given %pc. We use this to find conflicts with watchpoints below.
4268 4267 */
4269 4268 static int
4270 4269 pt_bp_overlap(mdb_sespec_t *sep, uintptr_t pc)
4271 4270 {
4272 4271 pt_brkpt_t *ptb = sep->se_data;
4273 4272
4274 4273 return (sep->se_state == MDB_TGT_SPEC_ARMED &&
4275 4274 sep->se_ops == &proc_brkpt_ops && ptb->ptb_addr == pc);
4276 4275 }
4277 4276
4278 4277 /*
4279 4278 * We step over watchpoints using Pxecwapt() in libproc. If a conflicting
4280 4279 * breakpoint is present, we must temporarily disarm it before stepping
4281 4280 * over the watchpoint so we do not immediately re-trigger the breakpoint.
4282 4281 * This is similar to the case handled in pt_brkpt_cont(), above.
4283 4282 */
4284 4283 static int
4285 4284 pt_wapt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4286 4285 {
4287 4286 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4288 4287 mdb_sespec_t *bep = NULL;
4289 4288 int status = -1;
4290 4289 int error;
4291 4290
4292 4291 /*
4293 4292 * If the PC no longer matches our original address, then the user has
4294 4293 * changed it while we have been stopped. In this case, it no longer
4295 4294 * makes any sense to continue over this instruction. We return as if
4296 4295 * we continued normally.
4297 4296 */
4298 4297 if ((uintptr_t)psp->pr_info.si_pc != psp->pr_reg[R_PC])
4299 4298 return (pt_status(t, tsp));
4300 4299
4301 4300 if (psp->pr_info.si_code != TRAP_XWATCH) {
4302 4301 for (bep = mdb_list_next(&t->t_active); bep != NULL;
4303 4302 bep = mdb_list_next(bep)) {
4304 4303 if (pt_bp_overlap(bep, psp->pr_reg[R_PC])) {
4305 4304 (void) bep->se_ops->se_disarm(t, bep);
4306 4305 bep->se_state = MDB_TGT_SPEC_ACTIVE;
4307 4306 break;
4308 4307 }
4309 4308 }
4310 4309 }
4311 4310
4312 4311 if (Pxecwapt(t->t_pshandle, sep->se_data) == 0)
4313 4312 status = pt_status(t, tsp);
4314 4313
4315 4314 error = errno; /* save errno from Pxecwapt or pt_status */
4316 4315
4317 4316 if (bep != NULL)
4318 4317 mdb_tgt_sespec_arm_one(t, bep);
4319 4318
4320 4319 (void) set_errno(error);
4321 4320 return (status);
4322 4321 }
4323 4322
4324 4323 /*ARGSUSED*/
4325 4324 static int
4326 4325 pt_wapt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4327 4326 {
4328 4327 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4329 4328 prwatch_t *wp = sep->se_data;
4330 4329
4331 4330 return (psp->pr_why == PR_FAULTED && psp->pr_what == FLTWATCH &&
4332 4331 (uintptr_t)psp->pr_info.si_addr - wp->pr_vaddr < wp->pr_size);
4333 4332 }
4334 4333
4335 4334 static const mdb_se_ops_t proc_wapt_ops = {
4336 4335 pt_wapt_ctor, /* se_ctor */
4337 4336 pt_wapt_dtor, /* se_dtor */
4338 4337 pt_wapt_info, /* se_info */
4339 4338 pt_wapt_secmp, /* se_secmp */
4340 4339 pt_wapt_vecmp, /* se_vecmp */
4341 4340 pt_wapt_arm, /* se_arm */
4342 4341 pt_wapt_disarm, /* se_disarm */
4343 4342 pt_wapt_cont, /* se_cont */
4344 4343 pt_wapt_match /* se_match */
4345 4344 };
4346 4345
4347 4346 static void
4348 4347 pt_bparg_dtor(mdb_vespec_t *vep)
4349 4348 {
4350 4349 pt_bparg_t *pta = vep->ve_args;
4351 4350
4352 4351 if (pta->pta_symbol != NULL)
4353 4352 strfree(pta->pta_symbol);
4354 4353
4355 4354 mdb_free(pta, sizeof (pt_bparg_t));
4356 4355 }
4357 4356
4358 4357 static int
4359 4358 pt_add_vbrkpt(mdb_tgt_t *t, uintptr_t addr,
4360 4359 int spec_flags, mdb_tgt_se_f *func, void *data)
4361 4360 {
4362 4361 pt_bparg_t *pta = mdb_alloc(sizeof (pt_bparg_t), UM_SLEEP);
4363 4362
4364 4363 pta->pta_symbol = NULL;
4365 4364 pta->pta_addr = addr;
4366 4365
4367 4366 return (mdb_tgt_vespec_insert(t, &proc_brkpt_ops, spec_flags,
4368 4367 func, data, pta, pt_bparg_dtor));
4369 4368 }
4370 4369
4371 4370 static int
4372 4371 pt_add_sbrkpt(mdb_tgt_t *t, const char *sym,
4373 4372 int spec_flags, mdb_tgt_se_f *func, void *data)
4374 4373 {
4375 4374 pt_bparg_t *pta;
4376 4375
4377 4376 if (sym[0] == '`') {
4378 4377 (void) set_errno(EMDB_NOOBJ);
4379 4378 return (0);
4380 4379 }
4381 4380
4382 4381 if (sym[strlen(sym) - 1] == '`') {
4383 4382 (void) set_errno(EMDB_NOSYM);
4384 4383 return (0);
4385 4384 }
4386 4385
4387 4386 pta = mdb_alloc(sizeof (pt_bparg_t), UM_SLEEP);
4388 4387 pta->pta_symbol = strdup(sym);
4389 4388 pta->pta_addr = NULL;
4390 4389
4391 4390 return (mdb_tgt_vespec_insert(t, &proc_brkpt_ops, spec_flags,
4392 4391 func, data, pta, pt_bparg_dtor));
4393 4392 }
4394 4393
4395 4394 static int
4396 4395 pt_wparg_overlap(const prwatch_t *wp1, const prwatch_t *wp2)
4397 4396 {
4398 4397 if (wp2->pr_vaddr + wp2->pr_size <= wp1->pr_vaddr)
4399 4398 return (0); /* no range overlap */
4400 4399
4401 4400 if (wp1->pr_vaddr + wp1->pr_size <= wp2->pr_vaddr)
4402 4401 return (0); /* no range overlap */
4403 4402
4404 4403 return (wp1->pr_vaddr != wp2->pr_vaddr ||
4405 4404 wp1->pr_size != wp2->pr_size || wp1->pr_wflags != wp2->pr_wflags);
4406 4405 }
4407 4406
4408 4407 static void
4409 4408 pt_wparg_dtor(mdb_vespec_t *vep)
4410 4409 {
4411 4410 mdb_free(vep->ve_args, sizeof (prwatch_t));
4412 4411 }
4413 4412
4414 4413 static int
4415 4414 pt_add_vwapt(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags,
4416 4415 int spec_flags, mdb_tgt_se_f *func, void *data)
4417 4416 {
4418 4417 prwatch_t *wp = mdb_alloc(sizeof (prwatch_t), UM_SLEEP);
4419 4418 mdb_sespec_t *sep;
4420 4419
4421 4420 wp->pr_vaddr = addr;
4422 4421 wp->pr_size = len;
4423 4422 wp->pr_wflags = 0;
4424 4423
4425 4424 if (wflags & MDB_TGT_WA_R)
4426 4425 wp->pr_wflags |= WA_READ;
4427 4426 if (wflags & MDB_TGT_WA_W)
4428 4427 wp->pr_wflags |= WA_WRITE;
4429 4428 if (wflags & MDB_TGT_WA_X)
4430 4429 wp->pr_wflags |= WA_EXEC;
4431 4430
4432 4431 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
4433 4432 if (sep->se_ops == &proc_wapt_ops &&
4434 4433 mdb_list_next(&sep->se_velist) != NULL &&
4435 4434 pt_wparg_overlap(wp, sep->se_data))
4436 4435 goto dup;
4437 4436 }
4438 4437
4439 4438 for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) {
4440 4439 if (sep->se_ops == &proc_wapt_ops && pt_wparg_overlap(wp,
4441 4440 ((mdb_vespec_t *)mdb_list_next(&sep->se_velist))->ve_args))
4442 4441 goto dup;
4443 4442 }
4444 4443
4445 4444 return (mdb_tgt_vespec_insert(t, &proc_wapt_ops, spec_flags,
4446 4445 func, data, wp, pt_wparg_dtor));
4447 4446
4448 4447 dup:
4449 4448 mdb_free(wp, sizeof (prwatch_t));
4450 4449 (void) set_errno(EMDB_WPDUP);
4451 4450 return (0);
4452 4451 }
4453 4452
4454 4453 static int
4455 4454 pt_add_sysenter(mdb_tgt_t *t, int sysnum,
4456 4455 int spec_flags, mdb_tgt_se_f *func, void *data)
4457 4456 {
4458 4457 if (sysnum <= 0 || sysnum > PRMAXSYS) {
4459 4458 (void) set_errno(EMDB_BADSYSNUM);
4460 4459 return (0);
4461 4460 }
4462 4461
4463 4462 return (mdb_tgt_vespec_insert(t, &proc_sysenter_ops, spec_flags,
4464 4463 func, data, (void *)(uintptr_t)sysnum, no_ve_dtor));
4465 4464 }
4466 4465
4467 4466 static int
4468 4467 pt_add_sysexit(mdb_tgt_t *t, int sysnum,
4469 4468 int spec_flags, mdb_tgt_se_f *func, void *data)
4470 4469 {
4471 4470 if (sysnum <= 0 || sysnum > PRMAXSYS) {
4472 4471 (void) set_errno(EMDB_BADSYSNUM);
4473 4472 return (0);
4474 4473 }
4475 4474
4476 4475 return (mdb_tgt_vespec_insert(t, &proc_sysexit_ops, spec_flags,
4477 4476 func, data, (void *)(uintptr_t)sysnum, no_ve_dtor));
4478 4477 }
4479 4478
4480 4479 static int
4481 4480 pt_add_signal(mdb_tgt_t *t, int signum,
4482 4481 int spec_flags, mdb_tgt_se_f *func, void *data)
4483 4482 {
4484 4483 pt_data_t *pt = t->t_data;
4485 4484
4486 4485 if (signum <= 0 || signum > pt->p_maxsig) {
4487 4486 (void) set_errno(EMDB_BADSIGNUM);
4488 4487 return (0);
4489 4488 }
4490 4489
4491 4490 return (mdb_tgt_vespec_insert(t, &proc_signal_ops, spec_flags,
4492 4491 func, data, (void *)(uintptr_t)signum, no_ve_dtor));
4493 4492 }
4494 4493
4495 4494 static int
4496 4495 pt_add_fault(mdb_tgt_t *t, int fltnum,
4497 4496 int spec_flags, mdb_tgt_se_f *func, void *data)
4498 4497 {
4499 4498 if (fltnum <= 0 || fltnum > PRMAXFAULT) {
4500 4499 (void) set_errno(EMDB_BADFLTNUM);
4501 4500 return (0);
4502 4501 }
4503 4502
4504 4503 return (mdb_tgt_vespec_insert(t, &proc_fault_ops, spec_flags,
4505 4504 func, data, (void *)(uintptr_t)fltnum, no_ve_dtor));
4506 4505 }
4507 4506
4508 4507 static int
4509 4508 pt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid,
4510 4509 const char *rname, mdb_tgt_reg_t *rp)
4511 4510 {
4512 4511 pt_data_t *pt = t->t_data;
4513 4512 prgregset_t grs;
4514 4513 mdb_var_t *v;
4515 4514
4516 4515 if (t->t_pshandle == NULL)
4517 4516 return (set_errno(EMDB_NOPROC));
4518 4517
4519 4518 if ((v = mdb_nv_lookup(&pt->p_regs, rname)) != NULL) {
4520 4519 uintmax_t rd_nval = mdb_nv_get_value(v);
4521 4520 ushort_t rd_num = MDB_TGT_R_NUM(rd_nval);
4522 4521 ushort_t rd_flags = MDB_TGT_R_FLAGS(rd_nval);
4523 4522
4524 4523 if (!MDB_TGT_R_IS_FP(rd_flags)) {
4525 4524 mdb_tgt_reg_t r = 0;
4526 4525
4527 4526 #if defined(__sparc) && defined(_ILP32)
4528 4527 /*
4529 4528 * If we are debugging on 32-bit SPARC, the globals and
4530 4529 * outs can have 32 upper bits hiding in the xregs.
4531 4530 */
4532 4531 /* gcc doesn't like >= R_G0 because R_G0 == 0 */
4533 4532 int is_g = (rd_num == R_G0 ||
4534 4533 rd_num >= R_G1 && rd_num <= R_G7);
4535 4534 int is_o = (rd_num >= R_O0 && rd_num <= R_O7);
4536 4535 prxregset_t xrs;
4537 4536
4538 4537 if (is_g && PTL_GETXREGS(t, tid, &xrs) == 0 &&
4539 4538 xrs.pr_type == XR_TYPE_V8P) {
4540 4539 r |= (uint64_t)xrs.pr_un.pr_v8p.pr_xg[
4541 4540 rd_num - R_G0 + XR_G0] << 32;
4542 4541 }
4543 4542
4544 4543 if (is_o && PTL_GETXREGS(t, tid, &xrs) == 0 &&
4545 4544 xrs.pr_type == XR_TYPE_V8P) {
4546 4545 r |= (uint64_t)xrs.pr_un.pr_v8p.pr_xo[
4547 4546 rd_num - R_O0 + XR_O0] << 32;
4548 4547 }
4549 4548 #endif /* __sparc && _ILP32 */
4550 4549
4551 4550 /*
4552 4551 * Avoid sign-extension by casting: recall that procfs
4553 4552 * defines prgreg_t as a long or int and our native
4554 4553 * register handling uses uint64_t's.
4555 4554 */
4556 4555 if (PTL_GETREGS(t, tid, grs) == 0) {
4557 4556 *rp = r | (ulong_t)grs[rd_num];
4558 4557 if (rd_flags & MDB_TGT_R_32)
4559 4558 *rp &= 0xffffffffULL;
4560 4559 else if (rd_flags & MDB_TGT_R_16)
4561 4560 *rp &= 0xffffULL;
4562 4561 else if (rd_flags & MDB_TGT_R_8H)
4563 4562 *rp = (*rp & 0xff00ULL) >> 8;
4564 4563 else if (rd_flags & MDB_TGT_R_8L)
4565 4564 *rp &= 0xffULL;
4566 4565 return (0);
4567 4566 }
4568 4567 return (-1);
4569 4568 } else
4570 4569 return (pt_getfpreg(t, tid, rd_num, rd_flags, rp));
4571 4570 }
4572 4571
4573 4572 return (set_errno(EMDB_BADREG));
4574 4573 }
4575 4574
4576 4575 static int
4577 4576 pt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, mdb_tgt_reg_t r)
4578 4577 {
4579 4578 pt_data_t *pt = t->t_data;
4580 4579 prgregset_t grs;
4581 4580 mdb_var_t *v;
4582 4581
4583 4582 if (t->t_pshandle == NULL)
4584 4583 return (set_errno(EMDB_NOPROC));
4585 4584
4586 4585 if ((v = mdb_nv_lookup(&pt->p_regs, rname)) != NULL) {
4587 4586 uintmax_t rd_nval = mdb_nv_get_value(v);
4588 4587 ushort_t rd_num = MDB_TGT_R_NUM(rd_nval);
4589 4588 ushort_t rd_flags = MDB_TGT_R_FLAGS(rd_nval);
4590 4589
4591 4590 if (!MDB_TGT_R_IS_FP(rd_flags)) {
4592 4591
4593 4592 if (rd_flags & MDB_TGT_R_32)
4594 4593 r &= 0xffffffffULL;
4595 4594 else if (rd_flags & MDB_TGT_R_16)
4596 4595 r &= 0xffffULL;
4597 4596 else if (rd_flags & MDB_TGT_R_8H)
4598 4597 r = (r & 0xffULL) << 8;
4599 4598 else if (rd_flags & MDB_TGT_R_8L)
4600 4599 r &= 0xffULL;
4601 4600
4602 4601 #if defined(__sparc) && defined(_ILP32)
4603 4602 /*
4604 4603 * If we are debugging on 32-bit SPARC, the globals and
4605 4604 * outs can have 32 upper bits stored in the xregs.
4606 4605 */
4607 4606 int is_g = (rd_num == R_G0 ||
4608 4607 rd_num >= R_G1 && rd_num <= R_G7);
4609 4608 int is_o = (rd_num >= R_O0 && rd_num <= R_O7);
4610 4609 prxregset_t xrs;
4611 4610
4612 4611 if ((is_g || is_o) && PTL_GETXREGS(t, tid, &xrs) == 0 &&
4613 4612 xrs.pr_type == XR_TYPE_V8P) {
4614 4613 if (is_g) {
4615 4614 xrs.pr_un.pr_v8p.pr_xg[rd_num -
4616 4615 R_G0 + XR_G0] = (uint32_t)(r >> 32);
4617 4616 } else if (is_o) {
4618 4617 xrs.pr_un.pr_v8p.pr_xo[rd_num -
4619 4618 R_O0 + XR_O0] = (uint32_t)(r >> 32);
4620 4619 }
4621 4620
4622 4621 if (PTL_SETXREGS(t, tid, &xrs) == -1)
4623 4622 return (-1);
4624 4623 }
4625 4624 #endif /* __sparc && _ILP32 */
4626 4625
4627 4626 if (PTL_GETREGS(t, tid, grs) == 0) {
4628 4627 grs[rd_num] = (prgreg_t)r;
4629 4628 return (PTL_SETREGS(t, tid, grs));
4630 4629 }
4631 4630 return (-1);
4632 4631 } else
4633 4632 return (pt_putfpreg(t, tid, rd_num, rd_flags, r));
4634 4633 }
4635 4634
4636 4635 return (set_errno(EMDB_BADREG));
4637 4636 }
4638 4637
4639 4638 static int
4640 4639 pt_stack_call(pt_stkarg_t *psp, const prgregset_t grs, uint_t argc, long *argv)
4641 4640 {
4642 4641 psp->pstk_gotpc |= (grs[R_PC] != 0);
4643 4642
4644 4643 if (!psp->pstk_gotpc)
4645 4644 return (0); /* skip initial zeroed frames */
4646 4645
4647 4646 return (psp->pstk_func(psp->pstk_private, grs[R_PC],
4648 4647 argc, argv, (const struct mdb_tgt_gregset *)grs));
4649 4648 }
4650 4649
4651 4650 static int
4652 4651 pt_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
4653 4652 mdb_tgt_stack_f *func, void *arg)
4654 4653 {
4655 4654 if (t->t_pshandle != NULL) {
4656 4655 pt_stkarg_t pstk;
4657 4656
4658 4657 pstk.pstk_func = func;
4659 4658 pstk.pstk_private = arg;
4660 4659 pstk.pstk_gotpc = FALSE;
4661 4660
4662 4661 (void) Pstack_iter(t->t_pshandle, gsp->gregs,
4663 4662 (proc_stack_f *)pt_stack_call, &pstk);
4664 4663
4665 4664 return (0);
4666 4665 }
4667 4666
4668 4667 return (set_errno(EMDB_NOPROC));
4669 4668 }
4670 4669
4671 4670 static int
4672 4671 pt_auxv(mdb_tgt_t *t, const auxv_t **auxvp)
4673 4672 {
4674 4673 if (t->t_pshandle != NULL) {
4675 4674 *auxvp = Pgetauxvec(t->t_pshandle);
4676 4675 return (0);
4677 4676 }
4678 4677
4679 4678 return (set_errno(EMDB_NOPROC));
4680 4679 }
4681 4680
4682 4681
4683 4682 static const mdb_tgt_ops_t proc_ops = {
4684 4683 pt_setflags, /* t_setflags */
4685 4684 (int (*)()) mdb_tgt_notsup, /* t_setcontext */
4686 4685 pt_activate, /* t_activate */
4687 4686 pt_deactivate, /* t_deactivate */
4688 4687 pt_periodic, /* t_periodic */
4689 4688 pt_destroy, /* t_destroy */
4690 4689 pt_name, /* t_name */
4691 4690 (const char *(*)()) mdb_conf_isa, /* t_isa */
4692 4691 pt_platform, /* t_platform */
4693 4692 pt_uname, /* t_uname */
4694 4693 pt_dmodel, /* t_dmodel */
4695 4694 (ssize_t (*)()) mdb_tgt_notsup, /* t_aread */
4696 4695 (ssize_t (*)()) mdb_tgt_notsup, /* t_awrite */
4697 4696 pt_vread, /* t_vread */
4698 4697 pt_vwrite, /* t_vwrite */
4699 4698 (ssize_t (*)()) mdb_tgt_notsup, /* t_pread */
4700 4699 (ssize_t (*)()) mdb_tgt_notsup, /* t_pwrite */
4701 4700 pt_fread, /* t_fread */
4702 4701 pt_fwrite, /* t_fwrite */
4703 4702 (ssize_t (*)()) mdb_tgt_notsup, /* t_ioread */
4704 4703 (ssize_t (*)()) mdb_tgt_notsup, /* t_iowrite */
4705 4704 (int (*)()) mdb_tgt_notsup, /* t_vtop */
4706 4705 pt_lookup_by_name, /* t_lookup_by_name */
4707 4706 pt_lookup_by_addr, /* t_lookup_by_addr */
4708 4707 pt_symbol_iter, /* t_symbol_iter */
4709 4708 pt_mapping_iter, /* t_mapping_iter */
4710 4709 pt_object_iter, /* t_object_iter */
4711 4710 pt_addr_to_map, /* t_addr_to_map */
4712 4711 pt_name_to_map, /* t_name_to_map */
4713 4712 pt_addr_to_ctf, /* t_addr_to_ctf */
4714 4713 pt_name_to_ctf, /* t_name_to_ctf */
4715 4714 pt_status, /* t_status */
4716 4715 pt_run, /* t_run */
4717 4716 pt_step, /* t_step */
4718 4717 pt_step_out, /* t_step_out */
4719 4718 pt_next, /* t_next */
4720 4719 pt_continue, /* t_cont */
4721 4720 pt_signal, /* t_signal */
4722 4721 pt_add_vbrkpt, /* t_add_vbrkpt */
4723 4722 pt_add_sbrkpt, /* t_add_sbrkpt */
4724 4723 (int (*)()) mdb_tgt_null, /* t_add_pwapt */
4725 4724 pt_add_vwapt, /* t_add_vwapt */
4726 4725 (int (*)()) mdb_tgt_null, /* t_add_iowapt */
4727 4726 pt_add_sysenter, /* t_add_sysenter */
4728 4727 pt_add_sysexit, /* t_add_sysexit */
4729 4728 pt_add_signal, /* t_add_signal */
4730 4729 pt_add_fault, /* t_add_fault */
4731 4730 pt_getareg, /* t_getareg */
4732 4731 pt_putareg, /* t_putareg */
4733 4732 pt_stack_iter, /* t_stack_iter */
4734 4733 pt_auxv /* t_auxv */
4735 4734 };
4736 4735
4737 4736 /*
4738 4737 * Utility function for converting libproc errno values to mdb error values
4739 4738 * for the ptl calls below. Currently, we only need to convert ENOENT to
4740 4739 * EMDB_NOTHREAD to produce a more useful error message for the user.
4741 4740 */
4742 4741 static int
4743 4742 ptl_err(int error)
4744 4743 {
4745 4744 if (error != 0 && errno == ENOENT)
4746 4745 return (set_errno(EMDB_NOTHREAD));
4747 4746
4748 4747 return (error);
4749 4748 }
4750 4749
4751 4750 /*ARGSUSED*/
4752 4751 static mdb_tgt_tid_t
4753 4752 pt_lwp_tid(mdb_tgt_t *t, void *tap)
4754 4753 {
4755 4754 if (t->t_pshandle != NULL)
4756 4755 return (Pstatus(t->t_pshandle)->pr_lwp.pr_lwpid);
4757 4756
4758 4757 return (set_errno(EMDB_NOPROC));
4759 4758 }
4760 4759
4761 4760 static int
4762 4761 pt_lwp_add(mdb_addrvec_t *ap, const lwpstatus_t *psp)
4763 4762 {
4764 4763 mdb_addrvec_unshift(ap, psp->pr_lwpid);
4765 4764 return (0);
4766 4765 }
4767 4766
4768 4767 /*ARGSUSED*/
4769 4768 static int
4770 4769 pt_lwp_iter(mdb_tgt_t *t, void *tap, mdb_addrvec_t *ap)
4771 4770 {
4772 4771 if (t->t_pshandle != NULL)
4773 4772 return (Plwp_iter(t->t_pshandle, (proc_lwp_f *)pt_lwp_add, ap));
4774 4773
4775 4774 return (set_errno(EMDB_NOPROC));
4776 4775 }
4777 4776
4778 4777 /*ARGSUSED*/
4779 4778 static int
4780 4779 pt_lwp_getregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
4781 4780 {
4782 4781 if (t->t_pshandle != NULL) {
4783 4782 return (ptl_err(Plwp_getregs(t->t_pshandle,
4784 4783 (lwpid_t)tid, gregs)));
4785 4784 }
4786 4785 return (set_errno(EMDB_NOPROC));
4787 4786 }
4788 4787
4789 4788 /*ARGSUSED*/
4790 4789 static int
4791 4790 pt_lwp_setregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
4792 4791 {
4793 4792 if (t->t_pshandle != NULL) {
4794 4793 return (ptl_err(Plwp_setregs(t->t_pshandle,
4795 4794 (lwpid_t)tid, gregs)));
4796 4795 }
4797 4796 return (set_errno(EMDB_NOPROC));
4798 4797 }
4799 4798
4800 4799 #ifdef __sparc
4801 4800
4802 4801 /*ARGSUSED*/
4803 4802 static int
4804 4803 pt_lwp_getxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prxregset_t *xregs)
4805 4804 {
4806 4805 if (t->t_pshandle != NULL) {
4807 4806 return (ptl_err(Plwp_getxregs(t->t_pshandle,
4808 4807 (lwpid_t)tid, xregs)));
4809 4808 }
4810 4809 return (set_errno(EMDB_NOPROC));
4811 4810 }
4812 4811
4813 4812 /*ARGSUSED*/
4814 4813 static int
4815 4814 pt_lwp_setxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
4816 4815 const prxregset_t *xregs)
4817 4816 {
4818 4817 if (t->t_pshandle != NULL) {
4819 4818 return (ptl_err(Plwp_setxregs(t->t_pshandle,
4820 4819 (lwpid_t)tid, xregs)));
4821 4820 }
4822 4821 return (set_errno(EMDB_NOPROC));
4823 4822 }
4824 4823
4825 4824 #endif /* __sparc */
4826 4825
4827 4826 /*ARGSUSED*/
4828 4827 static int
4829 4828 pt_lwp_getfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
4830 4829 prfpregset_t *fpregs)
4831 4830 {
4832 4831 if (t->t_pshandle != NULL) {
4833 4832 return (ptl_err(Plwp_getfpregs(t->t_pshandle,
4834 4833 (lwpid_t)tid, fpregs)));
4835 4834 }
4836 4835 return (set_errno(EMDB_NOPROC));
4837 4836 }
4838 4837
4839 4838 /*ARGSUSED*/
4840 4839 static int
4841 4840 pt_lwp_setfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
4842 4841 const prfpregset_t *fpregs)
4843 4842 {
4844 4843 if (t->t_pshandle != NULL) {
4845 4844 return (ptl_err(Plwp_setfpregs(t->t_pshandle,
4846 4845 (lwpid_t)tid, fpregs)));
4847 4846 }
4848 4847 return (set_errno(EMDB_NOPROC));
4849 4848 }
4850 4849
4851 4850 static const pt_ptl_ops_t proc_lwp_ops = {
4852 4851 (int (*)()) mdb_tgt_nop,
4853 4852 (void (*)()) mdb_tgt_nop,
4854 4853 pt_lwp_tid,
4855 4854 pt_lwp_iter,
4856 4855 pt_lwp_getregs,
4857 4856 pt_lwp_setregs,
4858 4857 #ifdef __sparc
4859 4858 pt_lwp_getxregs,
4860 4859 pt_lwp_setxregs,
4861 4860 #endif
4862 4861 pt_lwp_getfpregs,
4863 4862 pt_lwp_setfpregs
4864 4863 };
4865 4864
4866 4865 static int
4867 4866 pt_tdb_ctor(mdb_tgt_t *t)
4868 4867 {
4869 4868 pt_data_t *pt = t->t_data;
4870 4869 td_thragent_t *tap;
4871 4870 td_err_e err;
4872 4871
4873 4872 if ((err = pt->p_tdb_ops->td_ta_new(t->t_pshandle, &tap)) != TD_OK)
4874 4873 return (set_errno(tdb_to_errno(err)));
4875 4874
4876 4875 pt->p_ptl_hdl = tap;
4877 4876 return (0);
4878 4877 }
4879 4878
4880 4879 static void
4881 4880 pt_tdb_dtor(mdb_tgt_t *t, void *tap)
4882 4881 {
4883 4882 pt_data_t *pt = t->t_data;
4884 4883
4885 4884 ASSERT(tap == pt->p_ptl_hdl);
4886 4885 (void) pt->p_tdb_ops->td_ta_delete(tap);
4887 4886 pt->p_ptl_hdl = NULL;
4888 4887 }
4889 4888
4890 4889 static mdb_tgt_tid_t
4891 4890 pt_tdb_tid(mdb_tgt_t *t, void *tap)
4892 4891 {
4893 4892 pt_data_t *pt = t->t_data;
4894 4893
4895 4894 td_thrhandle_t th;
4896 4895 td_thrinfo_t ti;
4897 4896 td_err_e err;
4898 4897
4899 4898 if (t->t_pshandle == NULL)
4900 4899 return (set_errno(EMDB_NOPROC));
4901 4900
4902 4901 if ((err = pt->p_tdb_ops->td_ta_map_lwp2thr(tap,
4903 4902 Pstatus(t->t_pshandle)->pr_lwp.pr_lwpid, &th)) != TD_OK)
4904 4903 return (set_errno(tdb_to_errno(err)));
4905 4904
4906 4905 if ((err = pt->p_tdb_ops->td_thr_get_info(&th, &ti)) != TD_OK)
4907 4906 return (set_errno(tdb_to_errno(err)));
4908 4907
4909 4908 return (ti.ti_tid);
4910 4909 }
4911 4910
4912 4911 static int
4913 4912 pt_tdb_add(const td_thrhandle_t *thp, pt_addarg_t *pap)
4914 4913 {
4915 4914 td_thrinfo_t ti;
4916 4915
4917 4916 if (pap->pa_pt->p_tdb_ops->td_thr_get_info(thp, &ti) == TD_OK &&
4918 4917 ti.ti_state != TD_THR_ZOMBIE)
4919 4918 mdb_addrvec_unshift(pap->pa_ap, ti.ti_tid);
4920 4919
4921 4920 return (0);
4922 4921 }
4923 4922
4924 4923 static int
4925 4924 pt_tdb_iter(mdb_tgt_t *t, void *tap, mdb_addrvec_t *ap)
4926 4925 {
4927 4926 pt_data_t *pt = t->t_data;
4928 4927 pt_addarg_t arg;
4929 4928 int err;
4930 4929
4931 4930 if (t->t_pshandle == NULL)
4932 4931 return (set_errno(EMDB_NOPROC));
4933 4932
4934 4933 arg.pa_pt = pt;
4935 4934 arg.pa_ap = ap;
4936 4935
4937 4936 if ((err = pt->p_tdb_ops->td_ta_thr_iter(tap, (td_thr_iter_f *)
4938 4937 pt_tdb_add, &arg, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
4939 4938 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS)) != TD_OK)
4940 4939 return (set_errno(tdb_to_errno(err)));
4941 4940
4942 4941 return (0);
4943 4942 }
4944 4943
4945 4944 static int
4946 4945 pt_tdb_getregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
4947 4946 {
4948 4947 pt_data_t *pt = t->t_data;
4949 4948
4950 4949 td_thrhandle_t th;
4951 4950 td_err_e err;
4952 4951
4953 4952 if (t->t_pshandle == NULL)
4954 4953 return (set_errno(EMDB_NOPROC));
4955 4954
4956 4955 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
4957 4956 return (set_errno(tdb_to_errno(err)));
4958 4957
4959 4958 err = pt->p_tdb_ops->td_thr_getgregs(&th, gregs);
4960 4959 if (err != TD_OK && err != TD_PARTIALREG)
4961 4960 return (set_errno(tdb_to_errno(err)));
4962 4961
4963 4962 return (0);
4964 4963 }
4965 4964
4966 4965 static int
4967 4966 pt_tdb_setregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
4968 4967 {
4969 4968 pt_data_t *pt = t->t_data;
4970 4969
4971 4970 td_thrhandle_t th;
4972 4971 td_err_e err;
4973 4972
4974 4973 if (t->t_pshandle == NULL)
4975 4974 return (set_errno(EMDB_NOPROC));
4976 4975
4977 4976 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
4978 4977 return (set_errno(tdb_to_errno(err)));
4979 4978
4980 4979 err = pt->p_tdb_ops->td_thr_setgregs(&th, gregs);
4981 4980 if (err != TD_OK && err != TD_PARTIALREG)
4982 4981 return (set_errno(tdb_to_errno(err)));
4983 4982
4984 4983 return (0);
4985 4984 }
4986 4985
4987 4986 #ifdef __sparc
4988 4987
4989 4988 static int
4990 4989 pt_tdb_getxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prxregset_t *xregs)
4991 4990 {
4992 4991 pt_data_t *pt = t->t_data;
4993 4992
4994 4993 td_thrhandle_t th;
4995 4994 td_err_e err;
4996 4995
4997 4996 if (t->t_pshandle == NULL)
4998 4997 return (set_errno(EMDB_NOPROC));
4999 4998
5000 4999 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5001 5000 return (set_errno(tdb_to_errno(err)));
5002 5001
5003 5002 err = pt->p_tdb_ops->td_thr_getxregs(&th, xregs);
5004 5003 if (err != TD_OK && err != TD_PARTIALREG)
5005 5004 return (set_errno(tdb_to_errno(err)));
5006 5005
5007 5006 return (0);
5008 5007 }
5009 5008
5010 5009 static int
5011 5010 pt_tdb_setxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
5012 5011 const prxregset_t *xregs)
5013 5012 {
5014 5013 pt_data_t *pt = t->t_data;
5015 5014
5016 5015 td_thrhandle_t th;
5017 5016 td_err_e err;
5018 5017
5019 5018 if (t->t_pshandle == NULL)
5020 5019 return (set_errno(EMDB_NOPROC));
5021 5020
5022 5021 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5023 5022 return (set_errno(tdb_to_errno(err)));
5024 5023
5025 5024 err = pt->p_tdb_ops->td_thr_setxregs(&th, xregs);
5026 5025 if (err != TD_OK && err != TD_PARTIALREG)
5027 5026 return (set_errno(tdb_to_errno(err)));
5028 5027
5029 5028 return (0);
5030 5029 }
5031 5030
5032 5031 #endif /* __sparc */
5033 5032
5034 5033 static int
5035 5034 pt_tdb_getfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
5036 5035 prfpregset_t *fpregs)
5037 5036 {
5038 5037 pt_data_t *pt = t->t_data;
5039 5038
5040 5039 td_thrhandle_t th;
5041 5040 td_err_e err;
5042 5041
5043 5042 if (t->t_pshandle == NULL)
5044 5043 return (set_errno(EMDB_NOPROC));
5045 5044
5046 5045 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5047 5046 return (set_errno(tdb_to_errno(err)));
5048 5047
5049 5048 err = pt->p_tdb_ops->td_thr_getfpregs(&th, fpregs);
5050 5049 if (err != TD_OK && err != TD_PARTIALREG)
5051 5050 return (set_errno(tdb_to_errno(err)));
5052 5051
5053 5052 return (0);
5054 5053 }
5055 5054
5056 5055 static int
5057 5056 pt_tdb_setfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
5058 5057 const prfpregset_t *fpregs)
5059 5058 {
5060 5059 pt_data_t *pt = t->t_data;
5061 5060
5062 5061 td_thrhandle_t th;
5063 5062 td_err_e err;
5064 5063
5065 5064 if (t->t_pshandle == NULL)
5066 5065 return (set_errno(EMDB_NOPROC));
5067 5066
5068 5067 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5069 5068 return (set_errno(tdb_to_errno(err)));
5070 5069
5071 5070 err = pt->p_tdb_ops->td_thr_setfpregs(&th, fpregs);
5072 5071 if (err != TD_OK && err != TD_PARTIALREG)
5073 5072 return (set_errno(tdb_to_errno(err)));
5074 5073
5075 5074 return (0);
5076 5075 }
5077 5076
5078 5077 static const pt_ptl_ops_t proc_tdb_ops = {
5079 5078 pt_tdb_ctor,
5080 5079 pt_tdb_dtor,
5081 5080 pt_tdb_tid,
5082 5081 pt_tdb_iter,
5083 5082 pt_tdb_getregs,
5084 5083 pt_tdb_setregs,
5085 5084 #ifdef __sparc
5086 5085 pt_tdb_getxregs,
5087 5086 pt_tdb_setxregs,
5088 5087 #endif
5089 5088 pt_tdb_getfpregs,
5090 5089 pt_tdb_setfpregs
5091 5090 };
5092 5091
5093 5092 static ssize_t
5094 5093 pt_xd_auxv(mdb_tgt_t *t, void *buf, size_t nbytes)
5095 5094 {
5096 5095 struct ps_prochandle *P = t->t_pshandle;
5097 5096 const auxv_t *auxp, *auxv = NULL;
5098 5097 int auxn = 0;
5099 5098
5100 5099 if (P != NULL && (auxv = Pgetauxvec(P)) != NULL &&
5101 5100 auxv->a_type != AT_NULL) {
5102 5101 for (auxp = auxv, auxn = 1; auxp->a_type != NULL; auxp++)
5103 5102 auxn++;
5104 5103 }
5105 5104
5106 5105 if (buf == NULL && nbytes == 0)
5107 5106 return (sizeof (auxv_t) * auxn);
5108 5107
5109 5108 if (auxn == 0)
5110 5109 return (set_errno(ENODATA));
5111 5110
5112 5111 nbytes = MIN(nbytes, sizeof (auxv_t) * auxn);
5113 5112 bcopy(auxv, buf, nbytes);
5114 5113 return (nbytes);
5115 5114 }
5116 5115
5117 5116 static ssize_t
5118 5117 pt_xd_cred(mdb_tgt_t *t, void *buf, size_t nbytes)
5119 5118 {
5120 5119 prcred_t cr, *crp;
5121 5120 size_t cbytes = 0;
5122 5121
5123 5122 if (t->t_pshandle != NULL && Pcred(t->t_pshandle, &cr, 1) == 0) {
5124 5123 cbytes = (cr.pr_ngroups <= 1) ? sizeof (prcred_t) :
5125 5124 (sizeof (prcred_t) + (cr.pr_ngroups - 1) * sizeof (gid_t));
5126 5125 }
5127 5126
5128 5127 if (buf == NULL && nbytes == 0)
5129 5128 return (cbytes);
5130 5129
5131 5130 if (cbytes == 0)
5132 5131 return (set_errno(ENODATA));
5133 5132
5134 5133 crp = mdb_alloc(cbytes, UM_SLEEP);
5135 5134
5136 5135 if (Pcred(t->t_pshandle, crp, cr.pr_ngroups) == -1)
5137 5136 return (set_errno(ENODATA));
5138 5137
5139 5138 nbytes = MIN(nbytes, cbytes);
5140 5139 bcopy(crp, buf, nbytes);
5141 5140 mdb_free(crp, cbytes);
5142 5141 return (nbytes);
5143 5142 }
5144 5143
5145 5144 static ssize_t
5146 5145 pt_xd_ehdr(mdb_tgt_t *t, void *buf, size_t nbytes)
5147 5146 {
5148 5147 pt_data_t *pt = t->t_data;
5149 5148
5150 5149 if (buf == NULL && nbytes == 0)
5151 5150 return (sizeof (GElf_Ehdr));
5152 5151
5153 5152 if (pt->p_file == NULL)
5154 5153 return (set_errno(ENODATA));
5155 5154
5156 5155 nbytes = MIN(nbytes, sizeof (GElf_Ehdr));
5157 5156 bcopy(&pt->p_file->gf_ehdr, buf, nbytes);
5158 5157 return (nbytes);
5159 5158 }
5160 5159
5161 5160 static int
5162 5161 pt_copy_lwp(lwpstatus_t **lspp, const lwpstatus_t *lsp)
5163 5162 {
5164 5163 bcopy(lsp, *lspp, sizeof (lwpstatus_t));
5165 5164 (*lspp)++;
5166 5165 return (0);
5167 5166 }
5168 5167
5169 5168 static ssize_t
5170 5169 pt_xd_lwpstatus(mdb_tgt_t *t, void *buf, size_t nbytes)
5171 5170 {
5172 5171 lwpstatus_t *lsp, *lbuf;
5173 5172 const pstatus_t *psp;
5174 5173 int nlwp = 0;
5175 5174
5176 5175 if (t->t_pshandle != NULL && (psp = Pstatus(t->t_pshandle)) != NULL)
5177 5176 nlwp = psp->pr_nlwp;
5178 5177
5179 5178 if (buf == NULL && nbytes == 0)
5180 5179 return (sizeof (lwpstatus_t) * nlwp);
5181 5180
5182 5181 if (nlwp == 0)
5183 5182 return (set_errno(ENODATA));
5184 5183
5185 5184 lsp = lbuf = mdb_alloc(sizeof (lwpstatus_t) * nlwp, UM_SLEEP);
5186 5185 nbytes = MIN(nbytes, sizeof (lwpstatus_t) * nlwp);
5187 5186
5188 5187 (void) Plwp_iter(t->t_pshandle, (proc_lwp_f *)pt_copy_lwp, &lsp);
5189 5188 bcopy(lbuf, buf, nbytes);
5190 5189
5191 5190 mdb_free(lbuf, sizeof (lwpstatus_t) * nlwp);
5192 5191 return (nbytes);
5193 5192 }
5194 5193
5195 5194 static ssize_t
5196 5195 pt_xd_pshandle(mdb_tgt_t *t, void *buf, size_t nbytes)
5197 5196 {
5198 5197 if (buf == NULL && nbytes == 0)
5199 5198 return (sizeof (struct ps_prochandle *));
5200 5199
5201 5200 if (t->t_pshandle == NULL || nbytes != sizeof (struct ps_prochandle *))
5202 5201 return (set_errno(ENODATA));
5203 5202
5204 5203 bcopy(&t->t_pshandle, buf, nbytes);
5205 5204 return (nbytes);
5206 5205 }
5207 5206
5208 5207 static ssize_t
5209 5208 pt_xd_psinfo(mdb_tgt_t *t, void *buf, size_t nbytes)
5210 5209 {
5211 5210 const psinfo_t *psp;
5212 5211
5213 5212 if (buf == NULL && nbytes == 0)
5214 5213 return (sizeof (psinfo_t));
5215 5214
5216 5215 if (t->t_pshandle == NULL || (psp = Ppsinfo(t->t_pshandle)) == NULL)
5217 5216 return (set_errno(ENODATA));
5218 5217
5219 5218 nbytes = MIN(nbytes, sizeof (psinfo_t));
5220 5219 bcopy(psp, buf, nbytes);
5221 5220 return (nbytes);
5222 5221 }
5223 5222
5224 5223 static ssize_t
5225 5224 pt_xd_pstatus(mdb_tgt_t *t, void *buf, size_t nbytes)
5226 5225 {
5227 5226 const pstatus_t *psp;
5228 5227
5229 5228 if (buf == NULL && nbytes == 0)
5230 5229 return (sizeof (pstatus_t));
5231 5230
5232 5231 if (t->t_pshandle == NULL || (psp = Pstatus(t->t_pshandle)) == NULL)
5233 5232 return (set_errno(ENODATA));
5234 5233
5235 5234 nbytes = MIN(nbytes, sizeof (pstatus_t));
5236 5235 bcopy(psp, buf, nbytes);
5237 5236 return (nbytes);
5238 5237 }
5239 5238
5240 5239 static ssize_t
5241 5240 pt_xd_utsname(mdb_tgt_t *t, void *buf, size_t nbytes)
5242 5241 {
5243 5242 struct utsname uts;
5244 5243
5245 5244 if (buf == NULL && nbytes == 0)
5246 5245 return (sizeof (struct utsname));
5247 5246
5248 5247 if (t->t_pshandle == NULL || Puname(t->t_pshandle, &uts) != 0)
5249 5248 return (set_errno(ENODATA));
5250 5249
5251 5250 nbytes = MIN(nbytes, sizeof (struct utsname));
5252 5251 bcopy(&uts, buf, nbytes);
5253 5252 return (nbytes);
5254 5253 }
5255 5254
5256 5255 int
5257 5256 mdb_proc_tgt_create(mdb_tgt_t *t, int argc, const char *argv[])
5258 5257 {
5259 5258 pt_data_t *pt = mdb_zalloc(sizeof (pt_data_t), UM_SLEEP);
5260 5259
5261 5260 const char *aout_path = argc > 0 ? argv[0] : PT_EXEC_PATH;
5262 5261 const char *core_path = argc > 1 ? argv[1] : NULL;
5263 5262
5264 5263 const mdb_tgt_regdesc_t *rdp;
5265 5264 char execname[MAXPATHLEN];
5266 5265 struct stat64 st;
5267 5266 int perr;
5268 5267 int state;
5269 5268 struct rlimit rlim;
5270 5269 int i;
5271 5270
5272 5271 if (argc > 2) {
5273 5272 mdb_free(pt, sizeof (pt_data_t));
5274 5273 return (set_errno(EINVAL));
5275 5274 }
5276 5275
5277 5276 if (t->t_flags & MDB_TGT_F_RDWR)
5278 5277 pt->p_oflags = O_RDWR;
5279 5278 else
5280 5279 pt->p_oflags = O_RDONLY;
5281 5280
5282 5281 if (t->t_flags & MDB_TGT_F_FORCE)
5283 5282 pt->p_gflags |= PGRAB_FORCE;
5284 5283 if (t->t_flags & MDB_TGT_F_NOSTOP)
5285 5284 pt->p_gflags |= PGRAB_NOSTOP;
5286 5285
5287 5286 pt->p_ptl_ops = &proc_lwp_ops;
5288 5287 pt->p_maxsig = sysconf(_SC_SIGRT_MAX);
5289 5288
5290 5289 (void) mdb_nv_create(&pt->p_regs, UM_SLEEP);
5291 5290 (void) mdb_nv_create(&pt->p_env, UM_SLEEP);
5292 5291
5293 5292 t->t_ops = &proc_ops;
5294 5293 t->t_data = pt;
5295 5294
5296 5295 /*
5297 5296 * If no core file name was specified, but the file ./core is present,
5298 5297 * infer that we want to debug it. I find this behavior confusing,
5299 5298 * so we only do this when precise adb(1) compatibility is required.
5300 5299 */
5301 5300 if (core_path == NULL && (mdb.m_flags & MDB_FL_ADB) &&
5302 5301 access(PT_CORE_PATH, F_OK) == 0)
5303 5302 core_path = PT_CORE_PATH;
5304 5303
5305 5304 /*
5306 5305 * For compatibility with adb(1), the special name "-" may be used
5307 5306 * to suppress the loading of the executable or core file.
5308 5307 */
5309 5308 if (aout_path != NULL && strcmp(aout_path, "-") == 0)
5310 5309 aout_path = NULL;
5311 5310 if (core_path != NULL && strcmp(core_path, "-") == 0)
5312 5311 core_path = NULL;
5313 5312
5314 5313 /*
5315 5314 * If a core file or pid was specified, attempt to grab it now using
5316 5315 * proc_arg_grab(); otherwise we'll create a fresh process later.
5317 5316 */
5318 5317 if (core_path != NULL && (t->t_pshandle = proc_arg_xgrab(core_path,
5319 5318 aout_path == PT_EXEC_PATH ? NULL : aout_path, PR_ARG_ANY,
5320 5319 pt->p_gflags, &perr, NULL)) == NULL) {
5321 5320 mdb_warn("cannot debug %s: %s\n", core_path, Pgrab_error(perr));
5322 5321 goto err;
5323 5322 }
5324 5323
5325 5324 if (aout_path != NULL &&
5326 5325 (pt->p_idlehandle = Pgrab_file(aout_path, &perr)) != NULL &&
5327 5326 t->t_pshandle == NULL)
5328 5327 t->t_pshandle = pt->p_idlehandle;
5329 5328
5330 5329 if (t->t_pshandle != NULL)
5331 5330 state = Pstate(t->t_pshandle);
5332 5331
5333 5332 /*
5334 5333 * Make sure we'll have enough file descriptors to handle a target
5335 5334 * has many many mappings.
5336 5335 */
5337 5336 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
5338 5337 rlim.rlim_cur = rlim.rlim_max;
5339 5338 (void) setrlimit(RLIMIT_NOFILE, &rlim);
5340 5339 (void) enable_extended_FILE_stdio(-1, -1);
5341 5340 }
5342 5341
5343 5342 /*
5344 5343 * If we don't have an executable path or the executable path is the
5345 5344 * /proc/<pid>/object/a.out path, but we now have a libproc handle,
5346 5345 * attempt to derive the executable path using Pexecname(). We need
5347 5346 * to do this in the /proc case in order to open the executable for
5348 5347 * writing because /proc/object/<file> permission are masked with 0555.
5349 5348 * If Pexecname() fails us, fall back to /proc/<pid>/object/a.out.
5350 5349 */
5351 5350 if (t->t_pshandle != NULL && (aout_path == NULL || (stat64(aout_path,
5352 5351 &st) == 0 && strcmp(st.st_fstype, "proc") == 0))) {
5353 5352 GElf_Sym s;
5354 5353 aout_path = Pexecname(t->t_pshandle, execname, MAXPATHLEN);
5355 5354 if (aout_path == NULL && state != PS_DEAD && state != PS_IDLE) {
5356 5355 (void) mdb_iob_snprintf(execname, sizeof (execname),
5357 5356 "/proc/%d/object/a.out",
5358 5357 (int)Pstatus(t->t_pshandle)->pr_pid);
5359 5358 aout_path = execname;
5360 5359 }
5361 5360 if (aout_path == NULL &&
5362 5361 Plookup_by_name(t->t_pshandle, "a.out", "_start", &s) != 0)
5363 5362 mdb_warn("warning: failed to infer pathname to "
5364 5363 "executable; symbol table will not be available\n");
5365 5364
5366 5365 mdb_dprintf(MDB_DBG_TGT, "a.out is %s\n", aout_path);
5367 5366 }
5368 5367
5369 5368 /*
5370 5369 * Attempt to open the executable file. We only want this operation
5371 5370 * to actually cause the constructor to abort if the executable file
5372 5371 * name was given explicitly. If we defaulted to PT_EXEC_PATH or
5373 5372 * derived the executable using Pexecname, then we want to continue
5374 5373 * along with p_fio and p_file set to NULL.
5375 5374 */
5376 5375 if (aout_path != NULL && (pt->p_aout_fio = mdb_fdio_create_path(NULL,
5377 5376 aout_path, pt->p_oflags, 0)) == NULL && argc > 0) {
5378 5377 mdb_warn("failed to open %s", aout_path);
5379 5378 goto err;
5380 5379 }
5381 5380
5382 5381 /*
5383 5382 * Now create an ELF file from the input file, if we have one. Again,
5384 5383 * only abort the constructor if the name was given explicitly.
5385 5384 */
5386 5385 if (pt->p_aout_fio != NULL && pt_open_aout(t,
5387 5386 mdb_io_hold(pt->p_aout_fio)) == NULL && argc > 0)
5388 5387 goto err;
5389 5388
5390 5389 /*
5391 5390 * If we've successfully opened an ELF file, select the appropriate
5392 5391 * disassembler based on the ELF header.
5393 5392 */
5394 5393 if (pt->p_file != NULL)
5395 5394 (void) mdb_dis_select(pt_disasm(&pt->p_file->gf_ehdr));
5396 5395 else
5397 5396 (void) mdb_dis_select(pt_disasm(NULL));
5398 5397
5399 5398 /*
5400 5399 * Add each register described in the target ISA register description
5401 5400 * list to our hash table of register descriptions and then add any
5402 5401 * appropriate ISA-specific floating-point register descriptions.
5403 5402 */
5404 5403 for (rdp = pt_regdesc; rdp->rd_name != NULL; rdp++) {
5405 5404 (void) mdb_nv_insert(&pt->p_regs, rdp->rd_name, NULL,
5406 5405 MDB_TGT_R_NVAL(rdp->rd_num, rdp->rd_flags), MDB_NV_RDONLY);
5407 5406 }
5408 5407 pt_addfpregs(t);
5409 5408
5410 5409 /*
5411 5410 * Certain important /proc structures may be of interest to mdb
5412 5411 * modules and their dcmds. Export these using the xdata interface:
5413 5412 */
5414 5413 (void) mdb_tgt_xdata_insert(t, "auxv",
5415 5414 "procfs auxv_t array", pt_xd_auxv);
5416 5415 (void) mdb_tgt_xdata_insert(t, "cred",
5417 5416 "procfs prcred_t structure", pt_xd_cred);
5418 5417 (void) mdb_tgt_xdata_insert(t, "ehdr",
5419 5418 "executable file GElf_Ehdr structure", pt_xd_ehdr);
5420 5419 (void) mdb_tgt_xdata_insert(t, "lwpstatus",
5421 5420 "procfs lwpstatus_t array", pt_xd_lwpstatus);
5422 5421 (void) mdb_tgt_xdata_insert(t, "pshandle",
5423 5422 "libproc proc service API handle", pt_xd_pshandle);
5424 5423 (void) mdb_tgt_xdata_insert(t, "psinfo",
5425 5424 "procfs psinfo_t structure", pt_xd_psinfo);
5426 5425 (void) mdb_tgt_xdata_insert(t, "pstatus",
5427 5426 "procfs pstatus_t structure", pt_xd_pstatus);
5428 5427 (void) mdb_tgt_xdata_insert(t, "utsname",
5429 5428 "utsname structure", pt_xd_utsname);
5430 5429
5431 5430 /*
5432 5431 * Force a status update now so that we fill in t_status with the
5433 5432 * latest information based on any successful grab.
5434 5433 */
5435 5434 (void) mdb_tgt_status(t, &t->t_status);
5436 5435
5437 5436 /*
5438 5437 * If we're not examining a core file, trace SIGINT and all signals
5439 5438 * that cause the process to dump core as part of our initialization.
5440 5439 */
5441 5440 if ((t->t_pshandle != NULL && state != PS_DEAD && state != PS_IDLE) ||
5442 5441 (pt->p_file != NULL && pt->p_file->gf_ehdr.e_type == ET_EXEC)) {
5443 5442
5444 5443 int tflag = MDB_TGT_SPEC_STICKY; /* default sigs are sticky */
5445 5444
5446 5445 (void) mdb_tgt_add_signal(t, SIGINT, tflag, no_se_f, NULL);
5447 5446 (void) mdb_tgt_add_signal(t, SIGQUIT, tflag, no_se_f, NULL);
5448 5447 (void) mdb_tgt_add_signal(t, SIGILL, tflag, no_se_f, NULL);
5449 5448 (void) mdb_tgt_add_signal(t, SIGTRAP, tflag, no_se_f, NULL);
5450 5449 (void) mdb_tgt_add_signal(t, SIGABRT, tflag, no_se_f, NULL);
5451 5450 (void) mdb_tgt_add_signal(t, SIGEMT, tflag, no_se_f, NULL);
5452 5451 (void) mdb_tgt_add_signal(t, SIGFPE, tflag, no_se_f, NULL);
5453 5452 (void) mdb_tgt_add_signal(t, SIGBUS, tflag, no_se_f, NULL);
5454 5453 (void) mdb_tgt_add_signal(t, SIGSEGV, tflag, no_se_f, NULL);
5455 5454 (void) mdb_tgt_add_signal(t, SIGSYS, tflag, no_se_f, NULL);
5456 5455 (void) mdb_tgt_add_signal(t, SIGXCPU, tflag, no_se_f, NULL);
5457 5456 (void) mdb_tgt_add_signal(t, SIGXFSZ, tflag, no_se_f, NULL);
5458 5457 }
5459 5458
5460 5459 /*
5461 5460 * If we've grabbed a live process, establish our initial breakpoints
5462 5461 * and librtld_db agent so we can track rtld activity. If FL_VCREATE
5463 5462 * is set, this process was created by a previous instantiation of
5464 5463 * the debugger, so reset pr_flags to kill it; otherwise we attached
5465 5464 * to an already running process. Pgrab() has already set the PR_RLC
5466 5465 * flag appropriately based on whether the process was stopped when we
5467 5466 * attached.
5468 5467 */
5469 5468 if (t->t_pshandle != NULL && state != PS_DEAD && state != PS_IDLE) {
5470 5469 if (mdb.m_flags & MDB_FL_VCREATE) {
5471 5470 (void) Punsetflags(t->t_pshandle, PR_RLC);
5472 5471 (void) Psetflags(t->t_pshandle, PR_KLC);
5473 5472 pt->p_rflags = PRELEASE_KILL;
5474 5473 } else {
5475 5474 (void) Punsetflags(t->t_pshandle, PR_KLC);
5476 5475 }
5477 5476 pt_post_attach(t);
5478 5477 }
5479 5478
5480 5479 /*
5481 5480 * Initialize a local copy of the environment, which can be modified
5482 5481 * before running the program.
5483 5482 */
5484 5483 for (i = 0; mdb.m_env[i] != NULL; i++)
5485 5484 pt_env_set(pt, mdb.m_env[i]);
5486 5485
5487 5486 /*
5488 5487 * If adb(1) compatibility mode is on, then print the appropriate
5489 5488 * greeting message if we have grabbed a core file.
5490 5489 */
5491 5490 if ((mdb.m_flags & MDB_FL_ADB) && t->t_pshandle != NULL &&
5492 5491 state == PS_DEAD) {
5493 5492 const pstatus_t *psp = Pstatus(t->t_pshandle);
5494 5493 int cursig = psp->pr_lwp.pr_cursig;
5495 5494 char signame[SIG2STR_MAX];
5496 5495
5497 5496 mdb_printf("core file = %s -- program ``%s'' on platform %s\n",
5498 5497 core_path, aout_path ? aout_path : "?", pt_platform(t));
5499 5498
5500 5499 if (cursig != 0 && sig2str(cursig, signame) == 0)
5501 5500 mdb_printf("SIG%s: %s\n", signame, strsignal(cursig));
5502 5501 }
5503 5502
5504 5503 return (0);
5505 5504
5506 5505 err:
5507 5506 pt_destroy(t);
5508 5507 return (-1);
5509 5508 }
↓ open down ↓ |
3505 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX