blob: e513201c2379af850bbb971a44ca7d766e527b1f [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>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020034
Juan Cespedesf7281232009-06-25 16:11:21 +020035#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010036#include "proc.h"
Petr Machata29add4f2012-02-18 16:38:05 +010037#include "library.h"
Petr Machata000e3112012-01-03 17:03:39 +010038#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010039#include "value.h"
40#include "value_dict.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020041
Juan Cespedesf7281232009-06-25 16:11:21 +020042/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010043extern struct timeval current_time_spent;
44
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020045Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010046
Juan Cespedesa8909f72009-04-28 20:02:41 +020047static Process *current_proc = 0;
Juan Cespedes5916fda2002-02-25 00:19:21 +010048static int current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010049static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020050
Juan Cespedesf1350522008-12-16 18:19:58 +010051static void
Petr Machata54004752012-05-03 18:36:48 +020052output_indent(struct Process *proc)
53{
54 int d = options.indent * (proc->callstack_depth - 1);
55 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020056}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020057
Juan Cespedesf1350522008-12-16 18:19:58 +010058static void
Juan Cespedesa8909f72009-04-28 20:02:41 +020059begin_of_line(enum tof type, Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010060 current_column = 0;
61 if (!proc) {
62 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020063 }
Juan Cespedesc693f022009-05-21 18:59:41 +020064 if ((options.output != stderr) && (opt_p || options.follow)) {
65 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020066 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010067 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010068 }
Juan Cespedesf666d191998-09-20 23:04:34 +020069 if (opt_r) {
70 struct timeval tv;
71 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010072 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020073 struct timeval diff;
74
75 gettimeofday(&tv, &tz);
76
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
78 old_tv.tv_sec = tv.tv_sec;
79 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020080 }
81 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
82 if (tv.tv_usec >= old_tv.tv_usec) {
83 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
84 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020085 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020086 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
87 }
88 old_tv.tv_sec = tv.tv_sec;
89 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010090 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020092 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020093 if (opt_t) {
94 struct timeval tv;
95 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020096
97 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010098 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010099 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100100 tv.tv_sec, (int)tv.tv_usec);
101 } else if (opt_t > 1) {
102 struct tm *tmp = localtime(&tv.tv_sec);
103 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100104 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
106 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200107 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100109 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 tmp->tm_hour, tmp->tm_min,
111 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200112 }
113 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100114 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115 if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100116 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100117 proc->return_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100118 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100119 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100121 }
122 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100123 if (options.indent > 0 && type != LT_TOF_NONE) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200124 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200125 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200126}
127
Juan Cespedescde58262009-05-07 11:09:00 +0200128static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +0200129name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200130 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100132
133 tmp = list_of_functions;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100134 while (tmp) {
Juan Cespedesd914a202004-11-10 00:15:33 +0100135#ifdef USE_DEMANGLE
Juan Cespedesce377d52008-12-16 19:38:10 +0100136 str1 = options.demangle ? my_demangle(tmp->name) : tmp->name;
137 str2 = options.demangle ? my_demangle(name) : name;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200138#else
139 str1 = tmp->name;
140 str2 = name;
141#endif
142 if (!strcmp(str1, str2)) {
143
Juan Cespedes5e01f651998-03-08 22:31:44 +0100144 return tmp;
145 }
146 tmp = tmp->next;
147 }
148 return NULL;
149}
150
Juan Cespedesf1350522008-12-16 18:19:58 +0100151void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200152output_line(Process *proc, char *fmt, ...) {
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200153 va_list args;
154
Juan Cespedesda9b9532009-04-07 15:33:50 +0200155 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100156 return;
157 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200158 if (current_proc) {
159 if (current_proc->callstack[current_depth].return_addr) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100160 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200161 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100162 fprintf(options.output, " <no return ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200163 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100164 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200165 current_proc = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200166 if (!fmt) {
167 return;
168 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100169 begin_of_line(LT_TOF_NONE, proc);
170
Juan Cespedes21c63a12001-07-07 20:56:56 +0200171 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100172 vfprintf(options.output, fmt, args);
173 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200174 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100175 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100176}
177
Juan Cespedesf1350522008-12-16 18:19:58 +0100178static void
179tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100181 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100182 }
183}
184
Juan Cespedesf1350522008-12-16 18:19:58 +0100185void
Petr Machata29add4f2012-02-18 16:38:05 +0100186output_left(enum tof type, struct Process *proc,
187 struct library_symbol *libsym)
188{
189 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200190 Function *func;
Petr Machata000e3112012-01-03 17:03:39 +0100191 static struct arg_type_info *arg_unknown = NULL;
Steve Finkb0315a02006-08-07 04:22:06 +0200192 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200193 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100194
Juan Cespedesda9b9532009-04-07 15:33:50 +0200195 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100196 return;
197 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200198 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100199 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100201 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200202 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100203 current_depth = proc->callstack_depth;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204 begin_of_line(type, proc);
Petr Machata53bc49b2012-04-25 17:23:02 +0200205 if (!options.hide_caller && libsym->lib != NULL
206 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machatab89c2562012-04-03 17:00:28 +0200207 current_column += fprintf(options.output, "%s->",
208 libsym->lib->soname);
Juan Cespedesd914a202004-11-10 00:15:33 +0100209#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100210 current_column +=
Petr Machata03673892012-04-03 13:51:09 +0200211 fprintf(options.output, "%s(",
Petr Machata29add4f2012-02-18 16:38:05 +0100212 (options.demangle
Petr Machataf3db07a2012-03-21 05:16:46 +0100213 ? my_demangle(function_name) : function_name));
Juan Cespedesac3db291998-04-25 14:31:58 +0200214#else
Petr Machataa43876e2012-04-17 00:40:04 +0200215 current_column += fprintf(options.output, "%s(", function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200216#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100217
218 func = name2func(function_name);
Petr Machata94078ec2012-01-05 18:07:02 +0100219
220 struct value_dict *arguments = malloc(sizeof(*arguments));
221 if (arguments == NULL)
222 return;
223 val_dict_init(arguments);
224
225 int num, right;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100226 if (!func) {
227 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100228 for (i = 0; i < 4; i++) {
Petr Machata94078ec2012-01-05 18:07:02 +0100229 long l = gimme_arg(type, proc, i, arg_unknown);
230 struct value val;
231 value_init(&val, proc, NULL, arg_unknown, 0);
232 value_set_long(&val, l);
233 val_dict_push_next(arguments, &val);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100234 }
Petr Machata94078ec2012-01-05 18:07:02 +0100235 right = 0;
236 num = 4;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100237 } else {
238 int i;
Petr Machata94078ec2012-01-05 18:07:02 +0100239 for (i = 0; i < func->num_params; i++) {
240 long l = gimme_arg(type, proc, i, func->arg_info[i]);
241 struct value val;
242 value_init(&val, proc, NULL, func->arg_info[i], 0);
243 value_set_long(&val, l);
244 val_dict_push_next(arguments, &val);
245 }
246 right = func->params_right;
247 num = func->num_params;
248 }
249
250 int i;
251 for (i = 0; i < num - right - 1; i++) {
252 current_column +=
253 format_argument(options.output,
254 val_dict_get_num(arguments, i),
255 arguments);
256 current_column += fprintf(options.output, ", ");
257 }
258
259 if (num > right) {
260 current_column +=
261 format_argument(options.output,
262 val_dict_get_num(arguments, i),
263 arguments);
264 if (right) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100265 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100266 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100267 }
Petr Machata94078ec2012-01-05 18:07:02 +0100268
269 struct callstack_element *stel
270 = &proc->callstack[proc->callstack_depth - 1];
271 stel->arguments = arguments;
272
273 save_register_args(type, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100274}
275
Juan Cespedesf1350522008-12-16 18:19:58 +0100276void
Petr Machata29add4f2012-02-18 16:38:05 +0100277output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100278{
Petr Machata29add4f2012-02-18 16:38:05 +0100279 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200280 Function *func = name2func(function_name);
Petr Machata000e3112012-01-03 17:03:39 +0100281 static struct arg_type_info *arg_unknown = NULL;
Steve Finkb0315a02006-08-07 04:22:06 +0200282 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200283 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100284
Juan Cespedesda9b9532009-04-07 15:33:50 +0200285 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100287 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100288 dict_opt_c =
289 dict_init(dict_key2hash_string,
290 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100291 }
292 st = dict_find_entry(dict_opt_c, function_name);
293 if (!st) {
294 char *na;
295 st = malloc(sizeof(struct opt_c_struct));
296 na = strdup(function_name);
297 if (!st || !na) {
298 perror("malloc()");
299 exit(1);
300 }
301 st->count = 0;
302 st->tv.tv_sec = st->tv.tv_usec = 0;
303 dict_enter(dict_opt_c, na, st);
304 }
305 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
306 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
307 st->tv.tv_sec++;
308 } else {
309 st->tv.tv_usec += current_time_spent.tv_usec;
310 }
311 st->count++;
312 st->tv.tv_sec += current_time_spent.tv_sec;
313
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100314// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100315// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100316 return;
317 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200318 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100319 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100320 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200321 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200322 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200323 if (current_proc != proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100324 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100325#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100327 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100328 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200329#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100330 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100331 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200332#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100333 }
334
Petr Machata94078ec2012-01-05 18:07:02 +0100335 struct callstack_element *stel
336 = &proc->callstack[proc->callstack_depth - 1];
337
338 struct value retval;
339 struct arg_type_info *return_info = arg_unknown;
340 if (func != NULL)
341 return_info = func->return_info;
342 long l = gimme_arg(type, proc, -1, return_info);
343 value_init(&retval, proc, NULL, return_info, 0);
344 value_set_long(&retval, l);
345 val_dict_push_named(stel->arguments, &retval, "retval", 0);
346
Juan Cespedes5e01f651998-03-08 22:31:44 +0100347 if (!func) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100348 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100349 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100350 fprintf(options.output, "= ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100351 } else {
352 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100353 for (i = func->num_params - func->params_right;
354 i < func->num_params - 1; i++) {
355 current_column +=
Petr Machata94078ec2012-01-05 18:07:02 +0100356 format_argument(options.output,
357 val_dict_get_num
358 (stel->arguments, i),
359 stel->arguments);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100360 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100361 }
362 if (func->params_right) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100363 current_column +=
Petr Machata94078ec2012-01-05 18:07:02 +0100364 format_argument(options.output,
365 val_dict_get_num
366 (stel->arguments, i),
367 stel->arguments);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100368 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100369 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100370 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100371 fprintf(options.output, "= ");
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200372 }
Petr Machata94078ec2012-01-05 18:07:02 +0100373
374 format_argument(options.output, &retval, stel->arguments);
375 val_dict_destroy(stel->arguments);
376
Juan Cespedesd65efa32003-02-03 00:22:30 +0100377 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100378 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100379 current_time_spent.tv_sec,
380 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100381 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100382 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700383
384#if defined(HAVE_LIBUNWIND)
385 if (options.bt_depth > 0) {
386 unw_cursor_t cursor;
387 unw_word_t ip, sp;
388 int unwind_depth = options.bt_depth;
389 char fn_name[100];
390
391 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
392 while (unwind_depth) {
393 unw_get_reg(&cursor, UNW_REG_IP, &ip);
394 unw_get_reg(&cursor, UNW_REG_SP, &sp);
395 unw_get_proc_name(&cursor, fn_name, 100, NULL);
396 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
397 if (unw_step(&cursor) <= 0)
398 break;
399 unwind_depth--;
400 }
401 fprintf(options.output, "\n");
402 }
403#endif /* defined(HAVE_LIBUNWIND) */
404
Paul Gilliam76c61f12006-06-14 06:55:21 +0200405 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100407}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100408
409static void
410do_report(const char *filename, unsigned line_no, const char *severity,
411 const char *fmt, va_list args)
412{
413 char buf[128];
414 vsnprintf(buf, sizeof(buf), fmt, args);
415 buf[sizeof(buf) - 1] = 0;
416 if (filename != NULL)
417 output_line(0, "%s:%d: %s: %s",
418 filename, line_no, severity, buf);
419 else
420 output_line(0, "%s: %s", severity, buf);
421}
422
423void
424report_error(const char *filename, unsigned line_no, char *fmt, ...)
425{
426 va_list args;
427 va_start(args, fmt);
428 do_report(filename, line_no, "error", fmt, args);
429 va_end(args);
430}
431
432void
433report_warning(const char *filename, unsigned line_no, char *fmt, ...)
434{
435 va_list args;
436 va_start(args, fmt);
437 do_report(filename, line_no, "warning", fmt, args);
438 va_end(args);
439}
440
441void
442report_global_error(char *fmt, ...)
443{
444 va_list args;
445 va_start(args, fmt);
446 do_report(NULL, 0, "error", fmt, args);
447 va_end(args);
448}