1 /* Trace calls through PLTs and show caller, callee, and parameters.
2    Copyright (C) 2011-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <error.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/param.h>
26 #include <sys/uio.h>
27 
28 #include <ldsodefs.h>
29 
30 
31 extern const char *__progname;
32 extern const char *__progname_full;
33 
34 
35 /* List of objects to trace calls from.  */
36 static const char *fromlist;
37 /* List of objects to trace calls to.  */
38 static const char *tolist;
39 
40 /* If non-zero, also trace returns of the calls.  */
41 static int do_exit;
42 /* If non-zero print PID for each line.  */
43 static int print_pid;
44 
45 /* The output stream to use.  */
46 static FILE *out_file;
47 
48 
49 static int
match_pid(pid_t pid,const char * which)50 match_pid (pid_t pid, const char *which)
51 {
52   if (which == NULL || which[0] == '\0')
53     {
54       print_pid = 1;
55       return 1;
56     }
57 
58   char *endp;
59   unsigned long n = strtoul (which, &endp, 0);
60   return *endp == '\0' && n == pid;
61 }
62 
63 
64 static void
init(void)65 init (void)
66 {
67   fromlist = getenv ("SOTRUSS_FROMLIST");
68   if (fromlist != NULL && fromlist[0] == '\0')
69     fromlist = NULL;
70   tolist = getenv ("SOTRUSS_TOLIST");
71   if (tolist != NULL && tolist[0] == '\0')
72     tolist = NULL;
73   do_exit = (getenv ("SOTRUSS_EXIT") ?: "")[0] != '\0';
74 
75   /* Determine whether this process is supposed to be traced and if
76      yes, whether we should print into a file.  */
77   const char *which_process = getenv ("SOTRUSS_WHICH");
78   pid_t pid = getpid ();
79   int out_fd = -1;
80   if (match_pid (pid, which_process))
81     {
82       const char *out_filename = getenv ("SOTRUSS_OUTNAME");
83 
84       if (out_filename != NULL && out_filename[0] != 0)
85 	{
86 	  size_t out_filename_len = strlen (out_filename) + 13;
87 	  char fullname[out_filename_len];
88 	  char *endp = stpcpy (fullname, out_filename);
89 	  if (which_process == NULL || which_process[0] == '\0')
90 	    snprintf (endp, 13, ".%ld", (long int) pid);
91 
92 	  out_fd = open64 (fullname, O_RDWR | O_CREAT | O_TRUNC, 0666);
93 	  if (out_fd != -1)
94 	    print_pid = 0;
95 	}
96     }
97 
98   /* If we do not write into a file write to stderr.  Duplicate the
99      descriptor so that we can keep printing in case the program
100      closes stderr.  Try first to allocate a descriptor with a value
101      usually not used as to minimize interference with the
102      program.  */
103   if (out_fd == -1)
104     {
105       out_fd = fcntl64 (STDERR_FILENO, F_DUPFD, 1000);
106       if (out_fd == -1)
107 	out_fd = dup (STDERR_FILENO);
108     }
109 
110   if (out_fd != -1)
111     {
112       /* Convert file descriptor into a stream.  */
113       out_file = fdopen (out_fd, "w");
114       if (out_file != NULL)
115 	setlinebuf (out_file);
116     }
117 }
118 
119 
120 /* Audit interface verification.  We also initialize everything if
121    everything checks out OK.  */
122 unsigned int
la_version(unsigned int v)123 la_version (unsigned int v)
124 {
125   if (v != LAV_CURRENT)
126     error (1, 0, "cannot handle interface version %u", v);
127 
128   init ();
129 
130   return v;
131 }
132 
133 
134 /* Check whether a file name is on the colon-separated list of file
135    names.  */
136 static unsigned int
match_file(const char * list,const char * name,size_t name_len,unsigned int mask)137 match_file (const char *list, const char *name, size_t name_len,
138 	    unsigned int mask)
139 {
140   if (list[0] == '\0')
141     return 0;
142 
143   const char *cp = list;
144   while (1)
145     {
146       if (strncmp (cp, name, name_len) == 0
147 	  && (cp[name_len] == ':' || cp[name_len] == '\0'))
148 	return mask;
149 
150       cp = strchr (cp, ':');
151       if (cp == NULL)
152 	return 0;
153       ++cp;
154     }
155 }
156 
157 
158 unsigned int
la_objopen(struct link_map * map,Lmid_t lmid,uintptr_t * cookie)159 la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
160 {
161   if (out_file == NULL)
162     return 0;
163 
164   const char *full_name = map->l_name ?: "";
165   if (full_name[0] == '\0')
166     full_name = __progname_full;
167   size_t full_name_len = strlen (full_name);
168   const char *base_name = basename (full_name);
169   if (base_name[0] == '\0')
170     base_name = __progname;
171   size_t base_name_len = strlen (base_name);
172 
173   int result = 0;
174   const char *print_name = NULL;
175   for (struct libname_list *l = map->l_libname; l != NULL; l = l->next)
176     {
177       if (print_name == NULL || (print_name[0] == '/' && l->name[0] != '/'))
178 	print_name = l->name;
179 
180       if (fromlist != NULL)
181 	result |= match_file (fromlist, l->name, strlen (l->name),
182 			      LA_FLG_BINDFROM);
183 
184       if (tolist != NULL)
185 	result |= match_file (tolist, l->name, strlen (l->name),LA_FLG_BINDTO);
186     }
187 
188   if (print_name == NULL)
189     print_name = base_name;
190   if (print_name[0] == '\0')
191     print_name = __progname;
192 
193   /* We cannot easily get to the object name in the PLT handling
194      functions.  Use the cookie to get the string pointer passed back
195      to us.  */
196   *cookie = (uintptr_t) print_name;
197 
198   /* The object name has to be on the list of objects to trace calls
199      from or that list must be empty.  In the latter case we trace
200      only calls from the main binary.  */
201   if (fromlist == NULL)
202     result |= map->l_name[0] == '\0' ? LA_FLG_BINDFROM : 0;
203   else
204     result |= (match_file (fromlist, full_name, full_name_len,
205 			   LA_FLG_BINDFROM)
206 	       | match_file (fromlist, base_name, base_name_len,
207 			     LA_FLG_BINDFROM));
208 
209   /* The object name has to be on the list of objects to trace calls
210      to or that list must be empty.  In the latter case we trace
211      calls toall objects.  */
212   if (tolist == NULL)
213     result |= LA_FLG_BINDTO;
214   else
215     result |= (match_file (tolist, full_name, full_name_len, LA_FLG_BINDTO)
216 	       | match_file (tolist, base_name, base_name_len, LA_FLG_BINDTO));
217 
218   return result;
219 }
220 
221 
222 #if __ELF_NATIVE_CLASS == 32
223 # define la_symbind la_symbind32
224 typedef Elf32_Sym Elf_Sym;
225 #else
226 # define la_symbind la_symbind64
227 typedef Elf64_Sym Elf_Sym;
228 #endif
229 
230 uintptr_t
la_symbind(Elf_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,unsigned int * flags,const char * symname)231 la_symbind (Elf_Sym *sym, unsigned int ndx, uintptr_t *refcook,
232 	    uintptr_t *defcook, unsigned int *flags, const char *symname)
233 {
234   if (!do_exit)
235     *flags = LA_SYMB_NOPLTEXIT;
236 
237   return sym->st_value;
238 }
239 
240 
241 static void
print_enter(uintptr_t * refcook,uintptr_t * defcook,const char * symname,unsigned long int reg1,unsigned long int reg2,unsigned long int reg3,unsigned int flags)242 print_enter (uintptr_t *refcook, uintptr_t *defcook, const char *symname,
243 	     unsigned long int reg1, unsigned long int reg2,
244 	     unsigned long int reg3, unsigned int flags)
245 {
246   char buf[3 * sizeof (pid_t) + 3];
247   buf[0] = '\0';
248   if (print_pid)
249     snprintf (buf, sizeof (buf), "%5ld: ", (long int) getpid ());
250 
251   fprintf (out_file, "%s%15s -> %-15s:%s%s(0x%lx, 0x%lx, 0x%lx)\n",
252 	   buf, (char *) *refcook, (char *) *defcook,
253 	   (flags & LA_SYMB_NOPLTEXIT) ? "*" : " ", symname, reg1, reg2, reg3);
254 }
255 
256 
257 #ifdef __i386__
258 Elf32_Addr
la_i86_gnu_pltenter(Elf32_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,La_i86_regs * regs,unsigned int * flags,const char * symname,long int * framesizep)259 la_i86_gnu_pltenter (Elf32_Sym *sym __attribute__ ((unused)),
260 		     unsigned int ndx __attribute__ ((unused)),
261 		     uintptr_t *refcook, uintptr_t *defcook,
262 		     La_i86_regs *regs, unsigned int *flags,
263 		     const char *symname, long int *framesizep)
264 {
265   unsigned long int *sp = (unsigned long int *) regs->lr_esp;
266 
267   print_enter (refcook, defcook, symname, sp[1], sp[2], sp[3], *flags);
268 
269   /* No need to copy anything, we will not need the parameters in any case.  */
270   *framesizep = 0;
271 
272   return sym->st_value;
273 }
274 #elif defined __x86_64__
275 Elf64_Addr
la_x86_64_gnu_pltenter(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,La_x86_64_regs * regs,unsigned int * flags,const char * symname,long int * framesizep)276 la_x86_64_gnu_pltenter (Elf64_Sym *sym __attribute__ ((unused)),
277 			unsigned int ndx __attribute__ ((unused)),
278 			uintptr_t *refcook, uintptr_t *defcook,
279 			La_x86_64_regs *regs, unsigned int *flags,
280 			const char *symname, long int *framesizep)
281 {
282   print_enter (refcook, defcook, symname,
283 	       regs->lr_rdi, regs->lr_rsi, regs->lr_rdx, *flags);
284 
285   /* No need to copy anything, we will not need the parameters in any case.  */
286   *framesizep = 0;
287 
288   return sym->st_value;
289 }
290 #elif defined __sparc__ && !defined __arch64__
291 Elf32_Addr
la_sparc32_gnu_pltenter(Elf32_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,La_sparc32_regs * regs,unsigned int * flags,const char * symname,long int * framesizep)292 la_sparc32_gnu_pltenter (Elf32_Sym *sym __attribute__ ((unused)),
293 			 unsigned int ndx __attribute__ ((unused)),
294 			 uintptr_t *refcook, uintptr_t *defcook,
295 			 La_sparc32_regs *regs, unsigned int *flags,
296 			 const char *symname, long int *framesizep)
297 {
298   print_enter (refcook, defcook, symname,
299 	       regs->lr_reg[0], regs->lr_reg[1], regs->lr_reg[2],
300 	       *flags);
301 
302   /* No need to copy anything, we will not need the parameters in any case.  */
303   *framesizep = 0;
304 
305   return sym->st_value;
306 }
307 #elif defined __sparc__ && defined __arch64__
308 Elf64_Addr
la_sparc64_gnu_pltenter(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,La_sparc64_regs * regs,unsigned int * flags,const char * symname,long int * framesizep)309 la_sparc64_gnu_pltenter (Elf64_Sym *sym __attribute__ ((unused)),
310 			 unsigned int ndx __attribute__ ((unused)),
311 			 uintptr_t *refcook, uintptr_t *defcook,
312 			 La_sparc64_regs *regs, unsigned int *flags,
313 			 const char *symname, long int *framesizep)
314 {
315   print_enter (refcook, defcook, symname,
316 	       regs->lr_reg[0], regs->lr_reg[1], regs->lr_reg[2],
317 	       *flags);
318 
319   /* No need to copy anything, we will not need the parameters in any case.  */
320   *framesizep = 0;
321 
322   return sym->st_value;
323 }
324 #elif !defined HAVE_ARCH_PLTENTER
325 # warning "pltenter for architecture not supported"
326 #endif
327 
328 
329 static void
print_exit(uintptr_t * refcook,uintptr_t * defcook,const char * symname,unsigned long int reg)330 print_exit (uintptr_t *refcook, uintptr_t *defcook, const char *symname,
331 	    unsigned long int reg)
332 {
333   char buf[3 * sizeof (pid_t) + 3];
334   buf[0] = '\0';
335   if (print_pid)
336     snprintf (buf, sizeof (buf), "%5ld: ", (long int) getpid ());
337 
338   fprintf (out_file, "%s%15s -> %-15s:%s%s - 0x%lx\n",
339 	   buf, (char *) *refcook, (char *) *defcook, " ", symname, reg);
340 }
341 
342 
343 #ifdef __i386__
344 unsigned int
la_i86_gnu_pltexit(Elf32_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,const struct La_i86_regs * inregs,struct La_i86_retval * outregs,const char * symname)345 la_i86_gnu_pltexit (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
346 		    uintptr_t *defcook, const struct La_i86_regs *inregs,
347 		    struct La_i86_retval *outregs, const char *symname)
348 {
349   print_exit (refcook, defcook, symname, outregs->lrv_eax);
350 
351   return 0;
352 }
353 #elif defined __x86_64__
354 unsigned int
la_x86_64_gnu_pltexit(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,const struct La_x86_64_regs * inregs,struct La_x86_64_retval * outregs,const char * symname)355 la_x86_64_gnu_pltexit (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
356 		       uintptr_t *defcook, const struct La_x86_64_regs *inregs,
357 		       struct La_x86_64_retval *outregs, const char *symname)
358 {
359   print_exit (refcook, defcook, symname, outregs->lrv_rax);
360 
361   return 0;
362 }
363 #elif defined __sparc__ && !defined __arch64__
364 unsigned int
la_sparc32_gnu_pltexit(Elf32_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,const struct La_sparc32_regs * inregs,struct La_sparc32_retval * outregs,const char * symname)365 la_sparc32_gnu_pltexit (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
366 			uintptr_t *defcook, const struct La_sparc32_regs *inregs,
367 			struct La_sparc32_retval *outregs, const char *symname)
368 {
369   print_exit (refcook, defcook, symname, outregs->lrv_reg[0]);
370 
371   return 0;
372 }
373 #elif defined __sparc__ && defined __arch64__
374 unsigned int
la_sparc64_gnu_pltexit(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,const struct La_sparc64_regs * inregs,struct La_sparc64_retval * outregs,const char * symname)375 la_sparc64_gnu_pltexit (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
376 			uintptr_t *defcook, const struct La_sparc64_regs *inregs,
377 			struct La_sparc64_retval *outregs, const char *symname)
378 {
379   print_exit (refcook, defcook, symname, outregs->lrv_reg[0]);
380 
381   return 0;
382 }
383 #elif !defined HAVE_ARCH_PLTEXIT
384 # warning "pltexit for architecture not supported"
385 #endif
386