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