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