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 <strings.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 static int 63 print_utrace_rtld(FILE *fp, void *p) 64 { 65 struct utrace_rtld *ut = p; 66 void *parent; 67 int mode; 68 69 switch (ut->event) { 70 case UTRACE_DLOPEN_START: 71 mode = ut->refcnt; 72 fprintf(fp, "dlopen(%s, ", ut->name); 73 switch (mode & RTLD_MODEMASK) { 74 case RTLD_NOW: 75 fprintf(fp, "RTLD_NOW"); 76 break; 77 case RTLD_LAZY: 78 fprintf(fp, "RTLD_LAZY"); 79 break; 80 default: 81 fprintf(fp, "%#x", mode & RTLD_MODEMASK); 82 } 83 if (mode & RTLD_GLOBAL) 84 fprintf(fp, " | RTLD_GLOBAL"); 85 if (mode & RTLD_TRACE) 86 fprintf(fp, " | RTLD_TRACE"); 87 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE)) 88 fprintf(fp, " | %#x", mode & 89 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE)); 90 fprintf(fp, ")"); 91 break; 92 case UTRACE_DLOPEN_STOP: 93 fprintf(fp, "%p = dlopen(%s) ref %d", ut->handle, ut->name, 94 ut->refcnt); 95 break; 96 case UTRACE_DLCLOSE_START: 97 fprintf(fp, "dlclose(%p) (%s, %d)", ut->handle, ut->name, 98 ut->refcnt); 99 break; 100 case UTRACE_DLCLOSE_STOP: 101 fprintf(fp, "dlclose(%p) finished", ut->handle); 102 break; 103 case UTRACE_LOAD_OBJECT: 104 fprintf(fp, "RTLD: loaded %p @ %p - %p (%s)", ut->handle, 105 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1, 106 ut->name); 107 break; 108 case UTRACE_UNLOAD_OBJECT: 109 fprintf(fp, "RTLD: unloaded %p @ %p - %p (%s)", ut->handle, 110 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1, 111 ut->name); 112 break; 113 case UTRACE_ADD_RUNDEP: 114 parent = ut->mapbase; 115 fprintf(fp, "RTLD: %p now depends on %p (%s, %d)", parent, 116 ut->handle, ut->name, ut->refcnt); 117 break; 118 case UTRACE_PRELOAD_FINISHED: 119 fprintf(fp, "RTLD: LD_PRELOAD finished"); 120 break; 121 case UTRACE_INIT_CALL: 122 fprintf(fp, "RTLD: init %p for %p (%s)", ut->mapbase, ut->handle, 123 ut->name); 124 break; 125 case UTRACE_FINI_CALL: 126 fprintf(fp, "RTLD: fini %p for %p (%s)", ut->mapbase, ut->handle, 127 ut->name); 128 break; 129 case UTRACE_DLSYM_START: 130 fprintf(fp, "RTLD: dlsym(%p, %s)", ut->handle, ut->name); 131 break; 132 case UTRACE_DLSYM_STOP: 133 fprintf(fp, "RTLD: %p = dlsym(%p, %s)", ut->mapbase, ut->handle, 134 ut->name); 135 break; 136 default: 137 return (0); 138 } 139 return (1); 140 } 141 142 struct utrace_malloc { 143 void *p; 144 size_t s; 145 void *r; 146 }; 147 148 static void 149 print_utrace_malloc(FILE *fp, void *p) 150 { 151 struct utrace_malloc *ut = p; 152 153 if (ut->p == (void *)(intptr_t)(-1)) 154 fprintf(fp, "malloc_init()"); 155 else if (ut->s == 0) 156 fprintf(fp, "free(%p)", ut->p); 157 else if (ut->p == NULL) 158 fprintf(fp, "%p = malloc(%zu)", ut->r, ut->s); 159 else 160 fprintf(fp, "%p = realloc(%p, %zu)", ut->r, ut->p, ut->s); 161 } 162 163 int 164 sysdecode_utrace(FILE *fp, void *p, size_t len) 165 { 166 167 if (len == sizeof(struct utrace_rtld) && bcmp(p, "RTLD", 4) == 0) { 168 return (print_utrace_rtld(fp, p)); 169 } 170 171 if (len == sizeof(struct utrace_malloc)) { 172 print_utrace_malloc(fp, p); 173 return (1); 174 } 175 176 return (0); 177 } 178