blob: 2037f465f511c181ae284d3cdfe0605239fb422d [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
Petr Machatae99af272012-10-26 00:29:52 +02006 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
Petr Machata94078ec2012-01-05 18:07:02 +01007 * 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"
Petr Machatab7819162012-01-09 04:26:15 +010045#include "lens_default.h"
Petr Machata2ae374a2012-12-08 05:08:48 +010046#include "memstream.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020047
Juan Cespedesf7281232009-06-25 16:11:21 +020048/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010049extern struct timeval current_time_spent;
50
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020051Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010052
Juan Cespedesa8909f72009-04-28 20:02:41 +020053static Process *current_proc = 0;
Petr Machataba1664b2012-04-28 14:59:05 +020054static size_t current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010055static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020056
Juan Cespedesf1350522008-12-16 18:19:58 +010057static void
Petr Machata54004752012-05-03 18:36:48 +020058output_indent(struct Process *proc)
59{
60 int d = options.indent * (proc->callstack_depth - 1);
61 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020062}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020063
Juan Cespedesf1350522008-12-16 18:19:58 +010064static void
Petr Machata37b73c02012-01-06 16:00:25 +010065begin_of_line(Process *proc, int is_func, int indent)
66{
Juan Cespedes5e01f651998-03-08 22:31:44 +010067 current_column = 0;
68 if (!proc) {
69 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020070 }
Juan Cespedesc693f022009-05-21 18:59:41 +020071 if ((options.output != stderr) && (opt_p || options.follow)) {
72 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020073 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010074 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 }
Juan Cespedesf666d191998-09-20 23:04:34 +020076 if (opt_r) {
77 struct timeval tv;
78 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020080 struct timeval diff;
81
82 gettimeofday(&tv, &tz);
83
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
85 old_tv.tv_sec = tv.tv_sec;
86 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020087 }
88 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
89 if (tv.tv_usec >= old_tv.tv_usec) {
90 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
91 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020092 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020093 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
94 }
95 old_tv.tv_sec = tv.tv_sec;
96 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010097 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010098 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020099 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200100 if (opt_t) {
101 struct timeval tv;
102 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200103
104 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100106 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100107 tv.tv_sec, (int)tv.tv_usec);
108 } else if (opt_t > 1) {
109 struct tm *tmp = localtime(&tv.tv_sec);
110 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100111 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
113 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200114 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100116 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100117 tmp->tm_hour, tmp->tm_min,
118 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200119 }
120 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100121 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100122 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100123 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100124 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100125 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100126 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100128 }
Petr Machata37b73c02012-01-06 16:00:25 +0100129 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200130 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200131 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200132}
133
Petr Machatab7819162012-01-09 04:26:15 +0100134static struct arg_type_info *
135get_unknown_type(void)
136{
137 static struct arg_type_info *info = NULL;
138 if (info == NULL) {
139 info = malloc(sizeof(*info));
140 if (info == NULL) {
141 report_global_error("malloc: %s", strerror(errno));
142 abort();
143 }
144 *info = *type_get_simple(ARGTYPE_LONG);
145 info->lens = &guess_lens;
146 }
147 return info;
148}
149
Petr Machataf6ec08a2012-01-06 16:58:54 +0100150/* The default prototype is: long X(long, long, long, long). */
151static Function *
152build_default_prototype(void)
153{
154 Function *ret = malloc(sizeof(*ret));
155 size_t i = 0;
156 if (ret == NULL)
157 goto err;
158 memset(ret, 0, sizeof(*ret));
159
Petr Machatab7819162012-01-09 04:26:15 +0100160 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100161
162 ret->return_info = unknown_type;
Petr Machata3a9bf6d2012-01-06 21:44:10 +0100163 ret->own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100164
165 ret->num_params = 4;
Petr Machata865303f2012-01-06 18:40:38 +0100166 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
167 if (ret->params == NULL)
168 goto err;
169
170 for (i = 0; i < ret->num_params; ++i)
171 param_init_type(&ret->params[i], unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100172
173 return ret;
174
175err:
176 report_global_error("malloc: %s", strerror(errno));
Petr Machata865303f2012-01-06 18:40:38 +0100177 if (ret->params != NULL) {
178 while (i-- > 0)
179 param_destroy(&ret->params[i]);
180 free(ret->params);
181 }
182
Petr Machataf6ec08a2012-01-06 16:58:54 +0100183 free(ret);
184
185 return NULL;
186}
187
Juan Cespedescde58262009-05-07 11:09:00 +0200188static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +0200189name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200190 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100191 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100192
Petr Machataf6ec08a2012-01-06 16:58:54 +0100193 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200194 str1 = tmp->name;
195 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100196 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100198 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100199
200 static Function *def = NULL;
201 if (def == NULL)
202 def = build_default_prototype();
203
204 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100205}
206
Juan Cespedesf1350522008-12-16 18:19:58 +0100207void
Petr Machataefc94362012-10-27 00:49:05 +0200208output_line(struct Process *proc, const char *fmt, ...)
209{
210 if (options.summary)
211 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200212
Petr Machataefc94362012-10-27 00:49:05 +0200213 if (current_proc != NULL) {
214 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100215 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200216 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100217 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100218 }
Petr Machataefc94362012-10-27 00:49:05 +0200219 current_proc = NULL;
220 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200221 return;
Petr Machataefc94362012-10-27 00:49:05 +0200222
Petr Machata37b73c02012-01-06 16:00:25 +0100223 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100224
Petr Machataefc94362012-10-27 00:49:05 +0200225 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200226 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100227 vfprintf(options.output, fmt, args);
228 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200229 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200230
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100232}
233
Juan Cespedesf1350522008-12-16 18:19:58 +0100234static void
235tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100236 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100237 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100238 }
239}
240
Petr Machataf6ec08a2012-01-06 16:58:54 +0100241static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100242output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100243{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100244 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100245}
246
247static int
248fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100249 struct value_dict *arguments,
250 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100251 struct value *valuep)
252{
253 /* Arrays decay into pointers per C standard. We check for
254 * this here, because here we also capture arrays that come
255 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100256 if (info->type == ARGTYPE_ARRAY) {
257 struct arg_type_info *tmp = malloc(sizeof(*tmp));
258 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100259 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100260 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100261 info = tmp;
262 own = 1;
263 }
264 }
265
266 struct value value;
267 value_init(&value, proc, NULL, info, own);
268 if (fetch_arg_next(context, type, proc, info, &value) < 0)
269 return -1;
270
271 if (val_dict_push_next(arguments, &value) < 0) {
272 value_destroy(&value);
273 return -1;
274 }
275
276 if (valuep != NULL)
277 *valuep = value;
278
279 return 0;
280}
281
282static void
283fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
284{
285 if (*params_leftp == -1)
286 *params_leftp = val_dict_count(arguments);
287}
288
289static int
Petr Machata865303f2012-01-06 18:40:38 +0100290fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context,
291 struct value_dict *arguments, struct param *param,
292 ssize_t *params_leftp)
293{
294 struct param_enum *e = param_pack_init(param, arguments);
295 if (e == NULL)
296 return -1;
297
298 int ret = 0;
299 while (1) {
300 int insert_stop = 0;
301 struct arg_type_info *info = malloc(sizeof(*info));
302 if (info == NULL
303 || param_pack_next(param, e, info, &insert_stop) < 0) {
304 fail:
305 free(info);
306 ret = -1;
307 break;
308 }
309
310 if (insert_stop)
311 fetch_param_stop(arguments, params_leftp);
312
Petr Machatabc58f2a2012-10-30 00:25:50 +0100313 if (info->type == ARGTYPE_VOID) {
314 type_destroy(info);
315 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100316 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100317 }
Petr Machata865303f2012-01-06 18:40:38 +0100318
319 struct value val;
320 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100321 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100322 goto fail;
323
324 int stop = 0;
325 switch (param_pack_stop(param, e, &val)) {
326 case PPCB_ERR:
327 goto fail;
328 case PPCB_STOP:
329 stop = 1;
330 case PPCB_CONT:
331 break;
332 }
333
334 if (stop)
335 break;
336 }
337
338 param_pack_done(param, e);
339 return ret;
340}
341
342static int
Petr Machataf6ec08a2012-01-06 16:58:54 +0100343fetch_one_param(enum tof type, Process *proc, struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100344 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100345 ssize_t *params_leftp)
346{
Petr Machata865303f2012-01-06 18:40:38 +0100347 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200348 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100349 case PARAM_FLAVOR_TYPE:
350 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100351 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100352
353 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200354 if (fetch_param_pack_start(context,
355 param->u.pack.ppflavor) < 0)
356 return -1;
357 rc = fetch_param_pack(type, proc, context, arguments,
358 param, params_leftp);
359 fetch_param_pack_end(context);
360 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100361
362 case PARAM_FLAVOR_STOP:
363 fetch_param_stop(arguments, params_leftp);
364 return 0;
365 }
366
367 assert(!"Invalid param flavor!");
368 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100369}
370
371static int
372fetch_params(enum tof type, Process *proc, struct fetch_context *context,
373 struct value_dict *arguments, Function *func, ssize_t *params_leftp)
374{
375 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100376 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100377 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100378 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100379 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100380
381 /* Implicit stop at the end of parameter list. */
382 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100383
Petr Machataf6ec08a2012-01-06 16:58:54 +0100384 return 0;
385}
386
Petr Machataf7c46bb2012-11-01 22:15:45 +0100387struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100388{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100389 struct value *value;
390 struct value_dict *arguments;
391};
392
393static int
394format_argument_cb(FILE *stream, void *ptr)
395{
396 struct format_argument_data *data = ptr;
397 int o = format_argument(stream, data->value, data->arguments);
398 if (o < 0)
399 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100400 return o;
401}
402
403static int
404output_params(struct value_dict *arguments, size_t start, size_t end,
405 int *need_delimp)
406{
407 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100408 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100409 struct value *value = val_dict_get_num(arguments, i);
410 if (value == NULL)
411 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100412
413 struct format_argument_data data = { value, arguments };
414 int o = delim_output(options.output, need_delimp,
415 format_argument_cb, &data);
416 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100417 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100418 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100419 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100420 return 0;
421}
422
Juan Cespedesf1350522008-12-16 18:19:58 +0100423void
Petr Machata29add4f2012-02-18 16:38:05 +0100424output_left(enum tof type, struct Process *proc,
425 struct library_symbol *libsym)
426{
427 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200428 Function *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100429
Juan Cespedesda9b9532009-04-07 15:33:50 +0200430 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100431 return;
432 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200433 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100434 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100435 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100436 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200437 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100438 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100439 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200440 if (!options.hide_caller && libsym->lib != NULL
441 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100442 /* We don't terribly mind failing this. */
443 account_output(&current_column,
444 fprintf(options.output, "%s->",
445 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100446
447 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100448#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100449 if (options.demangle)
450 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200451#endif
Petr Machataadec2012012-11-01 22:05:04 +0100452 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100453 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100454 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100455
Petr Machata09654202012-12-05 17:07:21 +0100456 if (libsym->lib != NULL
457 && libsym->lib->type != LT_LIBTYPE_MAIN
458 && libsym->plt_type == LS_TOPLT_NONE
459 && account_output(&current_column,
460 fprintf(options.output, "@%s",
461 libsym->lib->soname)) < 0)
462 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100463 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100464
Petr Machata09654202012-12-05 17:07:21 +0100465 account_output(&current_column, fprintf(options.output, "("));
466
467 func = name2func(function_name);
468 if (func == NULL) {
469 account_output(&current_column, fprintf(options.output, "???"));
470 return;
471 }
472
Petr Machataf6ec08a2012-01-06 16:58:54 +0100473 struct fetch_context *context = fetch_arg_init(type, proc,
474 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100475 struct value_dict *arguments = malloc(sizeof(*arguments));
476 if (arguments == NULL)
477 return;
478 val_dict_init(arguments);
479
Petr Machataf6ec08a2012-01-06 16:58:54 +0100480 ssize_t params_left = -1;
481 int need_delim = 0;
482 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
483 || output_params(arguments, 0, params_left, &need_delim) < 0) {
484 val_dict_destroy(arguments);
485 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100486 arguments = NULL;
487 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100488 }
Petr Machata94078ec2012-01-05 18:07:02 +0100489
490 struct callstack_element *stel
491 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100492 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100493 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100494 stel->out.params_left = params_left;
495 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100496}
497
Juan Cespedesf1350522008-12-16 18:19:58 +0100498void
Petr Machata29add4f2012-02-18 16:38:05 +0100499output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100500{
Petr Machata29add4f2012-02-18 16:38:05 +0100501 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200502 Function *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100503 if (func == NULL)
504 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100505
Juan Cespedesda9b9532009-04-07 15:33:50 +0200506 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100507 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100508 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100509 dict_opt_c =
510 dict_init(dict_key2hash_string,
511 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100512 }
513 st = dict_find_entry(dict_opt_c, function_name);
514 if (!st) {
515 char *na;
516 st = malloc(sizeof(struct opt_c_struct));
517 na = strdup(function_name);
518 if (!st || !na) {
519 perror("malloc()");
520 exit(1);
521 }
522 st->count = 0;
523 st->tv.tv_sec = st->tv.tv_usec = 0;
524 dict_enter(dict_opt_c, na, st);
525 }
526 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
527 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
528 st->tv.tv_sec++;
529 } else {
530 st->tv.tv_usec += current_time_spent.tv_usec;
531 }
532 st->count++;
533 st->tv.tv_sec += current_time_spent.tv_sec;
534
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100535// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100536// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100537 return;
538 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200539 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100540 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100541 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200542 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200543 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200544 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100545 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100546#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100547 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100548 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100549 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200550#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100551 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100552 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200553#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100554 }
555
Petr Machata94078ec2012-01-05 18:07:02 +0100556 struct callstack_element *stel
557 = &proc->callstack[proc->callstack_depth - 1];
558
Petr Machataf6ec08a2012-01-06 16:58:54 +0100559 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100560
Petr Machataf6ec08a2012-01-06 16:58:54 +0100561 /* Fetch & enter into dictionary the retval first, so that
562 * other values can use it in expressions. */
563 struct value retval;
564 int own_retval = 0;
565 if (context != NULL) {
566 value_init(&retval, proc, NULL, func->return_info, 0);
567 own_retval = 1;
568 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200569 &retval) < 0)
570 value_set_type(&retval, NULL, 0);
571 else if (stel->arguments != NULL
572 && val_dict_push_named(stel->arguments, &retval,
573 "retval", 0) == 0)
574 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200575 }
Petr Machata94078ec2012-01-05 18:07:02 +0100576
Petr Machataf6ec08a2012-01-06 16:58:54 +0100577 if (stel->arguments != NULL)
578 output_params(stel->arguments, stel->out.params_left,
579 val_dict_count(stel->arguments),
580 &stel->out.need_delim);
581
582 current_column += fprintf(options.output, ") ");
583 tabto(options.align - 1);
584 fprintf(options.output, "= ");
585
Petr Machataf7c46bb2012-11-01 22:15:45 +0100586 if (context != NULL && retval.type != NULL) {
587 struct format_argument_data data = { &retval, stel->arguments };
588 format_argument_cb(options.output, &data);
589 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100590
591 if (own_retval)
592 value_destroy(&retval);
593
Juan Cespedesd65efa32003-02-03 00:22:30 +0100594 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100595 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100596 current_time_spent.tv_sec,
597 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100598 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100599 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700600
601#if defined(HAVE_LIBUNWIND)
602 if (options.bt_depth > 0) {
603 unw_cursor_t cursor;
604 unw_word_t ip, sp;
605 int unwind_depth = options.bt_depth;
606 char fn_name[100];
607
608 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
609 while (unwind_depth) {
610 unw_get_reg(&cursor, UNW_REG_IP, &ip);
611 unw_get_reg(&cursor, UNW_REG_SP, &sp);
612 unw_get_proc_name(&cursor, fn_name, 100, NULL);
613 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
614 if (unw_step(&cursor) <= 0)
615 break;
616 unwind_depth--;
617 }
618 fprintf(options.output, "\n");
619 }
620#endif /* defined(HAVE_LIBUNWIND) */
621
Paul Gilliam76c61f12006-06-14 06:55:21 +0200622 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100623 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100624}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100625
Petr Machataadec2012012-11-01 22:05:04 +0100626int
Petr Machata9c400982012-11-01 22:15:01 +0100627delim_output(FILE *stream, int *need_delimp,
628 int (*writer)(FILE *stream, void *data),
629 void *data)
630{
631 int o;
632
633 /* If we don't need a delimiter, then we don't need to go
634 * through a temporary stream. It's all the same whether
635 * WRITER emits anything or not. */
636 if (!*need_delimp) {
637 o = writer(stream, data);
638
639 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100640 struct memstream ms;
641 if (memstream_init(&ms) < 0)
642 return -1;
643 o = writer(ms.stream, data);
644 if (memstream_close(&ms) < 0)
645 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100646 if (o > 0 && ((*need_delimp
647 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100648 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100649 o = -1;
650
Petr Machata2ae374a2012-12-08 05:08:48 +0100651 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100652 }
653
654 if (o < 0)
655 return -1;
656
657 *need_delimp = *need_delimp || o > 0;
658 return o;
659}
660
661int
Petr Machataadec2012012-11-01 22:05:04 +0100662account_output(int *countp, int c)
663{
664 if (c > 0)
665 *countp += c;
666 return c;
667}
668
Petr Machatad71cf2d2012-01-05 18:28:31 +0100669static void
670do_report(const char *filename, unsigned line_no, const char *severity,
671 const char *fmt, va_list args)
672{
673 char buf[128];
674 vsnprintf(buf, sizeof(buf), fmt, args);
675 buf[sizeof(buf) - 1] = 0;
676 if (filename != NULL)
677 output_line(0, "%s:%d: %s: %s",
678 filename, line_no, severity, buf);
679 else
680 output_line(0, "%s: %s", severity, buf);
681}
682
683void
Petr Machata89c5ca22012-10-27 00:49:38 +0200684report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100685{
686 va_list args;
687 va_start(args, fmt);
688 do_report(filename, line_no, "error", fmt, args);
689 va_end(args);
690}
691
692void
Petr Machata89c5ca22012-10-27 00:49:38 +0200693report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100694{
695 va_list args;
696 va_start(args, fmt);
697 do_report(filename, line_no, "warning", fmt, args);
698 va_end(args);
699}
700
701void
Petr Machata89c5ca22012-10-27 00:49:38 +0200702report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100703{
704 va_list args;
705 va_start(args, fmt);
706 do_report(NULL, 0, "error", fmt, args);
707 va_end(args);
708}