blob: 63c8ea5ae2d8b6ea5e8ec124c0b38d8eb6a19434 [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"
Juan Cespedesac3db291998-04-25 14:31:58 +020046
Juan Cespedesf7281232009-06-25 16:11:21 +020047/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010048extern struct timeval current_time_spent;
49
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020050Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010051
Juan Cespedesa8909f72009-04-28 20:02:41 +020052static Process *current_proc = 0;
Petr Machataba1664b2012-04-28 14:59:05 +020053static size_t current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010054static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020055
Juan Cespedesf1350522008-12-16 18:19:58 +010056static void
Petr Machata54004752012-05-03 18:36:48 +020057output_indent(struct Process *proc)
58{
59 int d = options.indent * (proc->callstack_depth - 1);
60 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020061}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020062
Juan Cespedesf1350522008-12-16 18:19:58 +010063static void
Petr Machata37b73c02012-01-06 16:00:25 +010064begin_of_line(Process *proc, int is_func, int indent)
65{
Juan Cespedes5e01f651998-03-08 22:31:44 +010066 current_column = 0;
67 if (!proc) {
68 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020069 }
Juan Cespedesc693f022009-05-21 18:59:41 +020070 if ((options.output != stderr) && (opt_p || options.follow)) {
71 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020072 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010073 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010074 }
Juan Cespedesf666d191998-09-20 23:04:34 +020075 if (opt_r) {
76 struct timeval tv;
77 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020079 struct timeval diff;
80
81 gettimeofday(&tv, &tz);
82
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
84 old_tv.tv_sec = tv.tv_sec;
85 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020086 }
87 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
88 if (tv.tv_usec >= old_tv.tv_usec) {
89 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
90 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020091 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020092 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
93 }
94 old_tv.tv_sec = tv.tv_sec;
95 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010096 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020098 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020099 if (opt_t) {
100 struct timeval tv;
101 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200102
103 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100105 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 tv.tv_sec, (int)tv.tv_usec);
107 } else if (opt_t > 1) {
108 struct tm *tmp = localtime(&tv.tv_sec);
109 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100110 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
112 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200113 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100115 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100116 tmp->tm_hour, tmp->tm_min,
117 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200118 }
119 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100120 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100121 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100122 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100123 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100124 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100125 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100127 }
Petr Machata37b73c02012-01-06 16:00:25 +0100128 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200129 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200130 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200131}
132
Petr Machatab7819162012-01-09 04:26:15 +0100133static struct arg_type_info *
134get_unknown_type(void)
135{
136 static struct arg_type_info *info = NULL;
137 if (info == NULL) {
138 info = malloc(sizeof(*info));
139 if (info == NULL) {
140 report_global_error("malloc: %s", strerror(errno));
141 abort();
142 }
143 *info = *type_get_simple(ARGTYPE_LONG);
144 info->lens = &guess_lens;
145 }
146 return info;
147}
148
Petr Machataf6ec08a2012-01-06 16:58:54 +0100149/* The default prototype is: long X(long, long, long, long). */
150static Function *
151build_default_prototype(void)
152{
153 Function *ret = malloc(sizeof(*ret));
154 size_t i = 0;
155 if (ret == NULL)
156 goto err;
157 memset(ret, 0, sizeof(*ret));
158
Petr Machatab7819162012-01-09 04:26:15 +0100159 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100160
161 ret->return_info = unknown_type;
Petr Machata3a9bf6d2012-01-06 21:44:10 +0100162 ret->own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100163
164 ret->num_params = 4;
Petr Machata865303f2012-01-06 18:40:38 +0100165 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
166 if (ret->params == NULL)
167 goto err;
168
169 for (i = 0; i < ret->num_params; ++i)
170 param_init_type(&ret->params[i], unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100171
172 return ret;
173
174err:
175 report_global_error("malloc: %s", strerror(errno));
Petr Machata865303f2012-01-06 18:40:38 +0100176 if (ret->params != NULL) {
177 while (i-- > 0)
178 param_destroy(&ret->params[i]);
179 free(ret->params);
180 }
181
Petr Machataf6ec08a2012-01-06 16:58:54 +0100182 free(ret);
183
184 return NULL;
185}
186
Juan Cespedescde58262009-05-07 11:09:00 +0200187static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +0200188name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200189 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100190 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100191
Petr Machataf6ec08a2012-01-06 16:58:54 +0100192 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200193 str1 = tmp->name;
194 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100195 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100196 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100198
199 static Function *def = NULL;
200 if (def == NULL)
201 def = build_default_prototype();
202
203 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204}
205
Juan Cespedesf1350522008-12-16 18:19:58 +0100206void
Petr Machataefc94362012-10-27 00:49:05 +0200207output_line(struct Process *proc, const char *fmt, ...)
208{
209 if (options.summary)
210 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200211
Petr Machataefc94362012-10-27 00:49:05 +0200212 if (current_proc != NULL) {
213 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100214 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200215 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100216 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100217 }
Petr Machataefc94362012-10-27 00:49:05 +0200218 current_proc = NULL;
219 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200220 return;
Petr Machataefc94362012-10-27 00:49:05 +0200221
Petr Machata37b73c02012-01-06 16:00:25 +0100222 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100223
Petr Machataefc94362012-10-27 00:49:05 +0200224 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200225 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100226 vfprintf(options.output, fmt, args);
227 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200228 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200229
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100230 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100231}
232
Juan Cespedesf1350522008-12-16 18:19:58 +0100233static void
234tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100235 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100236 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100237 }
238}
239
Petr Machataf6ec08a2012-01-06 16:58:54 +0100240static int
Petr Machataf6ec08a2012-01-06 16:58:54 +0100241output_error(void)
242{
Petr Machataadec2012012-11-01 22:05:04 +0100243 return account_output(&current_column, fprintf(options.output, "?"));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100244}
245
246static int
247fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100248 struct value_dict *arguments,
249 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100250 struct value *valuep)
251{
252 /* Arrays decay into pointers per C standard. We check for
253 * this here, because here we also capture arrays that come
254 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100255 if (info->type == ARGTYPE_ARRAY) {
256 struct arg_type_info *tmp = malloc(sizeof(*tmp));
257 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100258 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100259 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100260 info = tmp;
261 own = 1;
262 }
263 }
264
265 struct value value;
266 value_init(&value, proc, NULL, info, own);
267 if (fetch_arg_next(context, type, proc, info, &value) < 0)
268 return -1;
269
270 if (val_dict_push_next(arguments, &value) < 0) {
271 value_destroy(&value);
272 return -1;
273 }
274
275 if (valuep != NULL)
276 *valuep = value;
277
278 return 0;
279}
280
281static void
282fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
283{
284 if (*params_leftp == -1)
285 *params_leftp = val_dict_count(arguments);
286}
287
288static int
Petr Machata865303f2012-01-06 18:40:38 +0100289fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context,
290 struct value_dict *arguments, struct param *param,
291 ssize_t *params_leftp)
292{
293 struct param_enum *e = param_pack_init(param, arguments);
294 if (e == NULL)
295 return -1;
296
297 int ret = 0;
298 while (1) {
299 int insert_stop = 0;
300 struct arg_type_info *info = malloc(sizeof(*info));
301 if (info == NULL
302 || param_pack_next(param, e, info, &insert_stop) < 0) {
303 fail:
304 free(info);
305 ret = -1;
306 break;
307 }
308
309 if (insert_stop)
310 fetch_param_stop(arguments, params_leftp);
311
Petr Machatabc58f2a2012-10-30 00:25:50 +0100312 if (info->type == ARGTYPE_VOID) {
313 type_destroy(info);
314 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100315 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100316 }
Petr Machata865303f2012-01-06 18:40:38 +0100317
318 struct value val;
319 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100320 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100321 goto fail;
322
323 int stop = 0;
324 switch (param_pack_stop(param, e, &val)) {
325 case PPCB_ERR:
326 goto fail;
327 case PPCB_STOP:
328 stop = 1;
329 case PPCB_CONT:
330 break;
331 }
332
333 if (stop)
334 break;
335 }
336
337 param_pack_done(param, e);
338 return ret;
339}
340
341static int
Petr Machataf6ec08a2012-01-06 16:58:54 +0100342fetch_one_param(enum tof type, Process *proc, struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100343 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100344 ssize_t *params_leftp)
345{
Petr Machata865303f2012-01-06 18:40:38 +0100346 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200347 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100348 case PARAM_FLAVOR_TYPE:
349 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100350 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100351
352 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200353 if (fetch_param_pack_start(context,
354 param->u.pack.ppflavor) < 0)
355 return -1;
356 rc = fetch_param_pack(type, proc, context, arguments,
357 param, params_leftp);
358 fetch_param_pack_end(context);
359 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100360
361 case PARAM_FLAVOR_STOP:
362 fetch_param_stop(arguments, params_leftp);
363 return 0;
364 }
365
366 assert(!"Invalid param flavor!");
367 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100368}
369
370static int
371fetch_params(enum tof type, Process *proc, struct fetch_context *context,
372 struct value_dict *arguments, Function *func, ssize_t *params_leftp)
373{
374 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100375 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100376 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100377 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100378 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100379
380 /* Implicit stop at the end of parameter list. */
381 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100382
Petr Machataf6ec08a2012-01-06 16:58:54 +0100383 return 0;
384}
385
386static int
387output_one(struct value *val, struct value_dict *arguments)
388{
389 int o = format_argument(options.output, val, arguments);
Petr Machataadec2012012-11-01 22:05:04 +0100390 if (account_output(&current_column, o) < 0) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100391 if (output_error() < 0)
392 return -1;
393 o = 1;
394 }
395 return o;
396}
397
398static int
399output_params(struct value_dict *arguments, size_t start, size_t end,
400 int *need_delimp)
401{
402 size_t i;
403 int need_delim = *need_delimp;
404 for (i = start; i < end; ++i) {
405 if (need_delim
Petr Machataadec2012012-11-01 22:05:04 +0100406 && account_output(&current_column,
407 fprintf(options.output, ", ")) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100408 return -1;
409 struct value *value = val_dict_get_num(arguments, i);
410 if (value == NULL)
411 return -1;
412 need_delim = output_one(value, arguments);
413 if (need_delim < 0)
414 return -1;
415 }
416 *need_delimp = need_delim;
417 return 0;
418}
419
Juan Cespedesf1350522008-12-16 18:19:58 +0100420void
Petr Machata29add4f2012-02-18 16:38:05 +0100421output_left(enum tof type, struct Process *proc,
422 struct library_symbol *libsym)
423{
424 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200425 Function *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100426
Juan Cespedesda9b9532009-04-07 15:33:50 +0200427 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100428 return;
429 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200430 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100431 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100432 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100433 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200434 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100435 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100436 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200437 if (!options.hide_caller && libsym->lib != NULL
438 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machatab89c2562012-04-03 17:00:28 +0200439 current_column += fprintf(options.output, "%s->",
440 libsym->lib->soname);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100441
442 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100443#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100444 if (options.demangle)
445 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200446#endif
Petr Machataadec2012012-11-01 22:05:04 +0100447 if (account_output(&current_column,
448 fprintf(options.output, "%s(", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100449 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100450
451 func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100452 if (func == NULL)
453 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100454
Petr Machataf6ec08a2012-01-06 16:58:54 +0100455 struct fetch_context *context = fetch_arg_init(type, proc,
456 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100457 struct value_dict *arguments = malloc(sizeof(*arguments));
458 if (arguments == NULL)
459 return;
460 val_dict_init(arguments);
461
Petr Machataf6ec08a2012-01-06 16:58:54 +0100462 ssize_t params_left = -1;
463 int need_delim = 0;
464 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
465 || output_params(arguments, 0, params_left, &need_delim) < 0) {
466 val_dict_destroy(arguments);
467 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100468 arguments = NULL;
469 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100470 }
Petr Machata94078ec2012-01-05 18:07:02 +0100471
472 struct callstack_element *stel
473 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100474 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100475 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100476 stel->out.params_left = params_left;
477 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100478}
479
Juan Cespedesf1350522008-12-16 18:19:58 +0100480void
Petr Machata29add4f2012-02-18 16:38:05 +0100481output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100482{
Petr Machata29add4f2012-02-18 16:38:05 +0100483 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200484 Function *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100485 if (func == NULL)
486 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100487
Juan Cespedesda9b9532009-04-07 15:33:50 +0200488 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100489 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100490 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100491 dict_opt_c =
492 dict_init(dict_key2hash_string,
493 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100494 }
495 st = dict_find_entry(dict_opt_c, function_name);
496 if (!st) {
497 char *na;
498 st = malloc(sizeof(struct opt_c_struct));
499 na = strdup(function_name);
500 if (!st || !na) {
501 perror("malloc()");
502 exit(1);
503 }
504 st->count = 0;
505 st->tv.tv_sec = st->tv.tv_usec = 0;
506 dict_enter(dict_opt_c, na, st);
507 }
508 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
509 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
510 st->tv.tv_sec++;
511 } else {
512 st->tv.tv_usec += current_time_spent.tv_usec;
513 }
514 st->count++;
515 st->tv.tv_sec += current_time_spent.tv_sec;
516
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100517// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100518// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100519 return;
520 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200521 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100522 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100523 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200524 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200525 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200526 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100527 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100528#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100529 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100530 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100531 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200532#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100533 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100534 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200535#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100536 }
537
Petr Machata94078ec2012-01-05 18:07:02 +0100538 struct callstack_element *stel
539 = &proc->callstack[proc->callstack_depth - 1];
540
Petr Machataf6ec08a2012-01-06 16:58:54 +0100541 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100542
Petr Machataf6ec08a2012-01-06 16:58:54 +0100543 /* Fetch & enter into dictionary the retval first, so that
544 * other values can use it in expressions. */
545 struct value retval;
546 int own_retval = 0;
547 if (context != NULL) {
548 value_init(&retval, proc, NULL, func->return_info, 0);
549 own_retval = 1;
550 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200551 &retval) < 0)
552 value_set_type(&retval, NULL, 0);
553 else if (stel->arguments != NULL
554 && val_dict_push_named(stel->arguments, &retval,
555 "retval", 0) == 0)
556 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200557 }
Petr Machata94078ec2012-01-05 18:07:02 +0100558
Petr Machataf6ec08a2012-01-06 16:58:54 +0100559 if (stel->arguments != NULL)
560 output_params(stel->arguments, stel->out.params_left,
561 val_dict_count(stel->arguments),
562 &stel->out.need_delim);
563
564 current_column += fprintf(options.output, ") ");
565 tabto(options.align - 1);
566 fprintf(options.output, "= ");
567
Petr Machatae75fc3b2012-05-29 01:16:42 +0200568 if (context != NULL && retval.type != NULL)
569 output_one(&retval, stel->arguments);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100570
571 if (own_retval)
572 value_destroy(&retval);
573
Juan Cespedesd65efa32003-02-03 00:22:30 +0100574 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100575 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100576 current_time_spent.tv_sec,
577 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100578 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100579 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700580
581#if defined(HAVE_LIBUNWIND)
582 if (options.bt_depth > 0) {
583 unw_cursor_t cursor;
584 unw_word_t ip, sp;
585 int unwind_depth = options.bt_depth;
586 char fn_name[100];
587
588 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
589 while (unwind_depth) {
590 unw_get_reg(&cursor, UNW_REG_IP, &ip);
591 unw_get_reg(&cursor, UNW_REG_SP, &sp);
592 unw_get_proc_name(&cursor, fn_name, 100, NULL);
593 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
594 if (unw_step(&cursor) <= 0)
595 break;
596 unwind_depth--;
597 }
598 fprintf(options.output, "\n");
599 }
600#endif /* defined(HAVE_LIBUNWIND) */
601
Paul Gilliam76c61f12006-06-14 06:55:21 +0200602 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100603 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100604}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100605
Petr Machataadec2012012-11-01 22:05:04 +0100606int
Petr Machata9c400982012-11-01 22:15:01 +0100607delim_output(FILE *stream, int *need_delimp,
608 int (*writer)(FILE *stream, void *data),
609 void *data)
610{
611 int o;
612
613 /* If we don't need a delimiter, then we don't need to go
614 * through a temporary stream. It's all the same whether
615 * WRITER emits anything or not. */
616 if (!*need_delimp) {
617 o = writer(stream, data);
618
619 } else {
620 char *buf;
621 size_t bufsz;
622 FILE *tmp = open_memstream(&buf, &bufsz);
623 o = writer(tmp, data);
624 fclose(tmp);
625
626 if (o > 0 && ((*need_delimp
627 && account_output(&o, fprintf(stream, ", ")) < 0)
628 || fwrite(buf, 1, bufsz, stream) != bufsz))
629 o = -1;
630
631 free(buf);
632 }
633
634 if (o < 0)
635 return -1;
636
637 *need_delimp = *need_delimp || o > 0;
638 return o;
639}
640
641int
Petr Machataadec2012012-11-01 22:05:04 +0100642account_output(int *countp, int c)
643{
644 if (c > 0)
645 *countp += c;
646 return c;
647}
648
Petr Machatad71cf2d2012-01-05 18:28:31 +0100649static void
650do_report(const char *filename, unsigned line_no, const char *severity,
651 const char *fmt, va_list args)
652{
653 char buf[128];
654 vsnprintf(buf, sizeof(buf), fmt, args);
655 buf[sizeof(buf) - 1] = 0;
656 if (filename != NULL)
657 output_line(0, "%s:%d: %s: %s",
658 filename, line_no, severity, buf);
659 else
660 output_line(0, "%s: %s", severity, buf);
661}
662
663void
Petr Machata89c5ca22012-10-27 00:49:38 +0200664report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100665{
666 va_list args;
667 va_start(args, fmt);
668 do_report(filename, line_no, "error", fmt, args);
669 va_end(args);
670}
671
672void
Petr Machata89c5ca22012-10-27 00:49:38 +0200673report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100674{
675 va_list args;
676 va_start(args, fmt);
677 do_report(filename, line_no, "warning", fmt, args);
678 va_end(args);
679}
680
681void
Petr Machata89c5ca22012-10-27 00:49:38 +0200682report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100683{
684 va_list args;
685 va_start(args, fmt);
686 do_report(NULL, 0, "error", fmt, args);
687 va_end(args);
688}