Print this page
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libproc/common/Pservice.c
+++ new/usr/src/lib/libproc/common/Pservice.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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25 /*
26 26 * Copyright (c) 2013 by Delphix. All rights reserved.
27 27 * Copyright 2023 Oxide Computer Company
28 28 */
29 29
30 30 #include <stdarg.h>
31 31 #include <string.h>
32 32 #include <errno.h>
33 33 #include "Pcontrol.h"
34 34
35 35 /*
36 36 * This file implements the process services declared in <proc_service.h>.
37 37 * This enables libproc to be used in conjunction with libc_db and
38 38 * librtld_db. As most of these facilities are already provided by
39 39 * (more elegant) interfaces in <libproc.h>, we can just call those.
40 40 *
41 41 * NOTE: We explicitly do *not* implement the functions ps_kill() and
42 42 * ps_lrolltoaddr() in this library. The very existence of these functions
43 43 * causes libc_db to create an "agent thread" in the target process.
44 44 * The only way to turn off this behavior is to omit these functions.
45 45 */
46 46
47 47 #pragma weak ps_pdread = ps_pread
48 48 #pragma weak ps_ptread = ps_pread
49 49 #pragma weak ps_pdwrite = ps_pwrite
50 50 #pragma weak ps_ptwrite = ps_pwrite
51 51
52 52 ps_err_e
53 53 ps_pdmodel(struct ps_prochandle *P, int *modelp)
54 54 {
55 55 *modelp = P->status.pr_dmodel;
56 56 return (PS_OK);
57 57 }
58 58
59 59 ps_err_e
60 60 ps_pread(struct ps_prochandle *P, psaddr_t addr, void *buf, size_t size)
61 61 {
62 62 if (P->ops.pop_pread(P, buf, size, addr, P->data) != size)
63 63 return (PS_BADADDR);
64 64 return (PS_OK);
65 65 }
66 66
67 67 ps_err_e
68 68 ps_pwrite(struct ps_prochandle *P, psaddr_t addr, const void *buf, size_t size)
69 69 {
70 70 if (P->ops.pop_pwrite(P, buf, size, addr, P->data) != size)
71 71 return (PS_BADADDR);
72 72 return (PS_OK);
73 73 }
74 74
75 75 /*
76 76 * libc_db calls matched pairs of ps_pstop()/ps_pcontinue()
77 77 * in the belief that the client may have left the process
78 78 * running while calling in to the libc_db interfaces.
79 79 *
80 80 * We interpret the meaning of these functions to be an inquiry
81 81 * as to whether the process is stopped, not an action to be
82 82 * performed to make it stopped. For similar reasons, we also
83 83 * return PS_OK for core files in order to allow libc_db to
84 84 * operate on these as well.
85 85 */
86 86 ps_err_e
87 87 ps_pstop(struct ps_prochandle *P)
88 88 {
89 89 if (P->state != PS_STOP && P->state != PS_DEAD)
90 90 return (PS_ERR);
91 91 return (PS_OK);
92 92 }
93 93
94 94 ps_err_e
95 95 ps_pcontinue(struct ps_prochandle *P)
96 96 {
97 97 if (P->state != PS_STOP && P->state != PS_DEAD)
98 98 return (PS_ERR);
99 99 return (PS_OK);
100 100 }
101 101
102 102 /*
103 103 * ps_lstop() and ps_lcontinue() are not called by any code in libc_db
104 104 * or librtld_db. We make them behave like ps_pstop() and ps_pcontinue().
105 105 */
106 106 /* ARGSUSED1 */
107 107 ps_err_e
108 108 ps_lstop(struct ps_prochandle *P, lwpid_t lwpid)
109 109 {
110 110 if (P->state != PS_STOP && P->state != PS_DEAD)
111 111 return (PS_ERR);
112 112 return (PS_OK);
113 113 }
114 114
115 115 /* ARGSUSED1 */
116 116 ps_err_e
117 117 ps_lcontinue(struct ps_prochandle *P, lwpid_t lwpid)
118 118 {
119 119 if (P->state != PS_STOP && P->state != PS_DEAD)
120 120 return (PS_ERR);
121 121 return (PS_OK);
122 122 }
123 123
124 124 ps_err_e
125 125 ps_lgetregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t regs)
126 126 {
127 127 if (P->state != PS_STOP && P->state != PS_DEAD)
128 128 return (PS_ERR);
129 129
130 130 if (Plwp_getregs(P, lwpid, regs) == 0)
131 131 return (PS_OK);
132 132
133 133 return (PS_BADLID);
134 134 }
135 135
136 136 ps_err_e
137 137 ps_lsetregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t regs)
138 138 {
139 139 if (P->state != PS_STOP)
140 140 return (PS_ERR);
141 141
142 142 if (Plwp_setregs(P, lwpid, regs) == 0)
143 143 return (PS_OK);
144 144
145 145 return (PS_BADLID);
146 146 }
147 147
148 148 ps_err_e
149 149 ps_lgetfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *regs)
150 150 {
151 151 if (P->state != PS_STOP && P->state != PS_DEAD)
152 152 return (PS_ERR);
153 153
154 154 if (Plwp_getfpregs(P, lwpid, regs) == 0)
155 155 return (PS_OK);
156 156
157 157 return (PS_BADLID);
158 158 }
159 159
160 160 ps_err_e
161 161 ps_lsetfpregs(struct ps_prochandle *P, lwpid_t lwpid, const prfpregset_t *regs)
162 162 {
163 163 if (P->state != PS_STOP)
164 164 return (PS_ERR);
165 165
166 166 if (Plwp_setfpregs(P, lwpid, regs) == 0)
167 167 return (PS_OK);
168 168
169 169 return (PS_BADLID);
170 170 }
171 171
172 172 ps_err_e
173 173 ps_lgetxregsize(struct ps_prochandle *P, lwpid_t lwpid, int *xrsize)
174 174 {
175 175 char fname[PATH_MAX];
176 176 struct stat statb;
177 177
178 178 if (P->state == PS_DEAD) {
179 179 core_info_t *core = P->data;
180 180 lwp_info_t *lwp;
181 181
182 182 for (lwp = list_head(&core->core_lwp_head); lwp != NULL;
183 183 lwp = list_next(&core->core_lwp_head, lwp)) {
184 184 if (lwp->lwp_id == lwpid) {
185 185 if (lwp->lwp_xregs != NULL &&
186 186 lwp->lwp_xregsize > 0) {
187 187 if (lwp->lwp_xregsize >= INT_MAX) {
188 188 return (PS_ERR);
189 189 }
190 190
191 191 *xrsize = (int)lwp->lwp_xregsize;
192 192 } else {
193 193 *xrsize = 0;
194 194 }
195 195 return (PS_OK);
196 196 }
197 197 }
198 198
199 199 return (PS_BADLID);
200 200 }
201 201
202 202 (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/xregs",
203 203 procfs_path, (int)P->status.pr_pid, (int)lwpid);
204 204
205 205 if (stat(fname, &statb) != 0)
206 206 return (PS_BADLID);
207 207
208 208 if (statb.st_size > INT_MAX)
209 209 return (PS_ERR);
210 210
211 211 *xrsize = (int)statb.st_size;
212 212 return (PS_OK);
213 213 }
214 214
215 215 ps_err_e
216 216 ps_lgetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
217 217 {
218 218 size_t xregsize;
219 219 prxregset_t *prx;
220 220
221 221 if (P->state != PS_STOP && P->state != PS_DEAD)
222 222 return (PS_ERR);
223 223
224 224 if (Plwp_getxregs(P, lwpid, &prx, &xregsize) == 0) {
225 225 (void) memcpy(xregs, prx, xregsize);
226 226 Plwp_freexregs(P, prx, xregsize);
227 227 return (PS_OK);
228 228 }
229 229
230 230 if (errno == ENODATA)
231 231 return (PS_NOXREGS);
232 232
233 233 return (PS_BADLID);
234 234 }
235 235
236 236 ps_err_e
237 237 ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
238 238 {
239 239 size_t xregsize = 0;
240 240
241 241 if (P->state != PS_STOP)
242 242 return (PS_ERR);
243 243
244 244 /*
245 245 * libproc asks the caller for the size of the extended register set.
246 246 * Unfortunately, right now we aren't given the actual size of this
247 247 * ourselves and we don't want to break the ABI that folks have used
248 248 * historically. Therefore, we reach in and ask the structure in a
249 249 * platform-specific way about what this should be. Sorry, this is a bit
250 250 * unfortunate. This really shouldn't be a platform-specific #ifdef.
251 251 */
252 252 #if defined(__i386) || defined(__amd64)
253 253 prxregset_hdr_t *hdr = (prxregset_hdr_t *)xregs;
254 254 xregsize = hdr->pr_size;
255 255 #endif
256 256 if (xregsize == 0)
257 257 return (PS_ERR);
258 258
259 259 if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs, xregsize) == 0)
260 260 return (PS_OK);
261 261
262 262 return (PS_BADLID);
263 263 }
264 264
265 265 #if defined(sparc) || defined(__sparc)
266 266 ps_err_e
267 267 ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
268 268 {
269 269 if (P->state != PS_STOP)
270 270 return (PS_ERR);
271 271
272 272 /* LINTED - alignment */
273 273 if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs) == 0)
274 274 return (PS_OK);
275 275
276 276 return (PS_BADLID);
277 277 }
278 278
279 279 #endif /* sparc */
280 280
281 281 #if defined(__i386) || defined(__amd64)
282 282
283 283 ps_err_e
284 284 ps_lgetLDT(struct ps_prochandle *P, lwpid_t lwpid, struct ssd *ldt)
285 285 {
286 286 #if defined(__amd64) && defined(_LP64)
287 287 if (P->status.pr_dmodel != PR_MODEL_NATIVE) {
288 288 #endif
289 289 prgregset_t regs;
290 290 struct ssd *ldtarray;
291 291 ps_err_e error;
292 292 uint_t gs;
293 293 int nldt;
294 294 int i;
295 295
296 296 if (P->state != PS_STOP && P->state != PS_DEAD)
297 297 return (PS_ERR);
298 298
299 299 /*
300 300 * We need to get the ldt entry that matches the
301 301 * value in the lwp's GS register.
302 302 */
303 303 if ((error = ps_lgetregs(P, lwpid, regs)) != PS_OK)
304 304 return (error);
305 305
306 306 gs = regs[GS];
307 307
308 308 if ((nldt = Pldt(P, NULL, 0)) <= 0 ||
309 309 (ldtarray = malloc(nldt * sizeof (struct ssd))) == NULL)
310 310 return (PS_ERR);
311 311 if ((nldt = Pldt(P, ldtarray, nldt)) <= 0) {
312 312 free(ldtarray);
313 313 return (PS_ERR);
314 314 }
315 315
316 316 for (i = 0; i < nldt; i++) {
317 317 if (gs == ldtarray[i].sel) {
318 318 *ldt = ldtarray[i];
319 319 break;
320 320 }
321 321 }
322 322 free(ldtarray);
323 323
324 324 if (i < nldt)
325 325 return (PS_OK);
326 326 #if defined(__amd64) && defined(_LP64)
327 327 }
328 328 #endif
329 329
330 330 return (PS_ERR);
331 331 }
332 332
333 333 #endif /* __i386 || __amd64 */
334 334
335 335 /*
336 336 * Libthread_db doesn't use this function currently, but librtld_db uses
337 337 * it for its debugging output. We turn this on via rd_log if our debugging
338 338 * switch is on, and then echo the messages sent to ps_plog to stderr.
339 339 */
340 340 void
341 341 ps_plog(const char *fmt, ...)
342 342 {
343 343 va_list ap;
344 344
345 345 if (_libproc_debug && fmt != NULL && *fmt != '\0') {
346 346 va_start(ap, fmt);
347 347 (void) vfprintf(stderr, fmt, ap);
348 348 va_end(ap);
349 349 if (fmt[strlen(fmt) - 1] != '\n')
350 350 (void) fputc('\n', stderr);
351 351 }
352 352 }
353 353
354 354 /*
355 355 * Store a pointer to our internal copy of the aux vector at the address
356 356 * specified by the caller. It should not hold on to this data for too long.
357 357 */
358 358 ps_err_e
359 359 ps_pauxv(struct ps_prochandle *P, const auxv_t **aux)
360 360 {
361 361 if (P->auxv == NULL)
362 362 Preadauxvec(P);
363 363
364 364 if (P->auxv == NULL)
365 365 return (PS_ERR);
366 366
367 367 *aux = (const auxv_t *)P->auxv;
368 368 return (PS_OK);
369 369 }
370 370
371 371 ps_err_e
372 372 ps_pbrandname(struct ps_prochandle *P, char *buf, size_t len)
373 373 {
374 374 return (Pbrandname(P, buf, len) ? PS_OK : PS_ERR);
375 375 }
376 376
377 377 /*
378 378 * Search for a symbol by name and return the corresponding address.
379 379 */
380 380 ps_err_e
381 381 ps_pglobal_lookup(struct ps_prochandle *P, const char *object_name,
382 382 const char *sym_name, psaddr_t *sym_addr)
383 383 {
384 384 GElf_Sym sym;
385 385
386 386 if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
387 387 dprintf("pglobal_lookup <%s> -> %p\n",
388 388 sym_name, (void *)(uintptr_t)sym.st_value);
389 389 *sym_addr = (psaddr_t)sym.st_value;
390 390 return (PS_OK);
391 391 }
392 392
393 393 return (PS_NOSYM);
394 394 }
395 395
396 396 /*
397 397 * Search for a symbol by name and return the corresponding symbol
398 398 * information. If we're compiled _LP64, we just call Plookup_by_name
399 399 * and return because ps_sym_t is defined to be an Elf64_Sym, which
400 400 * is the same as a GElf_Sym. In the _ILP32 case, we have to convert
401 401 * Plookup_by_name's result back to a ps_sym_t (which is an Elf32_Sym).
402 402 */
403 403 ps_err_e
404 404 ps_pglobal_sym(struct ps_prochandle *P, const char *object_name,
405 405 const char *sym_name, ps_sym_t *symp)
406 406 {
407 407 #if defined(_ILP32)
408 408 GElf_Sym sym;
409 409
410 410 if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
411 411 symp->st_name = (Elf32_Word)sym.st_name;
412 412 symp->st_value = (Elf32_Addr)sym.st_value;
413 413 symp->st_size = (Elf32_Word)sym.st_size;
414 414 symp->st_info = ELF32_ST_INFO(
415 415 GELF_ST_BIND(sym.st_info), GELF_ST_TYPE(sym.st_info));
416 416 symp->st_other = sym.st_other;
417 417 symp->st_shndx = sym.st_shndx;
418 418 return (PS_OK);
419 419 }
420 420
421 421 #elif defined(_LP64)
422 422 if (Plookup_by_name(P, object_name, sym_name, symp) == 0)
423 423 return (PS_OK);
424 424 #endif
425 425 return (PS_NOSYM);
426 426 }
|
↓ open down ↓ |
426 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX