1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2013 by Delphix. All rights reserved.
*/
#include <stdarg.h>
#include <string.h>
#include "Pcontrol.h"
/*
* This file implements the process services declared in <proc_service.h>.
* This enables libproc to be used in conjunction with libc_db and
* librtld_db. As most of these facilities are already provided by
* (more elegant) interfaces in <libproc.h>, we can just call those.
*
* NOTE: We explicitly do *not* implement the functions ps_kill() and
* ps_lrolltoaddr() in this library. The very existence of these functions
* causes libc_db to create an "agent thread" in the target process.
* The only way to turn off this behavior is to omit these functions.
*/
#pragma weak ps_pdread = ps_pread
#pragma weak ps_ptread = ps_pread
#pragma weak ps_pdwrite = ps_pwrite
#pragma weak ps_ptwrite = ps_pwrite
ps_err_e
ps_pdmodel(struct ps_prochandle *P, int *modelp)
{
*modelp = P->status.pr_dmodel;
return (PS_OK);
}
ps_err_e
ps_pread(struct ps_prochandle *P, psaddr_t addr, void *buf, size_t size)
{
if (P->ops.pop_pread(P, buf, size, addr, P->data) != size)
return (PS_BADADDR);
return (PS_OK);
}
ps_err_e
ps_pwrite(struct ps_prochandle *P, psaddr_t addr, const void *buf, size_t size)
{
if (P->ops.pop_pwrite(P, buf, size, addr, P->data) != size)
return (PS_BADADDR);
return (PS_OK);
}
/*
* libc_db calls matched pairs of ps_pstop()/ps_pcontinue()
* in the belief that the client may have left the process
* running while calling in to the libc_db interfaces.
*
* We interpret the meaning of these functions to be an inquiry
* as to whether the process is stopped, not an action to be
* performed to make it stopped. For similar reasons, we also
* return PS_OK for core files in order to allow libc_db to
* operate on these as well.
*/
ps_err_e
ps_pstop(struct ps_prochandle *P)
{
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
return (PS_OK);
}
ps_err_e
ps_pcontinue(struct ps_prochandle *P)
{
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
return (PS_OK);
}
/*
* ps_lstop() and ps_lcontinue() are not called by any code in libc_db
* or librtld_db. We make them behave like ps_pstop() and ps_pcontinue().
*/
/* ARGSUSED1 */
ps_err_e
ps_lstop(struct ps_prochandle *P, lwpid_t lwpid)
{
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
return (PS_OK);
}
/* ARGSUSED1 */
ps_err_e
ps_lcontinue(struct ps_prochandle *P, lwpid_t lwpid)
{
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
return (PS_OK);
}
ps_err_e
ps_lgetregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t regs)
{
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
if (Plwp_getregs(P, lwpid, regs) == 0)
return (PS_OK);
return (PS_BADLID);
}
ps_err_e
ps_lsetregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t regs)
{
if (P->state != PS_STOP)
return (PS_ERR);
if (Plwp_setregs(P, lwpid, regs) == 0)
return (PS_OK);
return (PS_BADLID);
}
ps_err_e
ps_lgetfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *regs)
{
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
if (Plwp_getfpregs(P, lwpid, regs) == 0)
return (PS_OK);
return (PS_BADLID);
}
ps_err_e
ps_lsetfpregs(struct ps_prochandle *P, lwpid_t lwpid, const prfpregset_t *regs)
{
if (P->state != PS_STOP)
return (PS_ERR);
if (Plwp_setfpregs(P, lwpid, regs) == 0)
return (PS_OK);
return (PS_BADLID);
}
#if defined(sparc) || defined(__sparc)
ps_err_e
ps_lgetxregsize(struct ps_prochandle *P, lwpid_t lwpid, int *xrsize)
{
char fname[PATH_MAX];
struct stat statb;
if (P->state == PS_DEAD) {
core_info_t *core = P->data;
lwp_info_t *lwp = list_next(&core->core_lwp_head);
uint_t i;
for (i = 0; i < core->core_nlwp; i++, lwp = list_next(lwp)) {
if (lwp->lwp_id == lwpid) {
if (lwp->lwp_xregs != NULL)
*xrsize = sizeof (prxregset_t);
else
*xrsize = 0;
return (PS_OK);
}
}
return (PS_BADLID);
}
(void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/xregs",
procfs_path, (int)P->status.pr_pid, (int)lwpid);
if (stat(fname, &statb) != 0)
return (PS_BADLID);
*xrsize = (int)statb.st_size;
return (PS_OK);
}
ps_err_e
ps_lgetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
{
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
/* LINTED - alignment */
if (Plwp_getxregs(P, lwpid, (prxregset_t *)xregs) == 0)
return (PS_OK);
return (PS_BADLID);
}
ps_err_e
ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
{
if (P->state != PS_STOP)
return (PS_ERR);
/* LINTED - alignment */
if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs) == 0)
return (PS_OK);
return (PS_BADLID);
}
#endif /* sparc */
#if defined(__i386) || defined(__amd64)
ps_err_e
ps_lgetLDT(struct ps_prochandle *P, lwpid_t lwpid, struct ssd *ldt)
{
#if defined(__amd64) && defined(_LP64)
if (P->status.pr_dmodel != PR_MODEL_NATIVE) {
#endif
prgregset_t regs;
struct ssd *ldtarray;
ps_err_e error;
uint_t gs;
int nldt;
int i;
if (P->state != PS_STOP && P->state != PS_DEAD)
return (PS_ERR);
/*
* We need to get the ldt entry that matches the
* value in the lwp's GS register.
*/
if ((error = ps_lgetregs(P, lwpid, regs)) != PS_OK)
return (error);
gs = regs[GS];
if ((nldt = Pldt(P, NULL, 0)) <= 0 ||
(ldtarray = malloc(nldt * sizeof (struct ssd))) == NULL)
return (PS_ERR);
if ((nldt = Pldt(P, ldtarray, nldt)) <= 0) {
free(ldtarray);
return (PS_ERR);
}
for (i = 0; i < nldt; i++) {
if (gs == ldtarray[i].sel) {
*ldt = ldtarray[i];
break;
}
}
free(ldtarray);
if (i < nldt)
return (PS_OK);
#if defined(__amd64) && defined(_LP64)
}
#endif
return (PS_ERR);
}
#endif /* __i386 || __amd64 */
/*
* Libthread_db doesn't use this function currently, but librtld_db uses
* it for its debugging output. We turn this on via rd_log if our debugging
* switch is on, and then echo the messages sent to ps_plog to stderr.
*/
void
ps_plog(const char *fmt, ...)
{
va_list ap;
if (_libproc_debug && fmt != NULL && *fmt != '\0') {
va_start(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[strlen(fmt) - 1] != '\n')
(void) fputc('\n', stderr);
}
}
/*
* Store a pointer to our internal copy of the aux vector at the address
* specified by the caller. It should not hold on to this data for too long.
*/
ps_err_e
ps_pauxv(struct ps_prochandle *P, const auxv_t **aux)
{
if (P->auxv == NULL)
Preadauxvec(P);
if (P->auxv == NULL)
return (PS_ERR);
*aux = (const auxv_t *)P->auxv;
return (PS_OK);
}
ps_err_e
ps_pbrandname(struct ps_prochandle *P, char *buf, size_t len)
{
return (Pbrandname(P, buf, len) ? PS_OK : PS_ERR);
}
/*
* Search for a symbol by name and return the corresponding address.
*/
ps_err_e
ps_pglobal_lookup(struct ps_prochandle *P, const char *object_name,
const char *sym_name, psaddr_t *sym_addr)
{
GElf_Sym sym;
if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
dprintf("pglobal_lookup <%s> -> %p\n",
sym_name, (void *)(uintptr_t)sym.st_value);
*sym_addr = (psaddr_t)sym.st_value;
return (PS_OK);
}
return (PS_NOSYM);
}
/*
* Search for a symbol by name and return the corresponding symbol
* information. If we're compiled _LP64, we just call Plookup_by_name
* and return because ps_sym_t is defined to be an Elf64_Sym, which
* is the same as a GElf_Sym. In the _ILP32 case, we have to convert
* Plookup_by_name's result back to a ps_sym_t (which is an Elf32_Sym).
*/
ps_err_e
ps_pglobal_sym(struct ps_prochandle *P, const char *object_name,
const char *sym_name, ps_sym_t *symp)
{
#if defined(_ILP32)
GElf_Sym sym;
if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
symp->st_name = (Elf32_Word)sym.st_name;
symp->st_value = (Elf32_Addr)sym.st_value;
symp->st_size = (Elf32_Word)sym.st_size;
symp->st_info = ELF32_ST_INFO(
GELF_ST_BIND(sym.st_info), GELF_ST_TYPE(sym.st_info));
symp->st_other = sym.st_other;
symp->st_shndx = sym.st_shndx;
return (PS_OK);
}
#elif defined(_LP64)
if (Plookup_by_name(P, object_name, sym_name, symp) == 0)
return (PS_OK);
#endif
return (PS_NOSYM);
}
|