1 /*-
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <dlfcn.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <sysdecode.h>
39 #include "rtld_utrace.h"
40
41 #ifdef __LP64__
42 struct utrace_rtld32 {
43 char sig[4];
44 int event;
45 uint32_t handle;
46 uint32_t mapbase;
47 uint32_t mapsize;
48 int refcnt;
49 char name[MAXPATHLEN];
50 };
51 #endif
52
53 static int
print_utrace_rtld(FILE * fp,void * p)54 print_utrace_rtld(FILE *fp, void *p)
55 {
56 struct utrace_rtld *ut = p;
57 void *parent;
58 int mode;
59
60 switch (ut->event) {
61 case UTRACE_DLOPEN_START:
62 mode = ut->refcnt;
63 fprintf(fp, "dlopen(%s, ", ut->name);
64 switch (mode & RTLD_MODEMASK) {
65 case RTLD_NOW:
66 fprintf(fp, "RTLD_NOW");
67 break;
68 case RTLD_LAZY:
69 fprintf(fp, "RTLD_LAZY");
70 break;
71 default:
72 fprintf(fp, "%#x", mode & RTLD_MODEMASK);
73 }
74 if (mode & RTLD_GLOBAL)
75 fprintf(fp, " | RTLD_GLOBAL");
76 if (mode & RTLD_TRACE)
77 fprintf(fp, " | RTLD_TRACE");
78 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
79 fprintf(fp, " | %#x", mode &
80 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
81 fprintf(fp, ")");
82 break;
83 case UTRACE_DLOPEN_STOP:
84 fprintf(fp, "%p = dlopen(%s) ref %d", ut->handle, ut->name,
85 ut->refcnt);
86 break;
87 case UTRACE_DLCLOSE_START:
88 fprintf(fp, "dlclose(%p) (%s, %d)", ut->handle, ut->name,
89 ut->refcnt);
90 break;
91 case UTRACE_DLCLOSE_STOP:
92 fprintf(fp, "dlclose(%p) finished", ut->handle);
93 break;
94 case UTRACE_LOAD_OBJECT:
95 fprintf(fp, "RTLD: loaded %p @ %p - %p (%s)", ut->handle,
96 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
97 ut->name);
98 break;
99 case UTRACE_UNLOAD_OBJECT:
100 fprintf(fp, "RTLD: unloaded %p @ %p - %p (%s)", ut->handle,
101 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
102 ut->name);
103 break;
104 case UTRACE_ADD_RUNDEP:
105 parent = ut->mapbase;
106 fprintf(fp, "RTLD: %p now depends on %p (%s, %d)", parent,
107 ut->handle, ut->name, ut->refcnt);
108 break;
109 case UTRACE_PRELOAD_FINISHED:
110 fprintf(fp, "RTLD: LD_PRELOAD finished");
111 break;
112 case UTRACE_INIT_CALL:
113 fprintf(fp, "RTLD: init %p for %p (%s)", ut->mapbase, ut->handle,
114 ut->name);
115 break;
116 case UTRACE_FINI_CALL:
117 fprintf(fp, "RTLD: fini %p for %p (%s)", ut->mapbase, ut->handle,
118 ut->name);
119 break;
120 case UTRACE_DLSYM_START:
121 fprintf(fp, "RTLD: dlsym(%p, %s)", ut->handle, ut->name);
122 break;
123 case UTRACE_DLSYM_STOP:
124 fprintf(fp, "RTLD: %p = dlsym(%p, %s)", ut->mapbase, ut->handle,
125 ut->name);
126 break;
127 case UTRACE_RTLD_ERROR:
128 fprintf(fp, "RTLD: error: %s\n", ut->name);
129 break;
130
131 default:
132 return (0);
133 }
134 return (1);
135 }
136
137 struct utrace_malloc {
138 void *p;
139 size_t s;
140 void *r;
141 };
142
143 #ifdef __LP64__
144 struct utrace_malloc32 {
145 uint32_t p;
146 uint32_t s;
147 uint32_t r;
148 };
149 #endif
150
151 static void
print_utrace_malloc(FILE * fp,void * p)152 print_utrace_malloc(FILE *fp, void *p)
153 {
154 struct utrace_malloc *ut = p;
155
156 if (ut->p == (void *)(intptr_t)(-1))
157 fprintf(fp, "malloc_init()");
158 else if (ut->s == 0)
159 fprintf(fp, "free(%p)", ut->p);
160 else if (ut->p == NULL)
161 fprintf(fp, "%p = malloc(%zu)", ut->r, ut->s);
162 else
163 fprintf(fp, "%p = realloc(%p, %zu)", ut->r, ut->p, ut->s);
164 }
165
166 int
sysdecode_utrace(FILE * fp,void * p,size_t len)167 sysdecode_utrace(FILE *fp, void *p, size_t len)
168 {
169 #ifdef __LP64__
170 struct utrace_rtld ur;
171 struct utrace_rtld32 *pr;
172 struct utrace_malloc um;
173 struct utrace_malloc32 *pm;
174 #endif
175 static const char rtld_utrace_sig[RTLD_UTRACE_SIG_SZ] = RTLD_UTRACE_SIG;
176
177 if (len == sizeof(struct utrace_rtld) && bcmp(p, rtld_utrace_sig,
178 sizeof(rtld_utrace_sig)) == 0)
179 return (print_utrace_rtld(fp, p));
180
181 if (len == sizeof(struct utrace_malloc)) {
182 print_utrace_malloc(fp, p);
183 return (1);
184 }
185
186 #ifdef __LP64__
187 if (len == sizeof(struct utrace_rtld32) && bcmp(p, rtld_utrace_sig,
188 sizeof(rtld_utrace_sig)) == 0) {
189 pr = p;
190 memset(&ur, 0, sizeof(ur));
191 memcpy(ur.sig, pr->sig, sizeof(ur.sig));
192 ur.event = pr->event;
193 ur.handle = (void *)(uintptr_t)pr->handle;
194 ur.mapbase = (void *)(uintptr_t)pr->mapbase;
195 ur.mapsize = pr->mapsize;
196 ur.refcnt = pr->refcnt;
197 memcpy(ur.name, pr->name, sizeof(ur.name));
198 return (print_utrace_rtld(fp, &ur));
199 }
200
201 if (len == sizeof(struct utrace_malloc32)) {
202 pm = p;
203 memset(&um, 0, sizeof(um));
204 um.p = pm->p == (uint32_t)-1 ? (void *)(intptr_t)-1 :
205 (void *)(uintptr_t)pm->p;
206 um.s = pm->s;
207 um.r = (void *)(uintptr_t)pm->r;
208 print_utrace_malloc(fp, &um);
209 return (1);
210 }
211 #endif
212
213 return (0);
214 }
215