blob: fe62bb463bef411c58799090732665fc45801eb9 [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
Petr Machata929bd572012-12-17 03:20:34 +010053static struct 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 Machata929bd572012-12-17 03:20:34 +010058output_indent(struct process *proc)
Petr Machata54004752012-05-03 18:36:48 +020059{
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 Machata929bd572012-12-17 03:20:34 +010065begin_of_line(struct process *proc, int is_func, int indent)
Petr Machata37b73c02012-01-06 16:00:25 +010066{
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 Machata929bd572012-12-17 03:20:34 +0100208output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200209{
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
Petr Machata929bd572012-12-17 03:20:34 +0100248fetch_simple_param(enum tof type, struct process *proc,
249 struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100250 struct value_dict *arguments,
251 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100252 struct value *valuep)
253{
254 /* Arrays decay into pointers per C standard. We check for
255 * this here, because here we also capture arrays that come
256 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100257 if (info->type == ARGTYPE_ARRAY) {
258 struct arg_type_info *tmp = malloc(sizeof(*tmp));
259 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100260 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100261 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100262 info = tmp;
263 own = 1;
264 }
265 }
266
267 struct value value;
268 value_init(&value, proc, NULL, info, own);
269 if (fetch_arg_next(context, type, proc, info, &value) < 0)
270 return -1;
271
272 if (val_dict_push_next(arguments, &value) < 0) {
273 value_destroy(&value);
274 return -1;
275 }
276
277 if (valuep != NULL)
278 *valuep = value;
279
280 return 0;
281}
282
283static void
284fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
285{
286 if (*params_leftp == -1)
287 *params_leftp = val_dict_count(arguments);
288}
289
290static int
Petr Machata929bd572012-12-17 03:20:34 +0100291fetch_param_pack(enum tof type, struct process *proc,
292 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100293 struct value_dict *arguments, struct param *param,
294 ssize_t *params_leftp)
295{
296 struct param_enum *e = param_pack_init(param, arguments);
297 if (e == NULL)
298 return -1;
299
300 int ret = 0;
301 while (1) {
302 int insert_stop = 0;
303 struct arg_type_info *info = malloc(sizeof(*info));
304 if (info == NULL
305 || param_pack_next(param, e, info, &insert_stop) < 0) {
306 fail:
307 free(info);
308 ret = -1;
309 break;
310 }
311
312 if (insert_stop)
313 fetch_param_stop(arguments, params_leftp);
314
Petr Machatabc58f2a2012-10-30 00:25:50 +0100315 if (info->type == ARGTYPE_VOID) {
316 type_destroy(info);
317 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100318 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100319 }
Petr Machata865303f2012-01-06 18:40:38 +0100320
321 struct value val;
322 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100323 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100324 goto fail;
325
326 int stop = 0;
327 switch (param_pack_stop(param, e, &val)) {
328 case PPCB_ERR:
329 goto fail;
330 case PPCB_STOP:
331 stop = 1;
332 case PPCB_CONT:
333 break;
334 }
335
336 if (stop)
337 break;
338 }
339
340 param_pack_done(param, e);
341 return ret;
342}
343
344static int
Petr Machata929bd572012-12-17 03:20:34 +0100345fetch_one_param(enum tof type, struct process *proc,
346 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100347 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100348 ssize_t *params_leftp)
349{
Petr Machata865303f2012-01-06 18:40:38 +0100350 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200351 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100352 case PARAM_FLAVOR_TYPE:
353 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100354 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100355
356 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200357 if (fetch_param_pack_start(context,
358 param->u.pack.ppflavor) < 0)
359 return -1;
360 rc = fetch_param_pack(type, proc, context, arguments,
361 param, params_leftp);
362 fetch_param_pack_end(context);
363 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100364
365 case PARAM_FLAVOR_STOP:
366 fetch_param_stop(arguments, params_leftp);
367 return 0;
368 }
369
370 assert(!"Invalid param flavor!");
371 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100372}
373
374static int
Petr Machata929bd572012-12-17 03:20:34 +0100375fetch_params(enum tof type, struct process *proc,
376 struct fetch_context *context,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100377 struct value_dict *arguments, Function *func, ssize_t *params_leftp)
378{
379 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100380 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100381 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100382 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100383 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100384
385 /* Implicit stop at the end of parameter list. */
386 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100387
Petr Machataf6ec08a2012-01-06 16:58:54 +0100388 return 0;
389}
390
Petr Machataf7c46bb2012-11-01 22:15:45 +0100391struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100392{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100393 struct value *value;
394 struct value_dict *arguments;
395};
396
397static int
398format_argument_cb(FILE *stream, void *ptr)
399{
400 struct format_argument_data *data = ptr;
401 int o = format_argument(stream, data->value, data->arguments);
402 if (o < 0)
403 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100404 return o;
405}
406
407static int
408output_params(struct value_dict *arguments, size_t start, size_t end,
409 int *need_delimp)
410{
411 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100412 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100413 struct value *value = val_dict_get_num(arguments, i);
414 if (value == NULL)
415 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100416
417 struct format_argument_data data = { value, arguments };
418 int o = delim_output(options.output, need_delimp,
419 format_argument_cb, &data);
420 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100421 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100422 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100423 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100424 return 0;
425}
426
Juan Cespedesf1350522008-12-16 18:19:58 +0100427void
Petr Machata929bd572012-12-17 03:20:34 +0100428output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100429 struct library_symbol *libsym)
430{
431 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200432 Function *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100433
Juan Cespedesda9b9532009-04-07 15:33:50 +0200434 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100435 return;
436 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200437 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100438 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100440 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200441 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100442 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100443 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200444 if (!options.hide_caller && libsym->lib != NULL
445 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100446 /* We don't terribly mind failing this. */
447 account_output(&current_column,
448 fprintf(options.output, "%s->",
449 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100450
451 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100452#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100453 if (options.demangle)
454 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200455#endif
Petr Machataadec2012012-11-01 22:05:04 +0100456 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100457 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100458 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100459
Petr Machata09654202012-12-05 17:07:21 +0100460 if (libsym->lib != NULL
461 && libsym->lib->type != LT_LIBTYPE_MAIN
462 && libsym->plt_type == LS_TOPLT_NONE
463 && account_output(&current_column,
464 fprintf(options.output, "@%s",
465 libsym->lib->soname)) < 0)
466 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100467 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100468
Petr Machata09654202012-12-05 17:07:21 +0100469 account_output(&current_column, fprintf(options.output, "("));
470
471 func = name2func(function_name);
472 if (func == NULL) {
473 account_output(&current_column, fprintf(options.output, "???"));
474 return;
475 }
476
Petr Machataf6ec08a2012-01-06 16:58:54 +0100477 struct fetch_context *context = fetch_arg_init(type, proc,
478 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100479 struct value_dict *arguments = malloc(sizeof(*arguments));
480 if (arguments == NULL)
481 return;
482 val_dict_init(arguments);
483
Petr Machataf6ec08a2012-01-06 16:58:54 +0100484 ssize_t params_left = -1;
485 int need_delim = 0;
486 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
487 || output_params(arguments, 0, params_left, &need_delim) < 0) {
488 val_dict_destroy(arguments);
489 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100490 arguments = NULL;
491 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100492 }
Petr Machata94078ec2012-01-05 18:07:02 +0100493
494 struct callstack_element *stel
495 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100496 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100497 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100498 stel->out.params_left = params_left;
499 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100500}
501
Juan Cespedesf1350522008-12-16 18:19:58 +0100502void
Petr Machata929bd572012-12-17 03:20:34 +0100503output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100504{
Petr Machata29add4f2012-02-18 16:38:05 +0100505 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200506 Function *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100507 if (func == NULL)
508 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100509
Juan Cespedesda9b9532009-04-07 15:33:50 +0200510 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100511 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100512 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100513 dict_opt_c =
514 dict_init(dict_key2hash_string,
515 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100516 }
517 st = dict_find_entry(dict_opt_c, function_name);
518 if (!st) {
519 char *na;
520 st = malloc(sizeof(struct opt_c_struct));
521 na = strdup(function_name);
522 if (!st || !na) {
523 perror("malloc()");
524 exit(1);
525 }
526 st->count = 0;
527 st->tv.tv_sec = st->tv.tv_usec = 0;
528 dict_enter(dict_opt_c, na, st);
529 }
530 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
531 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
532 st->tv.tv_sec++;
533 } else {
534 st->tv.tv_usec += current_time_spent.tv_usec;
535 }
536 st->count++;
537 st->tv.tv_sec += current_time_spent.tv_sec;
538
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100539// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100540// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100541 return;
542 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200543 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100544 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100545 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200546 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200547 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200548 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100549 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100550#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100551 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100552 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100553 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200554#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100555 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100556 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200557#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100558 }
559
Petr Machata94078ec2012-01-05 18:07:02 +0100560 struct callstack_element *stel
561 = &proc->callstack[proc->callstack_depth - 1];
562
Petr Machataf6ec08a2012-01-06 16:58:54 +0100563 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100564
Petr Machataf6ec08a2012-01-06 16:58:54 +0100565 /* Fetch & enter into dictionary the retval first, so that
566 * other values can use it in expressions. */
567 struct value retval;
568 int own_retval = 0;
569 if (context != NULL) {
570 value_init(&retval, proc, NULL, func->return_info, 0);
571 own_retval = 1;
572 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200573 &retval) < 0)
574 value_set_type(&retval, NULL, 0);
575 else if (stel->arguments != NULL
576 && val_dict_push_named(stel->arguments, &retval,
577 "retval", 0) == 0)
578 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200579 }
Petr Machata94078ec2012-01-05 18:07:02 +0100580
Petr Machataf6ec08a2012-01-06 16:58:54 +0100581 if (stel->arguments != NULL)
582 output_params(stel->arguments, stel->out.params_left,
583 val_dict_count(stel->arguments),
584 &stel->out.need_delim);
585
586 current_column += fprintf(options.output, ") ");
587 tabto(options.align - 1);
588 fprintf(options.output, "= ");
589
Petr Machataf7c46bb2012-11-01 22:15:45 +0100590 if (context != NULL && retval.type != NULL) {
591 struct format_argument_data data = { &retval, stel->arguments };
592 format_argument_cb(options.output, &data);
593 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100594
595 if (own_retval)
596 value_destroy(&retval);
597
Juan Cespedesd65efa32003-02-03 00:22:30 +0100598 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100599 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100600 current_time_spent.tv_sec,
601 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100602 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100603 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700604
605#if defined(HAVE_LIBUNWIND)
606 if (options.bt_depth > 0) {
607 unw_cursor_t cursor;
608 unw_word_t ip, sp;
609 int unwind_depth = options.bt_depth;
610 char fn_name[100];
611
612 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
613 while (unwind_depth) {
614 unw_get_reg(&cursor, UNW_REG_IP, &ip);
615 unw_get_reg(&cursor, UNW_REG_SP, &sp);
616 unw_get_proc_name(&cursor, fn_name, 100, NULL);
617 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
618 if (unw_step(&cursor) <= 0)
619 break;
620 unwind_depth--;
621 }
622 fprintf(options.output, "\n");
623 }
624#endif /* defined(HAVE_LIBUNWIND) */
625
Paul Gilliam76c61f12006-06-14 06:55:21 +0200626 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100627 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100628}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100629
Petr Machataadec2012012-11-01 22:05:04 +0100630int
Petr Machata9c400982012-11-01 22:15:01 +0100631delim_output(FILE *stream, int *need_delimp,
632 int (*writer)(FILE *stream, void *data),
633 void *data)
634{
635 int o;
636
637 /* If we don't need a delimiter, then we don't need to go
638 * through a temporary stream. It's all the same whether
639 * WRITER emits anything or not. */
640 if (!*need_delimp) {
641 o = writer(stream, data);
642
643 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100644 struct memstream ms;
645 if (memstream_init(&ms) < 0)
646 return -1;
647 o = writer(ms.stream, data);
648 if (memstream_close(&ms) < 0)
649 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100650 if (o > 0 && ((*need_delimp
651 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100652 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100653 o = -1;
654
Petr Machata2ae374a2012-12-08 05:08:48 +0100655 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100656 }
657
658 if (o < 0)
659 return -1;
660
661 *need_delimp = *need_delimp || o > 0;
662 return o;
663}
664
665int
Petr Machataadec2012012-11-01 22:05:04 +0100666account_output(int *countp, int c)
667{
668 if (c > 0)
669 *countp += c;
670 return c;
671}
672
Petr Machatad71cf2d2012-01-05 18:28:31 +0100673static void
674do_report(const char *filename, unsigned line_no, const char *severity,
675 const char *fmt, va_list args)
676{
677 char buf[128];
678 vsnprintf(buf, sizeof(buf), fmt, args);
679 buf[sizeof(buf) - 1] = 0;
680 if (filename != NULL)
681 output_line(0, "%s:%d: %s: %s",
682 filename, line_no, severity, buf);
683 else
684 output_line(0, "%s: %s", severity, buf);
685}
686
687void
Petr Machata89c5ca22012-10-27 00:49:38 +0200688report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100689{
690 va_list args;
691 va_start(args, fmt);
692 do_report(filename, line_no, "error", fmt, args);
693 va_end(args);
694}
695
696void
Petr Machata89c5ca22012-10-27 00:49:38 +0200697report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100698{
699 va_list args;
700 va_start(args, fmt);
701 do_report(filename, line_no, "warning", fmt, args);
702 va_end(args);
703}
704
705void
Petr Machata89c5ca22012-10-27 00:49:38 +0200706report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100707{
708 va_list args;
709 va_start(args, fmt);
710 do_report(NULL, 0, "error", fmt, args);
711 va_end(args);
712}