1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
24 */
25
26 #pragma ident "%Z%%M% %I% %E% SMI"
27
28 /*
29 * Interfaces that deal with loadobjects (shared objects) in target
30 */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <link.h>
37 #include <unistd.h>
38 #include <sys/procfs.h>
39
40 #include "tnfctl.h"
41 #include "prb_proc_int.h"
42 #include "dbg.h"
43
44 /*
45 * iterate over all loadobjects calling the callback function "obj_func"
46 * for every loadobject with information about the loadobject.
47 */
48 prb_status_t
prb_loadobj_iter(prb_proc_ctl_t * proc_p,prb_loadobj_f * obj_func,void * cd)49 prb_loadobj_iter(prb_proc_ctl_t *proc_p, prb_loadobj_f *obj_func, void *cd)
50 {
51 prb_status_t prbstat;
52 Elf3264_Dyn dentry;
53 struct r_debug r_dbg;
54 uintptr_t lmapaddr;
55 struct link_map lmap;
56 prb_loadobj_t loadobj;
57 int retval = 0;
58
59 DBG_TNF_PROBE_0(prb_loadobj_iter_start, "libtnfctl",
60 "start prb_loadobj_iter; sunw%verbosity 1");
61
62 if (proc_p->dbgaddr == 0) {
63 DBG((void) fprintf(stderr,
64 "prb_loadobj_iter: dbgaddr not set\n"));
65 return (PRB_STATUS_BADARG);
66 }
67
68 prbstat = prb_proc_read(proc_p, proc_p->dbgaddr, &dentry,
69 sizeof (dentry));
70 if (prbstat || !dentry.d_un.d_ptr) {
71 DBG((void) fprintf(stderr,
72 "prb_lmap_update: error in d_un.d_ptr\n"));
73 return (prbstat);
74 }
75 /* read in the debug struct that it points to */
76 prbstat = prb_proc_read(proc_p, dentry.d_un.d_ptr,
77 &r_dbg, sizeof (r_dbg));
78 if (prbstat)
79 return (prbstat);
80
81 DBG_TNF_PROBE_1(prb_loadobj_iter_1, "libtnfctl", "sunw%verbosity 1",
82 tnf_string, link_map_state,
83 (r_dbg.r_state == RT_CONSISTENT) ? "RT_CONSISTENT" :
84 (r_dbg.r_state == RT_ADD) ? "RT_ADD" : "RT_DELETE");
85
86 /* if the link map is not consistent, bail now */
87 if (r_dbg.r_state != RT_CONSISTENT)
88 return (PRB_STATUS_BADLMAPSTATE);
89
90 lmap.l_next = NULL; /* makes lint happy */
91
92 for (lmapaddr = (uintptr_t) r_dbg.r_map; lmapaddr;
93 lmapaddr = (uintptr_t) lmap.l_next) {
94
95 prbstat = prb_proc_read(proc_p, lmapaddr, &lmap, sizeof (lmap));
96 if (prbstat)
97 return (prbstat);
98
99 loadobj.text_base = lmap.l_addr;
100 loadobj.data_base = lmap.l_addr;
101 loadobj.objname = NULL;
102 /*
103 * client of this interface should deal with -1 for objfd,
104 * so no error checking is needed on this ioctl
105 */
106 loadobj.objfd = ioctl(proc_p->procfd, PIOCOPENM, &lmap.l_addr);
107
108 (void) prb_proc_readstr(proc_p, (uintptr_t) lmap.l_name,
109 &loadobj.objname);
110 retval = obj_func(proc_p, &loadobj, cd);
111 if (loadobj.objname)
112 free((char *)loadobj.objname);
113 if (loadobj.objfd != -1)
114 close(loadobj.objfd);
115 /* check for error */
116 if (retval == 1)
117 return (PRB_STATUS_BADARG);
118 }
119
120 DBG_TNF_PROBE_0(prb_loadobj_iter_end, "libtnfctl",
121 "end prb_loadobj_iter; sunw%verbosity 1");
122 return (PRB_STATUS_OK);
123 }
124
125 /*
126 * Return a fd for the main executable and also the address of where
127 * it was mapped.
128 */
129 prb_status_t
prb_mainobj_get(prb_proc_ctl_t * proc_p,int * objfd,uintptr_t * baseaddr)130 prb_mainobj_get(prb_proc_ctl_t *proc_p, int *objfd, uintptr_t *baseaddr)
131 {
132 int procfd;
133 int retfd;
134
135
136 procfd = proc_p->procfd;
137 again:
138 retfd = ioctl(procfd, PIOCOPENM, 0);
139 if (retfd < 0) {
140 if (errno == EINTR)
141 goto again;
142 DBG((void) fprintf(stderr,
143 "prb_mainobj_get: PIOCOPENM failed: %s\n",
144 strerror(errno)));
145 return (prb_status_map(errno));
146 }
147 *objfd = retfd;
148 *baseaddr = 0;
149
150 return (PRB_STATUS_OK);
151 }
152