4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Implements the routines that are needed only for internal process
36 #include "tnfctl_int.h"
37 #include "kernel_int.h"
41 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/procfs.h>
53 static int inprocess_read(void *ignore
,
54 uintptr_t addr
, void *buf
, size_t size
);
55 static int inprocess_write(void *ignore
,
56 uintptr_t addr
, void *buf
, size_t size
);
57 static pid_t
inprocess_getpid(void *ignore
);
58 static tnfctl_errcode_t
inprocess_get_dtdebug(void *hndl
, uintptr_t *ret_val
);
59 static int inprocess_loadobj_iter(void *opq
, tnfctl_ind_obj_f
*obj_func
,
63 * Cause interposition on dlclose() and dlopen()
65 #pragma weak dlclose = _tnfctl_dlclose
67 #pragma weak dlopen = _tnfctl_dlopen
70 * The lock used to protect the _tnfctl_internal_tracing_flag variable.
73 mutex_t _tnfctl_internalguard_lock
= DEFAULTMUTEX
;
74 boolean_t _tnfctl_internal_tracing_flag
= 0;
75 pid_t _tnfctl_externally_traced_pid
= NOPID
;
78 * Returns a pointer to a tnfctl handle that can do in process probe control.
81 tnfctl_internal_open(tnfctl_handle_t
**ret_val
)
84 tnfctl_errcode_t prexstat
;
87 /* allocate hdl and zero fill */
88 hdl
= calloc(1, sizeof (*hdl
));
90 return (TNFCTL_ERR_ALLOCFAIL
);
93 hdl
->mode
= INTERNAL_MODE
;
94 hdl
->called_exit
= B_FALSE
;
96 /* plug in inprocess call back functions */
97 hdl
->p_read
= inprocess_read
;
98 hdl
->p_write
= inprocess_write
;
99 hdl
->p_obj_iter
= inprocess_loadobj_iter
;
100 hdl
->p_getpid
= inprocess_getpid
;
103 * get the address of DT_DEBUG and store it in proc_p
104 * (the handle on the same process is the dbg address)
106 prexstat
= inprocess_get_dtdebug(hdl
, &dbgaddr
);
111 hdl
->proc_p
= (void *) dbgaddr
;
113 /* initialize state in handle */
114 prexstat
= _tnfctl_set_state(hdl
);
119 /* see if process is already being traced */
120 prexstat
= _tnfctl_internal_getlock();
126 return (TNFCTL_ERR_NONE
);
130 * reads a block of memory from the same address space.
133 inprocess_read(void *ignore
, uintptr_t addr
, void *buf
, size_t size
)
136 DBG_TNF_PROBE_2(inprocess_read_1
, "libtnfctl", "sunw%verbosity 3;",
137 tnf_long
, num_bytes
, size
,
138 tnf_opaque
, from_address
, addr
);
140 (void) memcpy(buf
, (void *) addr
, size
);
145 * writes a block of memory to the same address space.
148 inprocess_write(void *ignore
, uintptr_t addr
, void *buf
, size_t size
)
151 DBG_TNF_PROBE_2(inprocess_write_1
, "libtnfctl", "sunw%verbosity 3;",
152 tnf_long
, num_bytes
, size
,
153 tnf_opaque
, to_address
, addr
);
155 (void) memcpy((void *)addr
, buf
, size
);
160 * returns the pid of the process.
163 inprocess_getpid(void *ignore
)
167 extern Elf3264_Dyn _DYNAMIC
;
170 * returns the address of the DT_DEBUG field in the _DYNAMIC array
171 * of the same address space.
173 static tnfctl_errcode_t
174 inprocess_get_dtdebug(void *hndl
, uintptr_t *ret_val
)
176 Elf3264_Dyn
*dyn
= &_DYNAMIC
;
179 for (dp
= dyn
; dp
->d_tag
!= DT_NULL
; dp
++) {
180 if (dp
->d_tag
== DT_DEBUG
) {
181 *ret_val
= (uintptr_t) dp
;
182 return (TNFCTL_ERR_NONE
);
185 return (TNFCTL_ERR_INTERNAL
);
188 #define PROCFORMAT "/proc/%d"
191 * iterate over all loadobjects in the same address space calling the
192 * callback function "obj_func".
195 inprocess_loadobj_iter(void *opq
, tnfctl_ind_obj_f
*obj_func
, void *cd
)
197 Elf3264_Dyn
*dtdebug
= opq
;
198 struct r_debug
*r_dbg
;
199 struct link_map
*lmap
;
200 char path
[MAXPATHLEN
];
202 tnfctl_ind_obj_info_t loadobj
;
203 int retval
= 0; /* sucessful return */
205 DBG_TNF_PROBE_0(inprocess_loadobj_iter_start
, "libtnfctl",
206 "start inprocess_loadobj_iter; sunw%verbosity 1");
208 r_dbg
= (struct r_debug
*)dtdebug
->d_un
.d_ptr
;
210 DBG_TNF_PROBE_1(inprocess_loadobj_iter_1
, "libtnfctl",
212 tnf_string
, link_map_state
,
213 (r_dbg
->r_state
== RT_CONSISTENT
) ? "RT_CONSISTENT" :
214 (r_dbg
->r_state
== RT_ADD
) ? "RT_ADD" : "RT_DELETE");
216 /* bail if link map is not consistent */
217 if (r_dbg
->r_state
!= RT_CONSISTENT
)
220 (void) sprintf(path
, PROCFORMAT
, (int) getpid());
223 * opening /proc readonly, so debuggers can still run
224 * We use /proc in order to get fd on the object.
226 procfd
= open(path
, O_RDONLY
);
230 for (lmap
= r_dbg
->r_map
; lmap
; lmap
= lmap
->l_next
) {
231 loadobj
.text_base
= lmap
->l_addr
;
232 loadobj
.data_base
= lmap
->l_addr
;
233 loadobj
.objname
= lmap
->l_name
;
235 * client of this interface should deal with -1 for objfd,
236 * so no error checking is needed on this ioctl
238 loadobj
.objfd
= ioctl(procfd
, PIOCOPENM
, &(lmap
->l_addr
));
240 retval
= obj_func(opq
, &loadobj
, cd
);
243 if (loadobj
.objfd
!= -1)
244 close(loadobj
.objfd
);
246 /* check for error */
254 DBG_TNF_PROBE_0(inprocess_loadobj_iter_end
, "libtnfctl",
255 "end inprocess_loadobj_iter; sunw%verbosity 1");
260 * The lock that prevents a thread from accessing our cached library list
261 * and a dlopen or dlclose happening at the same time in another thread.
263 mutex_t _tnfctl_lmap_lock
= DEFAULTMUTEX
;
266 * The flag that indicates that the library list has changed via a
269 boolean_t _tnfctl_libs_changed
= B_FALSE
;
272 * Thread id of the owner of the lock in order to implement a
273 * recursive lock i.e. no deadlock if the same thread tries to lock
274 * a lock it already holds.
276 static thread_t lock_holder
= 0; /* XXX - no tid with 0 */
278 #define LMAP_LOCK (&_tnfctl_lmap_lock)
281 * dlclose interposition with a recursive lock so that a .fini section
282 * can recursively call dlopen or dlclose while holding _tnfctl_lmap_lock
283 * This interposition serializes access to rtld's loadobject list and
284 * also updates the flag _tnfctl_libs_changed to indicate a change in
285 * the library list. This flag is checked by operations that update
286 * probes so that it can sync up with the new library list and potential
287 * new/deleted probes.
290 _tnfctl_dlclose(void *handle
)
292 static int (*real_dlclose
)(void *handle
) = NULL
;
296 if (real_dlclose
== NULL
) {
297 real_dlclose
= (int (*)(void *)) dlsym(RTLD_NEXT
, "dlclose");
299 assert(real_dlclose
);
301 if (mutex_trylock(LMAP_LOCK
) != 0) {
302 /* don't have lock */
304 if (tid
== lock_holder
) {
305 /* recursive dlopen/dlclose by same thread */
306 return ((*real_dlclose
)(handle
));
308 /* not a recursive dlopen/dlclose - wait on lock */
309 mutex_lock(LMAP_LOCK
);
312 /* lock is held now */
313 lock_holder
= thr_self();
314 retval
= (*real_dlclose
)(handle
);
317 * reset lock_holder so that if _tnfctl_lmap_lock is held by some
318 * other part of the code, we don't assume it is a recursive
322 _tnfctl_libs_changed
= B_TRUE
;
323 mutex_unlock(LMAP_LOCK
);
329 * dlopen interposition with a recursive lock so that a .init section
330 * can recursively call dlopen or dlclose while holding _tnfctl_lmap_lock
331 * This interposition serializes access to rtld's loadobject list and
332 * also updates the flag _tnfctl_libs_changed to indicate a change in
333 * the library list. This flag is checked by operations that update
334 * probes so that it can sync up with the new library list and potential
335 * new/deleted probes.
338 _tnfctl_dlopen(const char *pathname
, int mode
)
340 static void * (*real_dlopen
)(const char *, int) = NULL
;
344 if (real_dlopen
== NULL
) {
345 real_dlopen
= (void * (*)(const char *, int))
346 dlsym(RTLD_NEXT
, "dlopen");
350 if (mutex_trylock(LMAP_LOCK
) != 0) {
351 /* don't have lock */
353 if (tid
== lock_holder
) {
354 /* recursive dlopen/dlclose by same thread */
355 return ((*real_dlopen
)(pathname
, mode
));
357 /* not a recursive dlopen/dlclose - wait on lock */
358 mutex_lock(LMAP_LOCK
);
361 /* lock is held now */
362 lock_holder
= thr_self();
363 retval
= (*real_dlopen
)(pathname
, mode
);
366 * reset lock_holder so that if _tnfctl_lmap_lock is held by some
367 * other part of the code, we don't assume it is a recursive
371 _tnfctl_libs_changed
= B_TRUE
;
372 mutex_unlock(LMAP_LOCK
);
378 _tnfctl_internal_getlock()
380 mutex_lock(&_tnfctl_internalguard_lock
);
381 if (_tnfctl_internal_tracing_flag
== 1) {
382 /* internal trace control active */
383 mutex_unlock(&_tnfctl_internalguard_lock
);
384 return (TNFCTL_ERR_BUSY
);
386 _tnfctl_internal_tracing_flag
= 1;
387 if (_tnfctl_externally_traced_pid
== getpid()) {
388 /* external trace control is active */
389 _tnfctl_internal_tracing_flag
= 0;
390 mutex_unlock(&_tnfctl_internalguard_lock
);
391 return (TNFCTL_ERR_BUSY
);
393 DBG((void) fprintf(stderr
, "_tnfctl_internal_getlock: ok to trace %d\n",
395 mutex_unlock(&_tnfctl_internalguard_lock
);
396 return (TNFCTL_ERR_NONE
);