8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libtnfctl / prb_lmap.c
blob246099d7e963e2fefd7984a57f870cabbf1f9885
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Interfaces that deal with loadobjects (shared objects) in target
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>
40 #include "tnfctl.h"
41 #include "prb_proc_int.h"
42 #include "dbg.h"
45 * iterate over all loadobjects calling the callback function "obj_func"
46 * for every loadobject with information about the loadobject.
48 prb_status_t
49 prb_loadobj_iter(prb_proc_ctl_t *proc_p, prb_loadobj_f *obj_func, void *cd)
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;
59 DBG_TNF_PROBE_0(prb_loadobj_iter_start, "libtnfctl",
60 "start prb_loadobj_iter; sunw%verbosity 1");
62 if (proc_p->dbgaddr == 0) {
63 DBG((void) fprintf(stderr,
64 "prb_loadobj_iter: dbgaddr not set\n"));
65 return (PRB_STATUS_BADARG);
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);
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);
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");
86 /* if the link map is not consistent, bail now */
87 if (r_dbg.r_state != RT_CONSISTENT)
88 return (PRB_STATUS_BADLMAPSTATE);
90 lmap.l_next = NULL; /* makes lint happy */
92 for (lmapaddr = (uintptr_t) r_dbg.r_map; lmapaddr;
93 lmapaddr = (uintptr_t) lmap.l_next) {
95 prbstat = prb_proc_read(proc_p, lmapaddr, &lmap, sizeof (lmap));
96 if (prbstat)
97 return (prbstat);
99 loadobj.text_base = lmap.l_addr;
100 loadobj.data_base = lmap.l_addr;
101 loadobj.objname = NULL;
103 * client of this interface should deal with -1 for objfd,
104 * so no error checking is needed on this ioctl
106 loadobj.objfd = ioctl(proc_p->procfd, PIOCOPENM, &lmap.l_addr);
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);
120 DBG_TNF_PROBE_0(prb_loadobj_iter_end, "libtnfctl",
121 "end prb_loadobj_iter; sunw%verbosity 1");
122 return (PRB_STATUS_OK);
126 * Return a fd for the main executable and also the address of where
127 * it was mapped.
129 prb_status_t
130 prb_mainobj_get(prb_proc_ctl_t *proc_p, int *objfd, uintptr_t *baseaddr)
132 int procfd;
133 int retfd;
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));
147 *objfd = retfd;
148 *baseaddr = 0;
150 return (PRB_STATUS_OK);