blob: ee444d57eea756dfab9164d38855492e740a0053 [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>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020035
Juan Cespedesf7281232009-06-25 16:11:21 +020036#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010037#include "proc.h"
Petr Machata29add4f2012-02-18 16:38:05 +010038#include "library.h"
Petr Machata000e3112012-01-03 17:03:39 +010039#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010040#include "value.h"
41#include "value_dict.h"
Petr Machataf6ec08a2012-01-06 16:58:54 +010042#include "fetch.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020043
Juan Cespedesf7281232009-06-25 16:11:21 +020044/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010045extern struct timeval current_time_spent;
46
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020047Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010048
Juan Cespedesa8909f72009-04-28 20:02:41 +020049static Process *current_proc = 0;
Juan Cespedes5916fda2002-02-25 00:19:21 +010050static int current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010051static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020052
Juan Cespedesf1350522008-12-16 18:19:58 +010053static void
Petr Machata54004752012-05-03 18:36:48 +020054output_indent(struct Process *proc)
55{
56 int d = options.indent * (proc->callstack_depth - 1);
57 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020058}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020059
Juan Cespedesf1350522008-12-16 18:19:58 +010060static void
Petr Machata37b73c02012-01-06 16:00:25 +010061begin_of_line(Process *proc, int is_func, int indent)
62{
Juan Cespedes5e01f651998-03-08 22:31:44 +010063 current_column = 0;
64 if (!proc) {
65 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020066 }
Juan Cespedesc693f022009-05-21 18:59:41 +020067 if ((options.output != stderr) && (opt_p || options.follow)) {
68 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020069 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010070 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010071 }
Juan Cespedesf666d191998-09-20 23:04:34 +020072 if (opt_r) {
73 struct timeval tv;
74 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020076 struct timeval diff;
77
78 gettimeofday(&tv, &tz);
79
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
81 old_tv.tv_sec = tv.tv_sec;
82 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020083 }
84 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
85 if (tv.tv_usec >= old_tv.tv_usec) {
86 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
87 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020088 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020089 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
90 }
91 old_tv.tv_sec = tv.tv_sec;
92 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010093 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020095 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020096 if (opt_t) {
97 struct timeval tv;
98 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020099
100 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100102 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 tv.tv_sec, (int)tv.tv_usec);
104 } else if (opt_t > 1) {
105 struct tm *tmp = localtime(&tv.tv_sec);
106 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100107 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
109 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200110 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100112 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 tmp->tm_hour, tmp->tm_min,
114 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200115 }
116 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100117 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100118 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100119 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100121 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100122 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100123 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100124 }
Petr Machata37b73c02012-01-06 16:00:25 +0100125 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200126 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200127 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200128}
129
Petr Machataf6ec08a2012-01-06 16:58:54 +0100130/* The default prototype is: long X(long, long, long, long). */
131static Function *
132build_default_prototype(void)
133{
134 Function *ret = malloc(sizeof(*ret));
135 size_t i = 0;
136 if (ret == NULL)
137 goto err;
138 memset(ret, 0, sizeof(*ret));
139
140 struct arg_type_info *unknown_type = type_get_simple(ARGTYPE_UNKNOWN);
141
142 ret->return_info = unknown_type;
143
144 ret->num_params = 4;
145 for (i = 0; i < (size_t)ret->num_params; ++i)
146 ret->arg_info[i] = unknown_type;
147
148 return ret;
149
150err:
151 report_global_error("malloc: %s", strerror(errno));
152 free(ret);
153
154 return NULL;
155}
156
Juan Cespedescde58262009-05-07 11:09:00 +0200157static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +0200158name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200159 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100161
Petr Machataf6ec08a2012-01-06 16:58:54 +0100162 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200163 str1 = tmp->name;
164 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100165 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100166 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100167 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100168
169 static Function *def = NULL;
170 if (def == NULL)
171 def = build_default_prototype();
172
173 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100174}
175
Juan Cespedesf1350522008-12-16 18:19:58 +0100176void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200177output_line(Process *proc, char *fmt, ...) {
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200178 va_list args;
179
Juan Cespedesda9b9532009-04-07 15:33:50 +0200180 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100181 return;
182 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200183 if (current_proc) {
184 if (current_proc->callstack[current_depth].return_addr) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100185 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200186 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100187 fprintf(options.output, " <no return ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200188 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100189 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200190 current_proc = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200191 if (!fmt) {
192 return;
193 }
Petr Machata37b73c02012-01-06 16:00:25 +0100194 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195
Juan Cespedes21c63a12001-07-07 20:56:56 +0200196 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100197 vfprintf(options.output, fmt, args);
198 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200199 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100201}
202
Juan Cespedesf1350522008-12-16 18:19:58 +0100203static void
204tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100205 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100206 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100207 }
208}
209
Petr Machataf6ec08a2012-01-06 16:58:54 +0100210static int
211account_output(int o)
212{
213 if (o < 0)
214 return -1;
215 current_column += o;
216 return 0;
217}
218
219static int
220output_error(void)
221{
222 return account_output(fprintf(options.output, "?"));
223}
224
225static int
226fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context,
227 struct value_dict *arguments, struct arg_type_info *info,
228 struct value *valuep)
229{
230 /* Arrays decay into pointers per C standard. We check for
231 * this here, because here we also capture arrays that come
232 * from parameter packs. */
233 int own = 0;
234 if (info->type == ARGTYPE_ARRAY) {
235 struct arg_type_info *tmp = malloc(sizeof(*tmp));
236 if (tmp != NULL) {
237 type_init_pointer(tmp, info, 0);
238 info = tmp;
239 own = 1;
240 }
241 }
242
243 struct value value;
244 value_init(&value, proc, NULL, info, own);
245 if (fetch_arg_next(context, type, proc, info, &value) < 0)
246 return -1;
247
248 if (val_dict_push_next(arguments, &value) < 0) {
249 value_destroy(&value);
250 return -1;
251 }
252
253 if (valuep != NULL)
254 *valuep = value;
255
256 return 0;
257}
258
259static void
260fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
261{
262 if (*params_leftp == -1)
263 *params_leftp = val_dict_count(arguments);
264}
265
266static int
267fetch_one_param(enum tof type, Process *proc, struct fetch_context *context,
268 struct value_dict *arguments, struct arg_type_info *info,
269 ssize_t *params_leftp)
270{
271 return fetch_simple_param(type, proc, context, arguments,
272 info, NULL);
273}
274
275static int
276fetch_params(enum tof type, Process *proc, struct fetch_context *context,
277 struct value_dict *arguments, Function *func, ssize_t *params_leftp)
278{
279 size_t i;
280 for (i = 0; i < (size_t)func->num_params; ++i) {
281 if (i == (size_t)(func->num_params - func->params_right))
282 fetch_param_stop(arguments, params_leftp);
283 if (fetch_one_param(type, proc, context, arguments,
284 func->arg_info[i], params_leftp) < 0)
285 return -1;
286 }
287
288 /* Implicit stop at the end of parameter list. */
289 fetch_param_stop(arguments, params_leftp);
290 return 0;
291}
292
293static int
294output_one(struct value *val, struct value_dict *arguments)
295{
296 int o = format_argument(options.output, val, arguments);
297 if (account_output(o) < 0) {
298 if (output_error() < 0)
299 return -1;
300 o = 1;
301 }
302 return o;
303}
304
305static int
306output_params(struct value_dict *arguments, size_t start, size_t end,
307 int *need_delimp)
308{
309 size_t i;
310 int need_delim = *need_delimp;
311 for (i = start; i < end; ++i) {
312 if (need_delim
313 && account_output(fprintf(options.output, ", ")) < 0)
314 return -1;
315 struct value *value = val_dict_get_num(arguments, i);
316 if (value == NULL)
317 return -1;
318 need_delim = output_one(value, arguments);
319 if (need_delim < 0)
320 return -1;
321 }
322 *need_delimp = need_delim;
323 return 0;
324}
325
Juan Cespedesf1350522008-12-16 18:19:58 +0100326void
Petr Machata29add4f2012-02-18 16:38:05 +0100327output_left(enum tof type, struct Process *proc,
328 struct library_symbol *libsym)
329{
330 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200331 Function *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100332
Juan Cespedesda9b9532009-04-07 15:33:50 +0200333 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100334 return;
335 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200336 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100337 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100338 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100339 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200340 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100341 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100342 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200343 if (!options.hide_caller && libsym->lib != NULL
344 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machatab89c2562012-04-03 17:00:28 +0200345 current_column += fprintf(options.output, "%s->",
346 libsym->lib->soname);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100347
348 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100349#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100350 if (options.demangle)
351 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200352#endif
Petr Machataf6ec08a2012-01-06 16:58:54 +0100353 if (account_output(fprintf(options.output, "%s(", name)) < 0)
354 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100355
356 func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100357 if (func == NULL)
358 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100359
Petr Machataf6ec08a2012-01-06 16:58:54 +0100360 struct fetch_context *context = fetch_arg_init(type, proc,
361 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100362 struct value_dict *arguments = malloc(sizeof(*arguments));
363 if (arguments == NULL)
364 return;
365 val_dict_init(arguments);
366
Petr Machataf6ec08a2012-01-06 16:58:54 +0100367 ssize_t params_left = -1;
368 int need_delim = 0;
369 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
370 || output_params(arguments, 0, params_left, &need_delim) < 0) {
371 val_dict_destroy(arguments);
372 fetch_arg_done(context);
373 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100374 }
Petr Machata94078ec2012-01-05 18:07:02 +0100375
376 struct callstack_element *stel
377 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100378 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100379 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100380 stel->out.params_left = params_left;
381 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100382}
383
Juan Cespedesf1350522008-12-16 18:19:58 +0100384void
Petr Machata29add4f2012-02-18 16:38:05 +0100385output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100386{
Petr Machata29add4f2012-02-18 16:38:05 +0100387 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200388 Function *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100389 if (func == NULL)
390 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100391
Juan Cespedesda9b9532009-04-07 15:33:50 +0200392 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100393 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100394 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100395 dict_opt_c =
396 dict_init(dict_key2hash_string,
397 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100398 }
399 st = dict_find_entry(dict_opt_c, function_name);
400 if (!st) {
401 char *na;
402 st = malloc(sizeof(struct opt_c_struct));
403 na = strdup(function_name);
404 if (!st || !na) {
405 perror("malloc()");
406 exit(1);
407 }
408 st->count = 0;
409 st->tv.tv_sec = st->tv.tv_usec = 0;
410 dict_enter(dict_opt_c, na, st);
411 }
412 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
413 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
414 st->tv.tv_sec++;
415 } else {
416 st->tv.tv_usec += current_time_spent.tv_usec;
417 }
418 st->count++;
419 st->tv.tv_sec += current_time_spent.tv_sec;
420
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100421// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100422// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100423 return;
424 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200425 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100426 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100427 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200428 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200429 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200430 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100431 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100432#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100433 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100434 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100435 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200436#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100437 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100438 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200439#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100440 }
441
Petr Machata94078ec2012-01-05 18:07:02 +0100442 struct callstack_element *stel
443 = &proc->callstack[proc->callstack_depth - 1];
444
Petr Machataf6ec08a2012-01-06 16:58:54 +0100445 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100446
Petr Machataf6ec08a2012-01-06 16:58:54 +0100447 /* Fetch & enter into dictionary the retval first, so that
448 * other values can use it in expressions. */
449 struct value retval;
450 int own_retval = 0;
451 if (context != NULL) {
452 value_init(&retval, proc, NULL, func->return_info, 0);
453 own_retval = 1;
454 if (fetch_retval(context, type, proc, func->return_info,
455 &retval) == 0) {
456 if (stel->arguments != NULL
457 && val_dict_push_named(stel->arguments, &retval,
458 "retval", 0) == 0)
459 own_retval = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100460 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200461 }
Petr Machata94078ec2012-01-05 18:07:02 +0100462
Petr Machataf6ec08a2012-01-06 16:58:54 +0100463 if (stel->arguments != NULL)
464 output_params(stel->arguments, stel->out.params_left,
465 val_dict_count(stel->arguments),
466 &stel->out.need_delim);
467
468 current_column += fprintf(options.output, ") ");
469 tabto(options.align - 1);
470 fprintf(options.output, "= ");
471
472 output_one(&retval, stel->arguments);
473
474 if (own_retval)
475 value_destroy(&retval);
476
Petr Machata94078ec2012-01-05 18:07:02 +0100477 val_dict_destroy(stel->arguments);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100478 free(stel->arguments);
479 fetch_arg_done(context);
Petr Machata94078ec2012-01-05 18:07:02 +0100480
Juan Cespedesd65efa32003-02-03 00:22:30 +0100481 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100482 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100483 current_time_spent.tv_sec,
484 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100485 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100486 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700487
488#if defined(HAVE_LIBUNWIND)
489 if (options.bt_depth > 0) {
490 unw_cursor_t cursor;
491 unw_word_t ip, sp;
492 int unwind_depth = options.bt_depth;
493 char fn_name[100];
494
495 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
496 while (unwind_depth) {
497 unw_get_reg(&cursor, UNW_REG_IP, &ip);
498 unw_get_reg(&cursor, UNW_REG_SP, &sp);
499 unw_get_proc_name(&cursor, fn_name, 100, NULL);
500 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
501 if (unw_step(&cursor) <= 0)
502 break;
503 unwind_depth--;
504 }
505 fprintf(options.output, "\n");
506 }
507#endif /* defined(HAVE_LIBUNWIND) */
508
Paul Gilliam76c61f12006-06-14 06:55:21 +0200509 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100510 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100511}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100512
513static void
514do_report(const char *filename, unsigned line_no, const char *severity,
515 const char *fmt, va_list args)
516{
517 char buf[128];
518 vsnprintf(buf, sizeof(buf), fmt, args);
519 buf[sizeof(buf) - 1] = 0;
520 if (filename != NULL)
521 output_line(0, "%s:%d: %s: %s",
522 filename, line_no, severity, buf);
523 else
524 output_line(0, "%s: %s", severity, buf);
525}
526
527void
528report_error(const char *filename, unsigned line_no, char *fmt, ...)
529{
530 va_list args;
531 va_start(args, fmt);
532 do_report(filename, line_no, "error", fmt, args);
533 va_end(args);
534}
535
536void
537report_warning(const char *filename, unsigned line_no, char *fmt, ...)
538{
539 va_list args;
540 va_start(args, fmt);
541 do_report(filename, line_no, "warning", fmt, args);
542 va_end(args);
543}
544
545void
546report_global_error(char *fmt, ...)
547{
548 va_list args;
549 va_start(args, fmt);
550 do_report(NULL, 0, "error", fmt, args);
551 va_end(args);
552}