blob: e1b3a2a39c0cf1c64a6bcc25464939755f3081de [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
Petr Machata37b73c02012-01-06 16:00:25 +010059begin_of_line(Process *proc, int is_func, int indent)
60{
Juan Cespedes5e01f651998-03-08 22:31:44 +010061 current_column = 0;
62 if (!proc) {
63 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020064 }
Juan Cespedesc693f022009-05-21 18:59:41 +020065 if ((options.output != stderr) && (opt_p || options.follow)) {
66 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020067 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010068 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010069 }
Juan Cespedesf666d191998-09-20 23:04:34 +020070 if (opt_r) {
71 struct timeval tv;
72 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020074 struct timeval diff;
75
76 gettimeofday(&tv, &tz);
77
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
79 old_tv.tv_sec = tv.tv_sec;
80 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020081 }
82 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
83 if (tv.tv_usec >= old_tv.tv_usec) {
84 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
85 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020086 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020087 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
88 }
89 old_tv.tv_sec = tv.tv_sec;
90 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010091 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020093 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020094 if (opt_t) {
95 struct timeval tv;
96 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020097
98 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100100 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 tv.tv_sec, (int)tv.tv_usec);
102 } else if (opt_t > 1) {
103 struct tm *tmp = localtime(&tv.tv_sec);
104 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100105 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
107 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200108 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100110 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 tmp->tm_hour, tmp->tm_min,
112 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200113 }
114 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100115 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100116 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100117 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100118 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100119 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100120 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100121 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100122 }
Petr Machata37b73c02012-01-06 16:00:25 +0100123 if (options.indent > 0 && indent) {
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 }
Petr Machata37b73c02012-01-06 16:00:25 +0100169 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100170
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;
Petr Machata37b73c02012-01-06 16:00:25 +0100204 begin_of_line(type, type == LT_TOF_FUNCTION, 1);
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;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100272}
273
Juan Cespedesf1350522008-12-16 18:19:58 +0100274void
Petr Machata29add4f2012-02-18 16:38:05 +0100275output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100276{
Petr Machata29add4f2012-02-18 16:38:05 +0100277 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200278 Function *func = name2func(function_name);
Petr Machata000e3112012-01-03 17:03:39 +0100279 static struct arg_type_info *arg_unknown = NULL;
Steve Finkb0315a02006-08-07 04:22:06 +0200280 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200281 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100282
Juan Cespedesda9b9532009-04-07 15:33:50 +0200283 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100285 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 dict_opt_c =
287 dict_init(dict_key2hash_string,
288 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100289 }
290 st = dict_find_entry(dict_opt_c, function_name);
291 if (!st) {
292 char *na;
293 st = malloc(sizeof(struct opt_c_struct));
294 na = strdup(function_name);
295 if (!st || !na) {
296 perror("malloc()");
297 exit(1);
298 }
299 st->count = 0;
300 st->tv.tv_sec = st->tv.tv_usec = 0;
301 dict_enter(dict_opt_c, na, st);
302 }
303 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
304 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
305 st->tv.tv_sec++;
306 } else {
307 st->tv.tv_usec += current_time_spent.tv_usec;
308 }
309 st->count++;
310 st->tv.tv_sec += current_time_spent.tv_sec;
311
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100312// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100313// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100314 return;
315 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200316 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100317 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100318 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200319 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200320 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200321 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100322 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100323#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100324 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100325 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100326 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200327#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100328 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100329 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200330#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100331 }
332
Petr Machata94078ec2012-01-05 18:07:02 +0100333 struct callstack_element *stel
334 = &proc->callstack[proc->callstack_depth - 1];
335
336 struct value retval;
337 struct arg_type_info *return_info = arg_unknown;
338 if (func != NULL)
339 return_info = func->return_info;
340 long l = gimme_arg(type, proc, -1, return_info);
341 value_init(&retval, proc, NULL, return_info, 0);
342 value_set_long(&retval, l);
343 val_dict_push_named(stel->arguments, &retval, "retval", 0);
344
Juan Cespedes5e01f651998-03-08 22:31:44 +0100345 if (!func) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100346 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100347 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100348 fprintf(options.output, "= ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100349 } else {
350 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100351 for (i = func->num_params - func->params_right;
352 i < func->num_params - 1; i++) {
353 current_column +=
Petr Machata94078ec2012-01-05 18:07:02 +0100354 format_argument(options.output,
355 val_dict_get_num
356 (stel->arguments, i),
357 stel->arguments);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100358 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100359 }
360 if (func->params_right) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100361 current_column +=
Petr Machata94078ec2012-01-05 18:07:02 +0100362 format_argument(options.output,
363 val_dict_get_num
364 (stel->arguments, i),
365 stel->arguments);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100366 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100367 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100368 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100369 fprintf(options.output, "= ");
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200370 }
Petr Machata94078ec2012-01-05 18:07:02 +0100371
372 format_argument(options.output, &retval, stel->arguments);
373 val_dict_destroy(stel->arguments);
374
Juan Cespedesd65efa32003-02-03 00:22:30 +0100375 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100376 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100377 current_time_spent.tv_sec,
378 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100379 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100380 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700381
382#if defined(HAVE_LIBUNWIND)
383 if (options.bt_depth > 0) {
384 unw_cursor_t cursor;
385 unw_word_t ip, sp;
386 int unwind_depth = options.bt_depth;
387 char fn_name[100];
388
389 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
390 while (unwind_depth) {
391 unw_get_reg(&cursor, UNW_REG_IP, &ip);
392 unw_get_reg(&cursor, UNW_REG_SP, &sp);
393 unw_get_proc_name(&cursor, fn_name, 100, NULL);
394 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
395 if (unw_step(&cursor) <= 0)
396 break;
397 unwind_depth--;
398 }
399 fprintf(options.output, "\n");
400 }
401#endif /* defined(HAVE_LIBUNWIND) */
402
Paul Gilliam76c61f12006-06-14 06:55:21 +0200403 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100404 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100405}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100406
407static void
408do_report(const char *filename, unsigned line_no, const char *severity,
409 const char *fmt, va_list args)
410{
411 char buf[128];
412 vsnprintf(buf, sizeof(buf), fmt, args);
413 buf[sizeof(buf) - 1] = 0;
414 if (filename != NULL)
415 output_line(0, "%s:%d: %s: %s",
416 filename, line_no, severity, buf);
417 else
418 output_line(0, "%s: %s", severity, buf);
419}
420
421void
422report_error(const char *filename, unsigned line_no, char *fmt, ...)
423{
424 va_list args;
425 va_start(args, fmt);
426 do_report(filename, line_no, "error", fmt, args);
427 va_end(args);
428}
429
430void
431report_warning(const char *filename, unsigned line_no, char *fmt, ...)
432{
433 va_list args;
434 va_start(args, fmt);
435 do_report(filename, line_no, "warning", fmt, args);
436 va_end(args);
437}
438
439void
440report_global_error(char *fmt, ...)
441{
442 va_list args;
443 va_start(args, fmt);
444 do_report(NULL, 0, "error", fmt, args);
445 va_end(args);
446}