blob: 1d01d315a872456c612b483d207179515b10d8a6 [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
Petr Machatada0cdf22012-11-02 18:04:24 +010025/* glibc before 2.10, eglibc and uClibc all need _GNU_SOURCE defined
26 * for open_memstream to become visible. */
27#define _GNU_SOURCE
28
Juan Cespedesac3db291998-04-25 14:31:58 +020029#include "config.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020030
Juan Cespedes3268a161997-08-25 16:45:22 +020031#include <stdio.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +010032#include <stdlib.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020033#include <stdarg.h>
Juan Cespedes1cd999a2001-07-03 00:46:04 +020034#include <string.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020035#include <time.h>
36#include <sys/time.h>
37#include <unistd.h>
Petr Machataf6ec08a2012-01-06 16:58:54 +010038#include <errno.h>
Petr Machata865303f2012-01-06 18:40:38 +010039#include <assert.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020040
Juan Cespedesf7281232009-06-25 16:11:21 +020041#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010042#include "proc.h"
Petr Machata29add4f2012-02-18 16:38:05 +010043#include "library.h"
Petr Machata000e3112012-01-03 17:03:39 +010044#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010045#include "value.h"
46#include "value_dict.h"
Petr Machata865303f2012-01-06 18:40:38 +010047#include "param.h"
Petr Machataf6ec08a2012-01-06 16:58:54 +010048#include "fetch.h"
Petr Machatab7819162012-01-09 04:26:15 +010049#include "lens_default.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020050
Juan Cespedesf7281232009-06-25 16:11:21 +020051/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010052extern struct timeval current_time_spent;
53
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020054Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010055
Juan Cespedesa8909f72009-04-28 20:02:41 +020056static Process *current_proc = 0;
Petr Machataba1664b2012-04-28 14:59:05 +020057static size_t current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010058static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020059
Juan Cespedesf1350522008-12-16 18:19:58 +010060static void
Petr Machata54004752012-05-03 18:36:48 +020061output_indent(struct Process *proc)
62{
63 int d = options.indent * (proc->callstack_depth - 1);
64 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020065}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020066
Juan Cespedesf1350522008-12-16 18:19:58 +010067static void
Petr Machata37b73c02012-01-06 16:00:25 +010068begin_of_line(Process *proc, int is_func, int indent)
69{
Juan Cespedes5e01f651998-03-08 22:31:44 +010070 current_column = 0;
71 if (!proc) {
72 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020073 }
Juan Cespedesc693f022009-05-21 18:59:41 +020074 if ((options.output != stderr) && (opt_p || options.follow)) {
75 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020076 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010077 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010078 }
Juan Cespedesf666d191998-09-20 23:04:34 +020079 if (opt_r) {
80 struct timeval tv;
81 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020083 struct timeval diff;
84
85 gettimeofday(&tv, &tz);
86
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
88 old_tv.tv_sec = tv.tv_sec;
89 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020090 }
91 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
92 if (tv.tv_usec >= old_tv.tv_usec) {
93 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
94 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020095 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020096 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
97 }
98 old_tv.tv_sec = tv.tv_sec;
99 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100100 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +0200102 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200103 if (opt_t) {
104 struct timeval tv;
105 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200106
107 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100109 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 tv.tv_sec, (int)tv.tv_usec);
111 } else if (opt_t > 1) {
112 struct tm *tmp = localtime(&tv.tv_sec);
113 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100114 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
116 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200117 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100118 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100119 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 tmp->tm_hour, tmp->tm_min,
121 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200122 }
123 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100124 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100125 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100126 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100128 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100129 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100130 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100131 }
Petr Machata37b73c02012-01-06 16:00:25 +0100132 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200133 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200134 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200135}
136
Petr Machatab7819162012-01-09 04:26:15 +0100137static struct arg_type_info *
138get_unknown_type(void)
139{
140 static struct arg_type_info *info = NULL;
141 if (info == NULL) {
142 info = malloc(sizeof(*info));
143 if (info == NULL) {
144 report_global_error("malloc: %s", strerror(errno));
145 abort();
146 }
147 *info = *type_get_simple(ARGTYPE_LONG);
148 info->lens = &guess_lens;
149 }
150 return info;
151}
152
Petr Machataf6ec08a2012-01-06 16:58:54 +0100153/* The default prototype is: long X(long, long, long, long). */
154static Function *
155build_default_prototype(void)
156{
157 Function *ret = malloc(sizeof(*ret));
158 size_t i = 0;
159 if (ret == NULL)
160 goto err;
161 memset(ret, 0, sizeof(*ret));
162
Petr Machatab7819162012-01-09 04:26:15 +0100163 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100164
165 ret->return_info = unknown_type;
Petr Machata3a9bf6d2012-01-06 21:44:10 +0100166 ret->own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100167
168 ret->num_params = 4;
Petr Machata865303f2012-01-06 18:40:38 +0100169 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
170 if (ret->params == NULL)
171 goto err;
172
173 for (i = 0; i < ret->num_params; ++i)
174 param_init_type(&ret->params[i], unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100175
176 return ret;
177
178err:
179 report_global_error("malloc: %s", strerror(errno));
Petr Machata865303f2012-01-06 18:40:38 +0100180 if (ret->params != NULL) {
181 while (i-- > 0)
182 param_destroy(&ret->params[i]);
183 free(ret->params);
184 }
185
Petr Machataf6ec08a2012-01-06 16:58:54 +0100186 free(ret);
187
188 return NULL;
189}
190
Juan Cespedescde58262009-05-07 11:09:00 +0200191static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +0200192name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200193 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100194 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195
Petr Machataf6ec08a2012-01-06 16:58:54 +0100196 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200197 str1 = tmp->name;
198 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100199 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100201 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100202
203 static Function *def = NULL;
204 if (def == NULL)
205 def = build_default_prototype();
206
207 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100208}
209
Juan Cespedesf1350522008-12-16 18:19:58 +0100210void
Petr Machataefc94362012-10-27 00:49:05 +0200211output_line(struct Process *proc, const char *fmt, ...)
212{
213 if (options.summary)
214 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200215
Petr Machataefc94362012-10-27 00:49:05 +0200216 if (current_proc != NULL) {
217 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100218 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200219 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100220 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100221 }
Petr Machataefc94362012-10-27 00:49:05 +0200222 current_proc = NULL;
223 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200224 return;
Petr Machataefc94362012-10-27 00:49:05 +0200225
Petr Machata37b73c02012-01-06 16:00:25 +0100226 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100227
Petr Machataefc94362012-10-27 00:49:05 +0200228 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200229 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100230 vfprintf(options.output, fmt, args);
231 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200232 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200233
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100234 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100235}
236
Juan Cespedesf1350522008-12-16 18:19:58 +0100237static void
238tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100239 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100240 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100241 }
242}
243
Petr Machataf6ec08a2012-01-06 16:58:54 +0100244static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100245output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100246{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100247 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100248}
249
250static int
251fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100252 struct value_dict *arguments,
253 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100254 struct value *valuep)
255{
256 /* Arrays decay into pointers per C standard. We check for
257 * this here, because here we also capture arrays that come
258 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100259 if (info->type == ARGTYPE_ARRAY) {
260 struct arg_type_info *tmp = malloc(sizeof(*tmp));
261 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100262 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100263 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100264 info = tmp;
265 own = 1;
266 }
267 }
268
269 struct value value;
270 value_init(&value, proc, NULL, info, own);
271 if (fetch_arg_next(context, type, proc, info, &value) < 0)
272 return -1;
273
274 if (val_dict_push_next(arguments, &value) < 0) {
275 value_destroy(&value);
276 return -1;
277 }
278
279 if (valuep != NULL)
280 *valuep = value;
281
282 return 0;
283}
284
285static void
286fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
287{
288 if (*params_leftp == -1)
289 *params_leftp = val_dict_count(arguments);
290}
291
292static int
Petr Machata865303f2012-01-06 18:40:38 +0100293fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context,
294 struct value_dict *arguments, struct param *param,
295 ssize_t *params_leftp)
296{
297 struct param_enum *e = param_pack_init(param, arguments);
298 if (e == NULL)
299 return -1;
300
301 int ret = 0;
302 while (1) {
303 int insert_stop = 0;
304 struct arg_type_info *info = malloc(sizeof(*info));
305 if (info == NULL
306 || param_pack_next(param, e, info, &insert_stop) < 0) {
307 fail:
308 free(info);
309 ret = -1;
310 break;
311 }
312
313 if (insert_stop)
314 fetch_param_stop(arguments, params_leftp);
315
Petr Machatabc58f2a2012-10-30 00:25:50 +0100316 if (info->type == ARGTYPE_VOID) {
317 type_destroy(info);
318 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100319 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100320 }
Petr Machata865303f2012-01-06 18:40:38 +0100321
322 struct value val;
323 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100324 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100325 goto fail;
326
327 int stop = 0;
328 switch (param_pack_stop(param, e, &val)) {
329 case PPCB_ERR:
330 goto fail;
331 case PPCB_STOP:
332 stop = 1;
333 case PPCB_CONT:
334 break;
335 }
336
337 if (stop)
338 break;
339 }
340
341 param_pack_done(param, e);
342 return ret;
343}
344
345static int
Petr Machataf6ec08a2012-01-06 16:58:54 +0100346fetch_one_param(enum tof type, Process *proc, 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
375fetch_params(enum tof type, Process *proc, struct fetch_context *context,
376 struct value_dict *arguments, Function *func, ssize_t *params_leftp)
377{
378 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100379 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100380 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100381 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100382 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100383
384 /* Implicit stop at the end of parameter list. */
385 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100386
Petr Machataf6ec08a2012-01-06 16:58:54 +0100387 return 0;
388}
389
Petr Machataf7c46bb2012-11-01 22:15:45 +0100390struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100391{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100392 struct value *value;
393 struct value_dict *arguments;
394};
395
396static int
397format_argument_cb(FILE *stream, void *ptr)
398{
399 struct format_argument_data *data = ptr;
400 int o = format_argument(stream, data->value, data->arguments);
401 if (o < 0)
402 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100403 return o;
404}
405
406static int
407output_params(struct value_dict *arguments, size_t start, size_t end,
408 int *need_delimp)
409{
410 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100411 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100412 struct value *value = val_dict_get_num(arguments, i);
413 if (value == NULL)
414 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100415
416 struct format_argument_data data = { value, arguments };
417 int o = delim_output(options.output, need_delimp,
418 format_argument_cb, &data);
419 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100420 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100421 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100422 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100423 return 0;
424}
425
Juan Cespedesf1350522008-12-16 18:19:58 +0100426void
Petr Machata29add4f2012-02-18 16:38:05 +0100427output_left(enum tof type, struct Process *proc,
428 struct library_symbol *libsym)
429{
430 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200431 Function *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100432
Juan Cespedesda9b9532009-04-07 15:33:50 +0200433 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100434 return;
435 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200436 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100437 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100439 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200440 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100441 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100442 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200443 if (!options.hide_caller && libsym->lib != NULL
444 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100445 /* We don't terribly mind failing this. */
446 account_output(&current_column,
447 fprintf(options.output, "%s->",
448 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100449
450 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100451#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100452 if (options.demangle)
453 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200454#endif
Petr Machataadec2012012-11-01 22:05:04 +0100455 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100456 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100457 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100458
Petr Machata09654202012-12-05 17:07:21 +0100459 if (libsym->lib != NULL
460 && libsym->lib->type != LT_LIBTYPE_MAIN
461 && libsym->plt_type == LS_TOPLT_NONE
462 && account_output(&current_column,
463 fprintf(options.output, "@%s",
464 libsym->lib->soname)) < 0)
465 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100466 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100467
Petr Machata09654202012-12-05 17:07:21 +0100468 account_output(&current_column, fprintf(options.output, "("));
469
470 func = name2func(function_name);
471 if (func == NULL) {
472 account_output(&current_column, fprintf(options.output, "???"));
473 return;
474 }
475
Petr Machataf6ec08a2012-01-06 16:58:54 +0100476 struct fetch_context *context = fetch_arg_init(type, proc,
477 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100478 struct value_dict *arguments = malloc(sizeof(*arguments));
479 if (arguments == NULL)
480 return;
481 val_dict_init(arguments);
482
Petr Machataf6ec08a2012-01-06 16:58:54 +0100483 ssize_t params_left = -1;
484 int need_delim = 0;
485 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
486 || output_params(arguments, 0, params_left, &need_delim) < 0) {
487 val_dict_destroy(arguments);
488 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100489 arguments = NULL;
490 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100491 }
Petr Machata94078ec2012-01-05 18:07:02 +0100492
493 struct callstack_element *stel
494 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100495 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100496 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100497 stel->out.params_left = params_left;
498 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100499}
500
Juan Cespedesf1350522008-12-16 18:19:58 +0100501void
Petr Machata29add4f2012-02-18 16:38:05 +0100502output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100503{
Petr Machata29add4f2012-02-18 16:38:05 +0100504 const char *function_name = libsym->name;
Juan Cespedescde58262009-05-07 11:09:00 +0200505 Function *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100506 if (func == NULL)
507 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100508
Juan Cespedesda9b9532009-04-07 15:33:50 +0200509 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100510 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100511 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 dict_opt_c =
513 dict_init(dict_key2hash_string,
514 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100515 }
516 st = dict_find_entry(dict_opt_c, function_name);
517 if (!st) {
518 char *na;
519 st = malloc(sizeof(struct opt_c_struct));
520 na = strdup(function_name);
521 if (!st || !na) {
522 perror("malloc()");
523 exit(1);
524 }
525 st->count = 0;
526 st->tv.tv_sec = st->tv.tv_usec = 0;
527 dict_enter(dict_opt_c, na, st);
528 }
529 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
530 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
531 st->tv.tv_sec++;
532 } else {
533 st->tv.tv_usec += current_time_spent.tv_usec;
534 }
535 st->count++;
536 st->tv.tv_sec += current_time_spent.tv_sec;
537
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100538// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100539// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100540 return;
541 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200542 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100543 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100544 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200545 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200546 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200547 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100548 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100549#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100550 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100551 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100552 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200553#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100554 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100555 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200556#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100557 }
558
Petr Machata94078ec2012-01-05 18:07:02 +0100559 struct callstack_element *stel
560 = &proc->callstack[proc->callstack_depth - 1];
561
Petr Machataf6ec08a2012-01-06 16:58:54 +0100562 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100563
Petr Machataf6ec08a2012-01-06 16:58:54 +0100564 /* Fetch & enter into dictionary the retval first, so that
565 * other values can use it in expressions. */
566 struct value retval;
567 int own_retval = 0;
568 if (context != NULL) {
569 value_init(&retval, proc, NULL, func->return_info, 0);
570 own_retval = 1;
571 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200572 &retval) < 0)
573 value_set_type(&retval, NULL, 0);
574 else if (stel->arguments != NULL
575 && val_dict_push_named(stel->arguments, &retval,
576 "retval", 0) == 0)
577 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200578 }
Petr Machata94078ec2012-01-05 18:07:02 +0100579
Petr Machataf6ec08a2012-01-06 16:58:54 +0100580 if (stel->arguments != NULL)
581 output_params(stel->arguments, stel->out.params_left,
582 val_dict_count(stel->arguments),
583 &stel->out.need_delim);
584
585 current_column += fprintf(options.output, ") ");
586 tabto(options.align - 1);
587 fprintf(options.output, "= ");
588
Petr Machataf7c46bb2012-11-01 22:15:45 +0100589 if (context != NULL && retval.type != NULL) {
590 struct format_argument_data data = { &retval, stel->arguments };
591 format_argument_cb(options.output, &data);
592 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100593
594 if (own_retval)
595 value_destroy(&retval);
596
Juan Cespedesd65efa32003-02-03 00:22:30 +0100597 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100598 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100599 current_time_spent.tv_sec,
600 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100601 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100602 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700603
604#if defined(HAVE_LIBUNWIND)
605 if (options.bt_depth > 0) {
606 unw_cursor_t cursor;
607 unw_word_t ip, sp;
608 int unwind_depth = options.bt_depth;
609 char fn_name[100];
610
611 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
612 while (unwind_depth) {
613 unw_get_reg(&cursor, UNW_REG_IP, &ip);
614 unw_get_reg(&cursor, UNW_REG_SP, &sp);
615 unw_get_proc_name(&cursor, fn_name, 100, NULL);
616 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
617 if (unw_step(&cursor) <= 0)
618 break;
619 unwind_depth--;
620 }
621 fprintf(options.output, "\n");
622 }
623#endif /* defined(HAVE_LIBUNWIND) */
624
Paul Gilliam76c61f12006-06-14 06:55:21 +0200625 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100626 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100627}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100628
Petr Machataadec2012012-11-01 22:05:04 +0100629int
Petr Machata9c400982012-11-01 22:15:01 +0100630delim_output(FILE *stream, int *need_delimp,
631 int (*writer)(FILE *stream, void *data),
632 void *data)
633{
634 int o;
635
636 /* If we don't need a delimiter, then we don't need to go
637 * through a temporary stream. It's all the same whether
638 * WRITER emits anything or not. */
639 if (!*need_delimp) {
640 o = writer(stream, data);
641
642 } else {
643 char *buf;
644 size_t bufsz;
645 FILE *tmp = open_memstream(&buf, &bufsz);
646 o = writer(tmp, data);
647 fclose(tmp);
648
649 if (o > 0 && ((*need_delimp
650 && account_output(&o, fprintf(stream, ", ")) < 0)
651 || fwrite(buf, 1, bufsz, stream) != bufsz))
652 o = -1;
653
654 free(buf);
655 }
656
657 if (o < 0)
658 return -1;
659
660 *need_delimp = *need_delimp || o > 0;
661 return o;
662}
663
664int
Petr Machataadec2012012-11-01 22:05:04 +0100665account_output(int *countp, int c)
666{
667 if (c > 0)
668 *countp += c;
669 return c;
670}
671
Petr Machatad71cf2d2012-01-05 18:28:31 +0100672static void
673do_report(const char *filename, unsigned line_no, const char *severity,
674 const char *fmt, va_list args)
675{
676 char buf[128];
677 vsnprintf(buf, sizeof(buf), fmt, args);
678 buf[sizeof(buf) - 1] = 0;
679 if (filename != NULL)
680 output_line(0, "%s:%d: %s: %s",
681 filename, line_no, severity, buf);
682 else
683 output_line(0, "%s: %s", severity, buf);
684}
685
686void
Petr Machata89c5ca22012-10-27 00:49:38 +0200687report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100688{
689 va_list args;
690 va_start(args, fmt);
691 do_report(filename, line_no, "error", fmt, args);
692 va_end(args);
693}
694
695void
Petr Machata89c5ca22012-10-27 00:49:38 +0200696report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100697{
698 va_list args;
699 va_start(args, fmt);
700 do_report(filename, line_no, "warning", fmt, args);
701 va_end(args);
702}
703
704void
Petr Machata89c5ca22012-10-27 00:49:38 +0200705report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100706{
707 va_list args;
708 va_start(args, fmt);
709 do_report(NULL, 0, "error", fmt, args);
710 va_end(args);
711}