Imported Upstream version 0.7.2
[platform/upstream/ltrace.git] / output.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2010 Joe Damato
5  * Copyright (C) 1997,1998,1999,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes
6  * Copyright (C) 2006 Paul Gilliam, IBM Corporation
7  * Copyright (C) 2006 Ian Wienand
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  */
24
25 /* glibc before 2.10, eglibc and uClibc all need _GNU_SOURCE defined
26  * for open_memstream to become visible.  */
27 #define _GNU_SOURCE
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <time.h>
36 #include <sys/time.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <assert.h>
40
41 #include "common.h"
42 #include "proc.h"
43 #include "library.h"
44 #include "type.h"
45 #include "value.h"
46 #include "value_dict.h"
47 #include "param.h"
48 #include "fetch.h"
49 #include "lens_default.h"
50
51 /* TODO FIXME XXX: include in common.h: */
52 extern struct timeval current_time_spent;
53
54 Dict *dict_opt_c = NULL;
55
56 static Process *current_proc = 0;
57 static size_t current_depth = 0;
58 static int current_column = 0;
59
60 static void
61 output_indent(struct Process *proc)
62 {
63         int d = options.indent * (proc->callstack_depth - 1);
64         current_column += fprintf(options.output, "%*s", d, "");
65 }
66
67 static void
68 begin_of_line(Process *proc, int is_func, int indent)
69 {
70         current_column = 0;
71         if (!proc) {
72                 return;
73         }
74         if ((options.output != stderr) && (opt_p || options.follow)) {
75                 current_column += fprintf(options.output, "%u ", proc->pid);
76         } else if (options.follow) {
77                 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
78         }
79         if (opt_r) {
80                 struct timeval tv;
81                 struct timezone tz;
82                 static struct timeval old_tv = { 0, 0 };
83                 struct timeval diff;
84
85                 gettimeofday(&tv, &tz);
86
87                 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
88                         old_tv.tv_sec = tv.tv_sec;
89                         old_tv.tv_usec = tv.tv_usec;
90                 }
91                 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
92                 if (tv.tv_usec >= old_tv.tv_usec) {
93                         diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
94                 } else {
95                         diff.tv_sec--;
96                         diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
97                 }
98                 old_tv.tv_sec = tv.tv_sec;
99                 old_tv.tv_usec = tv.tv_usec;
100                 current_column += fprintf(options.output, "%3lu.%06d ",
101                                           diff.tv_sec, (int)diff.tv_usec);
102         }
103         if (opt_t) {
104                 struct timeval tv;
105                 struct timezone tz;
106
107                 gettimeofday(&tv, &tz);
108                 if (opt_t > 2) {
109                         current_column += fprintf(options.output, "%lu.%06d ",
110                                                   tv.tv_sec, (int)tv.tv_usec);
111                 } else if (opt_t > 1) {
112                         struct tm *tmp = localtime(&tv.tv_sec);
113                         current_column +=
114                             fprintf(options.output, "%02d:%02d:%02d.%06d ",
115                                     tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
116                                     (int)tv.tv_usec);
117                 } else {
118                         struct tm *tmp = localtime(&tv.tv_sec);
119                         current_column += fprintf(options.output, "%02d:%02d:%02d ",
120                                                   tmp->tm_hour, tmp->tm_min,
121                                                   tmp->tm_sec);
122                 }
123         }
124         if (opt_i) {
125                 if (is_func)
126                         current_column += fprintf(options.output, "[%p] ",
127                                                   proc->return_addr);
128                 else
129                         current_column += fprintf(options.output, "[%p] ",
130                                                   proc->instruction_pointer);
131         }
132         if (options.indent > 0 && indent) {
133                 output_indent(proc);
134         }
135 }
136
137 static struct arg_type_info *
138 get_unknown_type(void)
139 {
140         static struct arg_type_info *info = NULL;
141         if (info == NULL) {
142                 info = malloc(sizeof(*info));
143                 if (info == NULL) {
144                         report_global_error("malloc: %s", strerror(errno));
145                         abort();
146                 }
147                 *info = *type_get_simple(ARGTYPE_LONG);
148                 info->lens = &guess_lens;
149         }
150         return info;
151 }
152
153 /* The default prototype is: long X(long, long, long, long).  */
154 static Function *
155 build_default_prototype(void)
156 {
157         Function *ret = malloc(sizeof(*ret));
158         size_t i = 0;
159         if (ret == NULL)
160                 goto err;
161         memset(ret, 0, sizeof(*ret));
162
163         struct arg_type_info *unknown_type = get_unknown_type();
164
165         ret->return_info = unknown_type;
166         ret->own_return_info = 0;
167
168         ret->num_params = 4;
169         ret->params = malloc(sizeof(*ret->params) * ret->num_params);
170         if (ret->params == NULL)
171                 goto err;
172
173         for (i = 0; i < ret->num_params; ++i)
174                 param_init_type(&ret->params[i], unknown_type, 0);
175
176         return ret;
177
178 err:
179         report_global_error("malloc: %s", strerror(errno));
180         if (ret->params != NULL) {
181                 while (i-- > 0)
182                         param_destroy(&ret->params[i]);
183                 free(ret->params);
184         }
185
186         free(ret);
187
188         return NULL;
189 }
190
191 static Function *
192 name2func(char const *name) {
193         Function *tmp;
194         const char *str1, *str2;
195
196         for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
197                 str1 = tmp->name;
198                 str2 = name;
199                 if (!strcmp(str1, str2))
200                         return tmp;
201         }
202
203         static Function *def = NULL;
204         if (def == NULL)
205                 def = build_default_prototype();
206
207         return def;
208 }
209
210 void
211 output_line(struct Process *proc, const char *fmt, ...)
212 {
213         if (options.summary)
214                 return;
215
216         if (current_proc != NULL) {
217                 if (current_proc->callstack[current_depth].return_addr)
218                         fprintf(options.output, " <unfinished ...>\n");
219                 else
220                         fprintf(options.output, " <no return ...>\n");
221         }
222         current_proc = NULL;
223         if (fmt == NULL)
224                 return;
225
226         begin_of_line(proc, 0, 0);
227
228         va_list args;
229         va_start(args, fmt);
230         vfprintf(options.output, fmt, args);
231         fprintf(options.output, "\n");
232         va_end(args);
233
234         current_column = 0;
235 }
236
237 static void
238 tabto(int col) {
239         if (current_column < col) {
240                 fprintf(options.output, "%*s", col - current_column, "");
241         }
242 }
243
244 static int
245 output_error(FILE *stream)
246 {
247         return fprintf(stream, "?");
248 }
249
250 static int
251 fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context,
252                    struct value_dict *arguments,
253                    struct arg_type_info *info, int own,
254                    struct value *valuep)
255 {
256         /* Arrays decay into pointers per C standard.  We check for
257          * this here, because here we also capture arrays that come
258          * from parameter packs.  */
259         if (info->type == ARGTYPE_ARRAY) {
260                 struct arg_type_info *tmp = malloc(sizeof(*tmp));
261                 if (tmp != NULL) {
262                         type_init_pointer(tmp, info, own);
263                         tmp->lens = info->lens;
264                         info = tmp;
265                         own = 1;
266                 }
267         }
268
269         struct value value;
270         value_init(&value, proc, NULL, info, own);
271         if (fetch_arg_next(context, type, proc, info, &value) < 0)
272                 return -1;
273
274         if (val_dict_push_next(arguments, &value) < 0) {
275                 value_destroy(&value);
276                 return -1;
277         }
278
279         if (valuep != NULL)
280                 *valuep = value;
281
282         return 0;
283 }
284
285 static void
286 fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
287 {
288         if (*params_leftp == -1)
289                 *params_leftp = val_dict_count(arguments);
290 }
291
292 static int
293 fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context,
294                  struct value_dict *arguments, struct param *param,
295                  ssize_t *params_leftp)
296 {
297         struct param_enum *e = param_pack_init(param, arguments);
298         if (e == NULL)
299                 return -1;
300
301         int ret = 0;
302         while (1) {
303                 int insert_stop = 0;
304                 struct arg_type_info *info = malloc(sizeof(*info));
305                 if (info == NULL
306                     || param_pack_next(param, e, info, &insert_stop) < 0) {
307                 fail:
308                         free(info);
309                         ret = -1;
310                         break;
311                 }
312
313                 if (insert_stop)
314                         fetch_param_stop(arguments, params_leftp);
315
316                 if (info->type == ARGTYPE_VOID) {
317                         type_destroy(info);
318                         free(info);
319                         break;
320                 }
321
322                 struct value val;
323                 if (fetch_simple_param(type, proc, context, arguments,
324                                        info, 1, &val) < 0)
325                         goto fail;
326
327                 int stop = 0;
328                 switch (param_pack_stop(param, e, &val)) {
329                 case PPCB_ERR:
330                         goto fail;
331                 case PPCB_STOP:
332                         stop = 1;
333                 case PPCB_CONT:
334                         break;
335                 }
336
337                 if (stop)
338                         break;
339         }
340
341         param_pack_done(param, e);
342         return ret;
343 }
344
345 static int
346 fetch_one_param(enum tof type, Process *proc, struct fetch_context *context,
347                 struct value_dict *arguments, struct param *param,
348                 ssize_t *params_leftp)
349 {
350         switch (param->flavor) {
351                 int rc;
352         case PARAM_FLAVOR_TYPE:
353                 return fetch_simple_param(type, proc, context, arguments,
354                                           param->u.type.type, 0, NULL);
355
356         case PARAM_FLAVOR_PACK:
357                 if (fetch_param_pack_start(context,
358                                            param->u.pack.ppflavor) < 0)
359                         return -1;
360                 rc = fetch_param_pack(type, proc, context, arguments,
361                                       param, params_leftp);
362                 fetch_param_pack_end(context);
363                 return rc;
364
365         case PARAM_FLAVOR_STOP:
366                 fetch_param_stop(arguments, params_leftp);
367                 return 0;
368         }
369
370         assert(!"Invalid param flavor!");
371         abort();
372 }
373
374 static int
375 fetch_params(enum tof type, Process *proc, struct fetch_context *context,
376              struct value_dict *arguments, Function *func, ssize_t *params_leftp)
377 {
378         size_t i;
379         for (i = 0; i < func->num_params; ++i)
380                 if (fetch_one_param(type, proc, context, arguments,
381                                     &func->params[i], params_leftp) < 0)
382                         return -1;
383
384         /* Implicit stop at the end of parameter list.  */
385         fetch_param_stop(arguments, params_leftp);
386
387         return 0;
388 }
389
390 struct format_argument_data
391 {
392         struct value *value;
393         struct value_dict *arguments;
394 };
395
396 static int
397 format_argument_cb(FILE *stream, void *ptr)
398 {
399         struct format_argument_data *data = ptr;
400         int o = format_argument(stream, data->value, data->arguments);
401         if (o < 0)
402                 o = output_error(stream);
403         return o;
404 }
405
406 static int
407 output_params(struct value_dict *arguments, size_t start, size_t end,
408               int *need_delimp)
409 {
410         size_t i;
411         for (i = start; i < end; ++i) {
412                 struct value *value = val_dict_get_num(arguments, i);
413                 if (value == NULL)
414                         return -1;
415
416                 struct format_argument_data data = { value, arguments };
417                 int o = delim_output(options.output, need_delimp,
418                                      format_argument_cb, &data);
419                 if (o < 0)
420                         return -1;
421                 current_column += o;
422         }
423         return 0;
424 }
425
426 void
427 output_left(enum tof type, struct Process *proc,
428             struct library_symbol *libsym)
429 {
430         const char *function_name = libsym->name;
431         Function *func;
432
433         if (options.summary) {
434                 return;
435         }
436         if (current_proc) {
437                 fprintf(options.output, " <unfinished ...>\n");
438                 current_column = 0;
439         }
440         current_proc = proc;
441         current_depth = proc->callstack_depth;
442         begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
443         if (!options.hide_caller && libsym->lib != NULL
444             && libsym->plt_type != LS_TOPLT_NONE)
445                 /* We don't terribly mind failing this.  */
446                 account_output(&current_column,
447                                fprintf(options.output, "%s->",
448                                        libsym->lib->soname));
449
450         const char *name = function_name;
451 #ifdef USE_DEMANGLE
452         if (options.demangle)
453                 name = my_demangle(function_name);
454 #endif
455         if (account_output(&current_column,
456                            fprintf(options.output, "%s", name)) < 0)
457                 return;
458
459         if (libsym->lib != NULL
460             && libsym->lib->type != LT_LIBTYPE_MAIN
461             && libsym->plt_type == LS_TOPLT_NONE
462             && account_output(&current_column,
463                               fprintf(options.output, "@%s",
464                                       libsym->lib->soname)) < 0)
465                 /* We do mind failing this though.  */
466                 return;
467
468         account_output(&current_column, fprintf(options.output, "("));
469
470         func = name2func(function_name);
471         if (func == NULL) {
472                 account_output(&current_column, fprintf(options.output, "???"));
473                 return;
474         }
475
476         struct fetch_context *context = fetch_arg_init(type, proc,
477                                                        func->return_info);
478         struct value_dict *arguments = malloc(sizeof(*arguments));
479         if (arguments == NULL)
480                 return;
481         val_dict_init(arguments);
482
483         ssize_t params_left = -1;
484         int need_delim = 0;
485         if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
486             || output_params(arguments, 0, params_left, &need_delim) < 0) {
487                 val_dict_destroy(arguments);
488                 fetch_arg_done(context);
489                 arguments = NULL;
490                 context = NULL;
491         }
492
493         struct callstack_element *stel
494                 = &proc->callstack[proc->callstack_depth - 1];
495         stel->fetch_context = context;
496         stel->arguments = arguments;
497         stel->out.params_left = params_left;
498         stel->out.need_delim = need_delim;
499 }
500
501 void
502 output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
503 {
504         const char *function_name = libsym->name;
505         Function *func = name2func(function_name);
506         if (func == NULL)
507                 return;
508
509         if (options.summary) {
510                 struct opt_c_struct *st;
511                 if (!dict_opt_c) {
512                         dict_opt_c =
513                             dict_init(dict_key2hash_string,
514                                       dict_key_cmp_string);
515                 }
516                 st = dict_find_entry(dict_opt_c, function_name);
517                 if (!st) {
518                         char *na;
519                         st = malloc(sizeof(struct opt_c_struct));
520                         na = strdup(function_name);
521                         if (!st || !na) {
522                                 perror("malloc()");
523                                 exit(1);
524                         }
525                         st->count = 0;
526                         st->tv.tv_sec = st->tv.tv_usec = 0;
527                         dict_enter(dict_opt_c, na, st);
528                 }
529                 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
530                         st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
531                         st->tv.tv_sec++;
532                 } else {
533                         st->tv.tv_usec += current_time_spent.tv_usec;
534                 }
535                 st->count++;
536                 st->tv.tv_sec += current_time_spent.tv_sec;
537
538 //              fprintf(options.output, "%s <%lu.%06d>\n", function_name,
539 //                              current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
540                 return;
541         }
542         if (current_proc && (current_proc != proc ||
543                             current_depth != proc->callstack_depth)) {
544                 fprintf(options.output, " <unfinished ...>\n");
545                 current_proc = 0;
546         }
547         if (current_proc != proc) {
548                 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
549 #ifdef USE_DEMANGLE
550                 current_column +=
551                     fprintf(options.output, "<... %s resumed> ",
552                             options.demangle ? my_demangle(function_name) : function_name);
553 #else
554                 current_column +=
555                     fprintf(options.output, "<... %s resumed> ", function_name);
556 #endif
557         }
558
559         struct callstack_element *stel
560                 = &proc->callstack[proc->callstack_depth - 1];
561
562         struct fetch_context *context = stel->fetch_context;
563
564         /* Fetch & enter into dictionary the retval first, so that
565          * other values can use it in expressions.  */
566         struct value retval;
567         int own_retval = 0;
568         if (context != NULL) {
569                 value_init(&retval, proc, NULL, func->return_info, 0);
570                 own_retval = 1;
571                 if (fetch_retval(context, type, proc, func->return_info,
572                                  &retval) < 0)
573                         value_set_type(&retval, NULL, 0);
574                 else if (stel->arguments != NULL
575                            && val_dict_push_named(stel->arguments, &retval,
576                                                   "retval", 0) == 0)
577                         own_retval = 0;
578         }
579
580         if (stel->arguments != NULL)
581                 output_params(stel->arguments, stel->out.params_left,
582                               val_dict_count(stel->arguments),
583                               &stel->out.need_delim);
584
585         current_column += fprintf(options.output, ") ");
586         tabto(options.align - 1);
587         fprintf(options.output, "= ");
588
589         if (context != NULL && retval.type != NULL) {
590                 struct format_argument_data data = { &retval, stel->arguments };
591                 format_argument_cb(options.output, &data);
592         }
593
594         if (own_retval)
595                 value_destroy(&retval);
596
597         if (opt_T) {
598                 fprintf(options.output, " <%lu.%06d>",
599                         current_time_spent.tv_sec,
600                         (int)current_time_spent.tv_usec);
601         }
602         fprintf(options.output, "\n");
603
604 #if defined(HAVE_LIBUNWIND)
605         if (options.bt_depth > 0) {
606                 unw_cursor_t cursor;
607                 unw_word_t ip, sp;
608                 int unwind_depth = options.bt_depth;
609                 char fn_name[100];
610
611                 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
612                 while (unwind_depth) {
613                         unw_get_reg(&cursor, UNW_REG_IP, &ip);
614                         unw_get_reg(&cursor, UNW_REG_SP, &sp);
615                         unw_get_proc_name(&cursor, fn_name, 100, NULL);
616                         fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
617                         if (unw_step(&cursor) <= 0)
618                                 break;
619                         unwind_depth--;
620                 }
621                 fprintf(options.output, "\n");
622         }
623 #endif /* defined(HAVE_LIBUNWIND) */
624
625         current_proc = 0;
626         current_column = 0;
627 }
628
629 int
630 delim_output(FILE *stream, int *need_delimp,
631              int (*writer)(FILE *stream, void *data),
632              void *data)
633 {
634         int o;
635
636         /* If we don't need a delimiter, then we don't need to go
637          * through a temporary stream.  It's all the same whether
638          * WRITER emits anything or not.  */
639         if (!*need_delimp) {
640                 o = writer(stream, data);
641
642         } else {
643                 char *buf;
644                 size_t bufsz;
645                 FILE *tmp = open_memstream(&buf, &bufsz);
646                 o = writer(tmp, data);
647                 fclose(tmp);
648
649                 if (o > 0 && ((*need_delimp
650                                && account_output(&o, fprintf(stream, ", ")) < 0)
651                               || fwrite(buf, 1, bufsz, stream) != bufsz))
652                         o = -1;
653
654                 free(buf);
655         }
656
657         if (o < 0)
658                 return -1;
659
660         *need_delimp = *need_delimp || o > 0;
661         return o;
662 }
663
664 int
665 account_output(int *countp, int c)
666 {
667         if (c > 0)
668                 *countp += c;
669         return c;
670 }
671
672 static void
673 do_report(const char *filename, unsigned line_no, const char *severity,
674           const char *fmt, va_list args)
675 {
676         char buf[128];
677         vsnprintf(buf, sizeof(buf), fmt, args);
678         buf[sizeof(buf) - 1] = 0;
679         if (filename != NULL)
680                 output_line(0, "%s:%d: %s: %s",
681                             filename, line_no, severity, buf);
682         else
683                 output_line(0, "%s: %s", severity, buf);
684 }
685
686 void
687 report_error(const char *filename, unsigned line_no, const char *fmt, ...)
688 {
689         va_list args;
690         va_start(args, fmt);
691         do_report(filename, line_no, "error", fmt, args);
692         va_end(args);
693 }
694
695 void
696 report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
697 {
698         va_list args;
699         va_start(args, fmt);
700         do_report(filename, line_no, "warning", fmt, args);
701         va_end(args);
702 }
703
704 void
705 report_global_error(const char *fmt, ...)
706 {
707         va_list args;
708         va_start(args, fmt);
709         do_report(NULL, 0, "error", fmt, args);
710         va_end(args);
711 }