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 * 4. 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 <stdio.h> 36 #include <string.h> 37 #include <sysdecode.h> 38 39 #define UTRACE_DLOPEN_START 1 40 #define UTRACE_DLOPEN_STOP 2 41 #define UTRACE_DLCLOSE_START 3 42 #define UTRACE_DLCLOSE_STOP 4 43 #define UTRACE_LOAD_OBJECT 5 44 #define UTRACE_UNLOAD_OBJECT 6 45 #define UTRACE_ADD_RUNDEP 7 46 #define UTRACE_PRELOAD_FINISHED 8 47 #define UTRACE_INIT_CALL 9 48 #define UTRACE_FINI_CALL 10 49 #define UTRACE_DLSYM_START 11 50 #define UTRACE_DLSYM_STOP 12 51 52 struct utrace_rtld { 53 char sig[4]; /* 'RTLD' */ 54 int event; 55 void *handle; 56 void *mapbase; 57 size_t mapsize; 58 int refcnt; 59 char name[MAXPATHLEN]; 60 }; 61 62 #ifdef __LP64__ 63 struct utrace_rtld32 { 64 char sig[4]; /* 'RTLD' */ 65 int event; 66 uint32_t handle; 67 uint32_t mapbase; 68 uint32_t mapsize; 69 int refcnt; 70 char name[MAXPATHLEN]; 71 }; 72 #endif 73 74 static int 75 print_utrace_rtld(FILE *fp, void *p) 76 { 77 struct utrace_rtld *ut = p; 78 void *parent; 79 int mode; 80 81 switch (ut->event) { 82 case UTRACE_DLOPEN_START: 83 mode = ut->refcnt; 84 fprintf(fp, "dlopen(%s, ", ut->name); 85 switch (mode & RTLD_MODEMASK) { 86 case RTLD_NOW: 87 fprintf(fp, "RTLD_NOW"); 88 break; 89 case RTLD_LAZY: 90 fprintf(fp, "RTLD_LAZY"); 91 break; 92 default: 93 fprintf(fp, "%#x", mode & RTLD_MODEMASK); 94 } 95 if (mode & RTLD_GLOBAL) 96 fprintf(fp, " | RTLD_GLOBAL"); 97 if (mode & RTLD_TRACE) 98 fprintf(fp, " | RTLD_TRACE"); 99 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE)) 100 fprintf(fp, " | %#x", mode & 101 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE)); 102 fprintf(fp, ")"); 103 break; 104 case UTRACE_DLOPEN_STOP: 105 fprintf(fp, "%p = dlopen(%s) ref %d", ut->handle, ut->name, 106 ut->refcnt); 107 break; 108 case UTRACE_DLCLOSE_START: 109 fprintf(fp, "dlclose(%p) (%s, %d)", ut->handle, ut->name, 110 ut->refcnt); 111 break; 112 case UTRACE_DLCLOSE_STOP: 113 fprintf(fp, "dlclose(%p) finished", ut->handle); 114 break; 115 case UTRACE_LOAD_OBJECT: 116 fprintf(fp, "RTLD: loaded %p @ %p - %p (%s)", ut->handle, 117 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1, 118 ut->name); 119 break; 120 case UTRACE_UNLOAD_OBJECT: 121 fprintf(fp, "RTLD: unloaded %p @ %p - %p (%s)", ut->handle, 122 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1, 123 ut->name); 124 break; 125 case UTRACE_ADD_RUNDEP: 126 parent = ut->mapbase; 127 fprintf(fp, "RTLD: %p now depends on %p (%s, %d)", parent, 128 ut->handle, ut->name, ut->refcnt); 129 break; 130 case UTRACE_PRELOAD_FINISHED: 131 fprintf(fp, "RTLD: LD_PRELOAD finished"); 132 break; 133 case UTRACE_INIT_CALL: 134 fprintf(fp, "RTLD: init %p for %p (%s)", ut->mapbase, ut->handle, 135 ut->name); 136 break; 137 case UTRACE_FINI_CALL: 138 fprintf(fp, "RTLD: fini %p for %p (%s)", ut->mapbase, ut->handle, 139 ut->name); 140 break; 141 case UTRACE_DLSYM_START: 142 fprintf(fp, "RTLD: dlsym(%p, %s)", ut->handle, ut->name); 143 break; 144 case UTRACE_DLSYM_STOP: 145 fprintf(fp, "RTLD: %p = dlsym(%p, %s)", ut->mapbase, ut->handle, 146 ut->name); 147 break; 148 default: 149 return (0); 150 } 151 return (1); 152 } 153 154 struct utrace_malloc { 155 void *p; 156 size_t s; 157 void *r; 158 }; 159 160 #ifdef __LP64__ 161 struct utrace_malloc32 { 162 uint32_t p; 163 uint32_t s; 164 uint32_t r; 165 }; 166 #endif 167 168 static void 169 print_utrace_malloc(FILE *fp, void *p) 170 { 171 struct utrace_malloc *ut = p; 172 173 if (ut->p == (void *)(intptr_t)(-1)) 174 fprintf(fp, "malloc_init()"); 175 else if (ut->s == 0) 176 fprintf(fp, "free(%p)", ut->p); 177 else if (ut->p == NULL) 178 fprintf(fp, "%p = malloc(%zu)", ut->r, ut->s); 179 else 180 fprintf(fp, "%p = realloc(%p, %zu)", ut->r, ut->p, ut->s); 181 } 182 183 int 184 sysdecode_utrace(FILE *fp, void *p, size_t len) 185 { 186 #ifdef __LP64__ 187 struct utrace_rtld ur; 188 struct utrace_rtld32 *pr; 189 struct utrace_malloc um; 190 struct utrace_malloc32 *pm; 191 #endif 192 193 if (len == sizeof(struct utrace_rtld) && bcmp(p, "RTLD", 4) == 0) { 194 return (print_utrace_rtld(fp, p)); 195 } 196 197 if (len == sizeof(struct utrace_malloc)) { 198 print_utrace_malloc(fp, p); 199 return (1); 200 } 201 202 #ifdef __LP64__ 203 if (len == sizeof(struct utrace_rtld32) && bcmp(p, "RTLD", 4) == 0) { 204 pr = p; 205 memset(&ur, 0, sizeof(ur)); 206 memcpy(ur.sig, pr->sig, sizeof(ur.sig)); 207 ur.event = pr->event; 208 ur.handle = (void *)(uintptr_t)pr->handle; 209 ur.mapbase = (void *)(uintptr_t)pr->mapbase; 210 ur.mapsize = pr->mapsize; 211 ur.refcnt = pr->refcnt; 212 memcpy(ur.name, pr->name, sizeof(ur.name)); 213 return (print_utrace_rtld(fp, &ur)); 214 } 215 216 if (len == sizeof(struct utrace_malloc32)) { 217 pm = p; 218 memset(&um, 0, sizeof(um)); 219 um.p = pm->p == (uint32_t)-1 ? (void *)(intptr_t)-1 : 220 (void *)(uintptr_t)pm->p; 221 um.s = pm->s; 222 um.r = (void *)(uintptr_t)pm->r; 223 print_utrace_malloc(fp, &um); 224 return (1); 225 } 226 #endif 227 228 return (0); 229 } 230