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