blob: 59e02b7b4ccac61b20e51d7f476f9951d02c9bdd [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
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
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
Juan Cespedesac3db291998-04-25 14:31:58 +020025#include "config.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020026
Juan Cespedes3268a161997-08-25 16:45:22 +020027#include <stdio.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +010028#include <stdlib.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020029#include <stdarg.h>
Juan Cespedes1cd999a2001-07-03 00:46:04 +020030#include <string.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020031#include <time.h>
32#include <sys/time.h>
33#include <unistd.h>
Petr Machataf6ec08a2012-01-06 16:58:54 +010034#include <errno.h>
Petr Machata865303f2012-01-06 18:40:38 +010035#include <assert.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020036
Juan Cespedesf7281232009-06-25 16:11:21 +020037#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010038#include "proc.h"
Petr Machata29add4f2012-02-18 16:38:05 +010039#include "library.h"
Petr Machata000e3112012-01-03 17:03:39 +010040#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010041#include "value.h"
42#include "value_dict.h"
Petr Machata865303f2012-01-06 18:40:38 +010043#include "param.h"
Petr Machataf6ec08a2012-01-06 16:58:54 +010044#include "fetch.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020045
Juan Cespedesf7281232009-06-25 16:11:21 +020046/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010047extern struct timeval current_time_spent;
48
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020049Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010050
Juan Cespedesa8909f72009-04-28 20:02:41 +020051static Process *current_proc = 0;
Juan Cespedes5916fda2002-02-25 00:19:21 +010052static int current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010053static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020054
Juan Cespedesf1350522008-12-16 18:19:58 +010055static void
Petr Machata54004752012-05-03 18:36:48 +020056output_indent(struct Process *proc)
57{
58 int d = options.indent * (proc->callstack_depth - 1);
59 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020060}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020061
Juan Cespedesf1350522008-12-16 18:19:58 +010062static void
Petr Machata37b73c02012-01-06 16:00:25 +010063begin_of_line(Process *proc, int is_func, int indent)
64{
Juan Cespedes5e01f651998-03-08 22:31:44 +010065 current_column = 0;
66 if (!proc) {
67 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020068 }
Juan Cespedesc693f022009-05-21 18:59:41 +020069 if ((options.output != stderr) && (opt_p || options.follow)) {
70 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020071 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010072 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010073 }
Juan Cespedesf666d191998-09-20 23:04:34 +020074 if (opt_r) {
75 struct timeval tv;
76 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020078 struct timeval diff;
79
80 gettimeofday(&tv, &tz);
81
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
83 old_tv.tv_sec = tv.tv_sec;
84 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020085 }
86 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
87 if (tv.tv_usec >= old_tv.tv_usec) {
88 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
89 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020090 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020091 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
92 }
93 old_tv.tv_sec = tv.tv_sec;
94 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010095 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010096 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020097 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020098 if (opt_t) {
99 struct timeval tv;
100 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200101
102 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100104 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 tv.tv_sec, (int)tv.tv_usec);
106 } else if (opt_t > 1) {
107 struct tm *tmp = localtime(&tv.tv_sec);
108 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100109 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
111 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200112 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100114 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115 tmp->tm_hour, tmp->tm_min,
116 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200117 }
118 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100119 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100120 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100121 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100122 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100123 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100124 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100125 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100126 }
Petr Machata37b73c02012-01-06 16:00:25 +0100127 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200128 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200129 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200130}
131
Petr Machataf6ec08a2012-01-06 16:58:54 +0100132/* The default prototype is: long X(long, long, long, long). */
133static Function *
134build_default_prototype(void)
135{
136 Function *ret = malloc(sizeof(*ret));
137 size_t i = 0;
138 if (ret == NULL)
139 goto err;
140 memset(ret, 0, sizeof(*ret));
141
142 struct arg_type_info *unknown_type = type_get_simple(ARGTYPE_UNKNOWN);
143
144 ret->return_info = unknown_type;
145
146 ret->num_params = 4;
Petr Machata865303f2012-01-06 18:40:38 +0100147 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
148 if (ret->params == NULL)
149 goto err;
150
151 for (i = 0; i < ret->num_params; ++i)
152 param_init_type(&ret->params[i], unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100153
154 return ret;
155
156err:
157 report_global_error("malloc: %s", strerror(errno));
Petr Machata865303f2012-01-06 18:40:38 +0100158 if (ret->params != NULL) {
159 while (i-- > 0)
160 param_destroy(&ret->params[i]);
161 free(ret->params);
162 }
163
Petr Machataf6ec08a2012-01-06 16:58:54 +0100164 free(ret);
165
166 return NULL;
167}
168
Juan Cespedescde58262009-05-07 11:09:00 +0200169static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +0200170name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200171 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100172 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100173
Petr Machataf6ec08a2012-01-06 16:58:54 +0100174 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200175 str1 = tmp->name;
176 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100177 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100178 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100179 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100180
181 static Function *def = NULL;
182 if (def == NULL)
183 def = build_default_prototype();
184
185 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100186}
187
Juan Cespedesf1350522008-12-16 18:19:58 +0100188void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200189output_line(Process *proc, char *fmt, ...) {
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200190 va_list args;
191
Juan Cespedesda9b9532009-04-07 15:33:50 +0200192 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100193 return;
194 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200195 if (current_proc) {
196 if (current_proc->callstack[current_depth].return_addr) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100197 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200198 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100199 fprintf(options.output, " <no return ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200200 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100201 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200202 current_proc = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200203 if (!fmt) {
204 return;
205 }
Petr Machata37b73c02012-01-06 16:00:25 +0100206 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100207
Juan Cespedes21c63a12001-07-07 20:56:56 +0200208 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100209 vfprintf(options.output, fmt, args);
210 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200211 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100212 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100213}
214
Juan Cespedesf1350522008-12-16 18:19:58 +0100215static void
216tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100217 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100218 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100219 }
220}
221
Petr Machataf6ec08a2012-01-06 16:58:54 +0100222static int
223account_output(int o)
224{
225 if (o < 0)
226 return -1;
227 current_column += o;
228 return 0;
229}
230
231static int
232output_error(void)
233{
234 return account_output(fprintf(options.output, "?"));
235}
236
237static int
238fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context,
239 struct value_dict *arguments, struct arg_type_info *info,
240 struct value *valuep)
241{
242 /* Arrays decay into pointers per C standard. We check for
243 * this here, because here we also capture arrays that come
244 * from parameter packs. */
245 int own = 0;
246 if (info->type == ARGTYPE_ARRAY) {
247 struct arg_type_info *tmp = malloc(sizeof(*tmp));
248 if (tmp != NULL) {
249 type_init_pointer(tmp, info, 0);
250 info = tmp;
251 own = 1;
252 }
253 }
254
255 struct value value;
256 value_init(&value, proc, NULL, info, own);
257 if (fetch_arg_next(context, type, proc, info, &value) < 0)
258 return -1;
259
260 if (val_dict_push_next(arguments, &value) < 0) {
261 value_destroy(&value);
262 return -1;
263 }
264
265 if (valuep != NULL)
266 *valuep = value;
267
268 return 0;
269}
270
271static void
272fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
273{
274 if (*params_leftp == -1)
275 *params_leftp = val_dict_count(arguments);
276}
277
278static int
Petr Machata865303f2012-01-06 18:40:38 +0100279fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context,
280 struct value_dict *arguments, struct param *param,
281 ssize_t *params_leftp)
282{
283 struct param_enum *e = param_pack_init(param, arguments);
284 if (e == NULL)
285 return -1;
286
287 int ret = 0;
288 while (1) {
289 int insert_stop = 0;
290 struct arg_type_info *info = malloc(sizeof(*info));
291 if (info == NULL
292 || param_pack_next(param, e, info, &insert_stop) < 0) {
293 fail:
294 free(info);
295 ret = -1;
296 break;
297 }
298
299 if (insert_stop)
300 fetch_param_stop(arguments, params_leftp);
301
302 if (info->type == ARGTYPE_VOID)
303 break;
304
305 struct value val;
306 if (fetch_simple_param(type, proc, context, arguments,
307 info, &val) < 0)
308 goto fail;
309
310 int stop = 0;
311 switch (param_pack_stop(param, e, &val)) {
312 case PPCB_ERR:
313 goto fail;
314 case PPCB_STOP:
315 stop = 1;
316 case PPCB_CONT:
317 break;
318 }
319
320 if (stop)
321 break;
322 }
323
324 param_pack_done(param, e);
325 return ret;
326}
327
328static int
Petr Machataf6ec08a2012-01-06 16:58:54 +0100329fetch_one_param(enum tof type, Process *proc, struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100330 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100331 ssize_t *params_leftp)
332{
Petr Machata865303f2012-01-06 18:40:38 +0100333 switch (param->flavor) {
334 case PARAM_FLAVOR_TYPE:
335 return fetch_simple_param(type, proc, context, arguments,
336 param->u.type.type, NULL);
337
338 case PARAM_FLAVOR_PACK:
339 return fetch_param_pack(type, proc, context, arguments,
340 param, params_leftp);
341
342 case PARAM_FLAVOR_STOP:
343 fetch_param_stop(arguments, params_leftp);
344 return 0;
345 }
346
347 assert(!"Invalid param flavor!");
348 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100349}
350
351static int
352fetch_params(enum tof type, Process *proc, struct fetch_context *context,
353 struct value_dict *arguments, Function *func, ssize_t *params_leftp)
354{
355 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100356 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100357 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100358 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100359 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100360
361 /* Implicit stop at the end of parameter list. */
362 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100363
Petr Machataf6ec08a2012-01-06 16:58:54 +0100364 return 0;
365}
366
367static int
368output_one(struct value *val, struct value_dict *arguments)
369{
370 int o = format_argument(options.output, val, arguments);
371 if (account_output(o) < 0) {
372 if (output_error() < 0)
373 return -1;
374 o = 1;
375 }
376 return o;
377}
378
379static int
380output_params(struct value_dict *arguments, size_t start, size_t end,
381 int *need_delimp)
382{
383 size_t i;
384 int need_delim = *need_delimp;
385 for (i = start; i < end; ++i) {
386 if (need_delim
387 && account_output(fprintf(options.output, ", ")) < 0)
388 return -1;
389 struct value *value = val_dict_get_num(arguments, i);
390 if (value == NULL)
391 return -1;
392 need_delim = output_one(value, arguments);
393 if (need_delim < 0)
394 return -1;
395 }
396 *need_delimp = need_delim;
397 return 0;
398}
399
Juan Cespedesf1350522008-12-16 18:19:58 +0100400void
Petr Machata29add4f2012-02-18 16:38:05 +0100401output_left(enum tof type, struct Process *proc,
402 struct library_symbol *libsym)
403{
404 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200405 Function *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100406
Juan Cespedesda9b9532009-04-07 15:33:50 +0200407 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100408 return;
409 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200410 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100411 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100412 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100413 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200414 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100415 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100416 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200417 if (!options.hide_caller && libsym->lib != NULL
418 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machatab89c2562012-04-03 17:00:28 +0200419 current_column += fprintf(options.output, "%s->",
420 libsym->lib->soname);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100421
422 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100423#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100424 if (options.demangle)
425 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200426#endif
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 if (account_output(fprintf(options.output, "%s(", name)) < 0)
428 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100429
430 func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100431 if (func == NULL)
432 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100433
Petr Machataf6ec08a2012-01-06 16:58:54 +0100434 struct fetch_context *context = fetch_arg_init(type, proc,
435 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100436 struct value_dict *arguments = malloc(sizeof(*arguments));
437 if (arguments == NULL)
438 return;
439 val_dict_init(arguments);
440
Petr Machataf6ec08a2012-01-06 16:58:54 +0100441 ssize_t params_left = -1;
442 int need_delim = 0;
443 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
444 || output_params(arguments, 0, params_left, &need_delim) < 0) {
445 val_dict_destroy(arguments);
446 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100447 arguments = NULL;
448 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100449 }
Petr Machata94078ec2012-01-05 18:07:02 +0100450
451 struct callstack_element *stel
452 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100453 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100454 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100455 stel->out.params_left = params_left;
456 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100457}
458
Juan Cespedesf1350522008-12-16 18:19:58 +0100459void
Petr Machata29add4f2012-02-18 16:38:05 +0100460output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100461{
Petr Machata29add4f2012-02-18 16:38:05 +0100462 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200463 Function *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100464 if (func == NULL)
465 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100466
Juan Cespedesda9b9532009-04-07 15:33:50 +0200467 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100468 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100469 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100470 dict_opt_c =
471 dict_init(dict_key2hash_string,
472 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100473 }
474 st = dict_find_entry(dict_opt_c, function_name);
475 if (!st) {
476 char *na;
477 st = malloc(sizeof(struct opt_c_struct));
478 na = strdup(function_name);
479 if (!st || !na) {
480 perror("malloc()");
481 exit(1);
482 }
483 st->count = 0;
484 st->tv.tv_sec = st->tv.tv_usec = 0;
485 dict_enter(dict_opt_c, na, st);
486 }
487 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
488 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
489 st->tv.tv_sec++;
490 } else {
491 st->tv.tv_usec += current_time_spent.tv_usec;
492 }
493 st->count++;
494 st->tv.tv_sec += current_time_spent.tv_sec;
495
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100496// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100497// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100498 return;
499 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200500 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100501 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100502 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200503 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200504 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200505 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100506 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100507#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100508 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100509 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100510 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200511#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100513 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200514#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100515 }
516
Petr Machata94078ec2012-01-05 18:07:02 +0100517 struct callstack_element *stel
518 = &proc->callstack[proc->callstack_depth - 1];
519
Petr Machataf6ec08a2012-01-06 16:58:54 +0100520 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100521
Petr Machataf6ec08a2012-01-06 16:58:54 +0100522 /* Fetch & enter into dictionary the retval first, so that
523 * other values can use it in expressions. */
524 struct value retval;
525 int own_retval = 0;
526 if (context != NULL) {
527 value_init(&retval, proc, NULL, func->return_info, 0);
528 own_retval = 1;
529 if (fetch_retval(context, type, proc, func->return_info,
530 &retval) == 0) {
531 if (stel->arguments != NULL
532 && val_dict_push_named(stel->arguments, &retval,
533 "retval", 0) == 0)
534 own_retval = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100535 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200536 }
Petr Machata94078ec2012-01-05 18:07:02 +0100537
Petr Machataf6ec08a2012-01-06 16:58:54 +0100538 if (stel->arguments != NULL)
539 output_params(stel->arguments, stel->out.params_left,
540 val_dict_count(stel->arguments),
541 &stel->out.need_delim);
542
543 current_column += fprintf(options.output, ") ");
544 tabto(options.align - 1);
545 fprintf(options.output, "= ");
546
547 output_one(&retval, stel->arguments);
548
549 if (own_retval)
550 value_destroy(&retval);
551
Petr Machata865303f2012-01-06 18:40:38 +0100552 if (stel->arguments != NULL) {
553 val_dict_destroy(stel->arguments);
554 free(stel->arguments);
555 }
556 if (context != NULL)
557 fetch_arg_done(context);
Petr Machata94078ec2012-01-05 18:07:02 +0100558
Juan Cespedesd65efa32003-02-03 00:22:30 +0100559 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100560 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100561 current_time_spent.tv_sec,
562 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100563 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100564 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700565
566#if defined(HAVE_LIBUNWIND)
567 if (options.bt_depth > 0) {
568 unw_cursor_t cursor;
569 unw_word_t ip, sp;
570 int unwind_depth = options.bt_depth;
571 char fn_name[100];
572
573 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
574 while (unwind_depth) {
575 unw_get_reg(&cursor, UNW_REG_IP, &ip);
576 unw_get_reg(&cursor, UNW_REG_SP, &sp);
577 unw_get_proc_name(&cursor, fn_name, 100, NULL);
578 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
579 if (unw_step(&cursor) <= 0)
580 break;
581 unwind_depth--;
582 }
583 fprintf(options.output, "\n");
584 }
585#endif /* defined(HAVE_LIBUNWIND) */
586
Paul Gilliam76c61f12006-06-14 06:55:21 +0200587 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100588 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100589}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100590
591static void
592do_report(const char *filename, unsigned line_no, const char *severity,
593 const char *fmt, va_list args)
594{
595 char buf[128];
596 vsnprintf(buf, sizeof(buf), fmt, args);
597 buf[sizeof(buf) - 1] = 0;
598 if (filename != NULL)
599 output_line(0, "%s:%d: %s: %s",
600 filename, line_no, severity, buf);
601 else
602 output_line(0, "%s: %s", severity, buf);
603}
604
605void
606report_error(const char *filename, unsigned line_no, char *fmt, ...)
607{
608 va_list args;
609 va_start(args, fmt);
610 do_report(filename, line_no, "error", fmt, args);
611 va_end(args);
612}
613
614void
615report_warning(const char *filename, unsigned line_no, char *fmt, ...)
616{
617 va_list args;
618 va_start(args, fmt);
619 do_report(filename, line_no, "warning", fmt, args);
620 va_end(args);
621}
622
623void
624report_global_error(char *fmt, ...)
625{
626 va_list args;
627 va_start(args, fmt);
628 do_report(NULL, 0, "error", fmt, args);
629 va_end(args);
630}