blob: bde93c75175550980c658da9370964ded3edb5a6 [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
2 * This file is part of ltrace.
Petr Machata98ff3092013-03-08 22:11:36 +01003 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata94078ec2012-01-05 18:07:02 +01004 * 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 Machata3ac8db62012-11-23 18:43:10 +010038#include "demangle.h"
Petr Machata9bc290b2012-11-23 19:08:22 +010039#include "fetch.h"
40#include "lens_default.h"
Petr Machata29add4f2012-02-18 16:38:05 +010041#include "library.h"
Petr Machata9bc290b2012-11-23 19:08:22 +010042#include "memstream.h"
Petr Machata8bf82d02012-11-23 18:44:31 +010043#include "options.h"
Petr Machata9bc290b2012-11-23 19:08:22 +010044#include "param.h"
45#include "proc.h"
Petr Machata000e3112012-01-03 17:03:39 +010046#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010047#include "value.h"
48#include "value_dict.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020049
Juan Cespedesf7281232009-06-25 16:11:21 +020050/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010051extern struct timeval current_time_spent;
52
Petr Machatad7e4ca82012-11-28 03:38:47 +010053struct dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010054
Petr Machata929bd572012-12-17 03:20:34 +010055static struct process *current_proc = 0;
Petr Machataba1664b2012-04-28 14:59:05 +020056static size_t current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010057static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020058
Juan Cespedesf1350522008-12-16 18:19:58 +010059static void
Petr Machata929bd572012-12-17 03:20:34 +010060output_indent(struct process *proc)
Petr Machata54004752012-05-03 18:36:48 +020061{
62 int d = options.indent * (proc->callstack_depth - 1);
63 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020064}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020065
Juan Cespedesf1350522008-12-16 18:19:58 +010066static void
Petr Machata929bd572012-12-17 03:20:34 +010067begin_of_line(struct process *proc, int is_func, int indent)
Petr Machata37b73c02012-01-06 16:00:25 +010068{
Juan Cespedes5e01f651998-03-08 22:31:44 +010069 current_column = 0;
70 if (!proc) {
71 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020072 }
Juan Cespedesc693f022009-05-21 18:59:41 +020073 if ((options.output != stderr) && (opt_p || options.follow)) {
74 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020075 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010076 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010077 }
Juan Cespedesf666d191998-09-20 23:04:34 +020078 if (opt_r) {
79 struct timeval tv;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020081 struct timeval diff;
82
Petr Machata75642272012-11-23 19:02:13 +010083 gettimeofday(&tv, NULL);
Juan Cespedesf666d191998-09-20 23:04:34 +020084
Ian Wienand2d45b1a2006-02-20 22:48:07 +010085 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
86 old_tv.tv_sec = tv.tv_sec;
87 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020088 }
89 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
90 if (tv.tv_usec >= old_tv.tv_usec) {
91 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
92 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020093 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020094 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
95 }
96 old_tv.tv_sec = tv.tv_sec;
97 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010098 current_column += fprintf(options.output, "%3lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +010099 (unsigned long)diff.tv_sec,
100 (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +0200101 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200102 if (opt_t) {
103 struct timeval tv;
Petr Machata75642272012-11-23 19:02:13 +0100104 gettimeofday(&tv, NULL);
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 ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100107 (unsigned long)tv.tv_sec,
108 (int)tv.tv_usec);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 } else if (opt_t > 1) {
110 struct tm *tmp = localtime(&tv.tv_sec);
111 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100112 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
114 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200115 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100116 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100117 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100118 tmp->tm_hour, tmp->tm_min,
119 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200120 }
121 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100122 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100123 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100124 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100125 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100126 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100127 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100129 }
Petr Machata37b73c02012-01-06 16:00:25 +0100130 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200131 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200132 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200133}
134
Petr Machatab7819162012-01-09 04:26:15 +0100135static struct arg_type_info *
136get_unknown_type(void)
137{
138 static struct arg_type_info *info = NULL;
139 if (info == NULL) {
140 info = malloc(sizeof(*info));
141 if (info == NULL) {
142 report_global_error("malloc: %s", strerror(errno));
143 abort();
144 }
145 *info = *type_get_simple(ARGTYPE_LONG);
146 info->lens = &guess_lens;
147 }
148 return info;
149}
150
Petr Machataf6ec08a2012-01-06 16:58:54 +0100151/* The default prototype is: long X(long, long, long, long). */
Petr Machata3d083b62012-11-22 15:14:45 +0100152static struct prototype *
Petr Machataf6ec08a2012-01-06 16:58:54 +0100153build_default_prototype(void)
154{
Petr Machata3d083b62012-11-22 15:14:45 +0100155 struct prototype *ret = malloc(sizeof(*ret));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100156 size_t i = 0;
157 if (ret == NULL)
158 goto err;
159 memset(ret, 0, sizeof(*ret));
160
Petr Machatab7819162012-01-09 04:26:15 +0100161 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100162
163 ret->return_info = unknown_type;
Petr Machata3a9bf6d2012-01-06 21:44:10 +0100164 ret->own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100165
166 ret->num_params = 4;
Petr Machata865303f2012-01-06 18:40:38 +0100167 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
168 if (ret->params == NULL)
169 goto err;
170
171 for (i = 0; i < ret->num_params; ++i)
172 param_init_type(&ret->params[i], unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100173
174 return ret;
175
176err:
177 report_global_error("malloc: %s", strerror(errno));
Petr Machata865303f2012-01-06 18:40:38 +0100178 if (ret->params != NULL) {
179 while (i-- > 0)
180 param_destroy(&ret->params[i]);
181 free(ret->params);
182 }
183
Petr Machataf6ec08a2012-01-06 16:58:54 +0100184 free(ret);
185
186 return NULL;
187}
188
Petr Machata3d083b62012-11-22 15:14:45 +0100189static struct prototype *
190name2func(char const *name)
191{
192 struct prototype *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100194
Petr Machataf6ec08a2012-01-06 16:58:54 +0100195 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200196 str1 = tmp->name;
197 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100198 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100201
Petr Machata3d083b62012-11-22 15:14:45 +0100202 static struct prototype *def = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100203 if (def == NULL)
204 def = build_default_prototype();
205
206 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100207}
208
Juan Cespedesf1350522008-12-16 18:19:58 +0100209void
Petr Machata929bd572012-12-17 03:20:34 +0100210output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200211{
212 if (options.summary)
213 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200214
Petr Machataefc94362012-10-27 00:49:05 +0200215 if (current_proc != NULL) {
216 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100217 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200218 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100219 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100220 }
Petr Machataefc94362012-10-27 00:49:05 +0200221 current_proc = NULL;
222 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200223 return;
Petr Machataefc94362012-10-27 00:49:05 +0200224
Petr Machata37b73c02012-01-06 16:00:25 +0100225 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100226
Petr Machataefc94362012-10-27 00:49:05 +0200227 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200228 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100229 vfprintf(options.output, fmt, args);
230 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200231 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200232
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100234}
235
Juan Cespedesf1350522008-12-16 18:19:58 +0100236static void
237tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100238 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100239 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100240 }
241}
242
Petr Machataf6ec08a2012-01-06 16:58:54 +0100243static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100244output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100245{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100246 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100247}
248
249static int
Petr Machata929bd572012-12-17 03:20:34 +0100250fetch_simple_param(enum tof type, struct process *proc,
251 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 Machata929bd572012-12-17 03:20:34 +0100293fetch_param_pack(enum tof type, struct process *proc,
294 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100295 struct value_dict *arguments, struct param *param,
296 ssize_t *params_leftp)
297{
298 struct param_enum *e = param_pack_init(param, arguments);
299 if (e == NULL)
300 return -1;
301
302 int ret = 0;
303 while (1) {
304 int insert_stop = 0;
305 struct arg_type_info *info = malloc(sizeof(*info));
306 if (info == NULL
307 || param_pack_next(param, e, info, &insert_stop) < 0) {
308 fail:
309 free(info);
310 ret = -1;
311 break;
312 }
313
314 if (insert_stop)
315 fetch_param_stop(arguments, params_leftp);
316
Petr Machatabc58f2a2012-10-30 00:25:50 +0100317 if (info->type == ARGTYPE_VOID) {
318 type_destroy(info);
319 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100320 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100321 }
Petr Machata865303f2012-01-06 18:40:38 +0100322
323 struct value val;
324 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100325 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100326 goto fail;
327
328 int stop = 0;
329 switch (param_pack_stop(param, e, &val)) {
330 case PPCB_ERR:
331 goto fail;
332 case PPCB_STOP:
333 stop = 1;
334 case PPCB_CONT:
335 break;
336 }
337
338 if (stop)
339 break;
340 }
341
342 param_pack_done(param, e);
343 return ret;
344}
345
346static int
Petr Machata929bd572012-12-17 03:20:34 +0100347fetch_one_param(enum tof type, struct process *proc,
348 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100349 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100350 ssize_t *params_leftp)
351{
Petr Machata865303f2012-01-06 18:40:38 +0100352 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200353 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100354 case PARAM_FLAVOR_TYPE:
355 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100356 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100357
358 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200359 if (fetch_param_pack_start(context,
360 param->u.pack.ppflavor) < 0)
361 return -1;
362 rc = fetch_param_pack(type, proc, context, arguments,
363 param, params_leftp);
364 fetch_param_pack_end(context);
365 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100366
367 case PARAM_FLAVOR_STOP:
368 fetch_param_stop(arguments, params_leftp);
369 return 0;
370 }
371
372 assert(!"Invalid param flavor!");
373 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100374}
375
376static int
Petr Machata929bd572012-12-17 03:20:34 +0100377fetch_params(enum tof type, struct process *proc,
378 struct fetch_context *context,
Petr Machata3d083b62012-11-22 15:14:45 +0100379 struct value_dict *arguments, struct prototype *func,
380 ssize_t *params_leftp)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100381{
382 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100383 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100384 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100385 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100386 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100387
388 /* Implicit stop at the end of parameter list. */
389 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100390
Petr Machataf6ec08a2012-01-06 16:58:54 +0100391 return 0;
392}
393
Petr Machataf7c46bb2012-11-01 22:15:45 +0100394struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100395{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100396 struct value *value;
397 struct value_dict *arguments;
398};
399
400static int
401format_argument_cb(FILE *stream, void *ptr)
402{
403 struct format_argument_data *data = ptr;
404 int o = format_argument(stream, data->value, data->arguments);
405 if (o < 0)
406 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100407 return o;
408}
409
410static int
411output_params(struct value_dict *arguments, size_t start, size_t end,
412 int *need_delimp)
413{
414 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100415 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100416 struct value *value = val_dict_get_num(arguments, i);
417 if (value == NULL)
418 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100419
420 struct format_argument_data data = { value, arguments };
421 int o = delim_output(options.output, need_delimp,
422 format_argument_cb, &data);
423 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100424 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100425 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100426 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 return 0;
428}
429
Juan Cespedesf1350522008-12-16 18:19:58 +0100430void
Petr Machata929bd572012-12-17 03:20:34 +0100431output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100432 struct library_symbol *libsym)
433{
434 const char *function_name = libsym->name;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100435
Juan Cespedesda9b9532009-04-07 15:33:50 +0200436 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100437 return;
438 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200439 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100440 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100441 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100442 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200443 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100444 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100445 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200446 if (!options.hide_caller && libsym->lib != NULL
447 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100448 /* We don't terribly mind failing this. */
449 account_output(&current_column,
450 fprintf(options.output, "%s->",
451 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100452
453 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100454#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100455 if (options.demangle)
456 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200457#endif
Petr Machataadec2012012-11-01 22:05:04 +0100458 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100459 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100460 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100461
Petr Machata09654202012-12-05 17:07:21 +0100462 if (libsym->lib != NULL
463 && libsym->lib->type != LT_LIBTYPE_MAIN
464 && libsym->plt_type == LS_TOPLT_NONE
465 && account_output(&current_column,
466 fprintf(options.output, "@%s",
467 libsym->lib->soname)) < 0)
468 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100469 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100470
Petr Machata09654202012-12-05 17:07:21 +0100471 account_output(&current_column, fprintf(options.output, "("));
472
Petr Machata9bc290b2012-11-23 19:08:22 +0100473 struct prototype *func = name2func(function_name);
Petr Machata09654202012-12-05 17:07:21 +0100474 if (func == NULL) {
475 account_output(&current_column, fprintf(options.output, "???"));
476 return;
477 }
478
Petr Machataf6ec08a2012-01-06 16:58:54 +0100479 struct fetch_context *context = fetch_arg_init(type, proc,
480 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100481 struct value_dict *arguments = malloc(sizeof(*arguments));
482 if (arguments == NULL)
483 return;
484 val_dict_init(arguments);
485
Petr Machataf6ec08a2012-01-06 16:58:54 +0100486 ssize_t params_left = -1;
487 int need_delim = 0;
488 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
489 || output_params(arguments, 0, params_left, &need_delim) < 0) {
490 val_dict_destroy(arguments);
491 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100492 arguments = NULL;
493 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100494 }
Petr Machata94078ec2012-01-05 18:07:02 +0100495
496 struct callstack_element *stel
497 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100498 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100499 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100500 stel->out.params_left = params_left;
501 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100502}
503
Petr Machatad7e4ca82012-11-28 03:38:47 +0100504static void
505free_stringp_cb(const char **stringp, void *data)
506{
507 free((char *)*stringp);
508}
509
Juan Cespedesf1350522008-12-16 18:19:58 +0100510void
Petr Machata929bd572012-12-17 03:20:34 +0100511output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100512{
Petr Machata29add4f2012-02-18 16:38:05 +0100513 const char *function_name = libsym->name;
Petr Machata3d083b62012-11-22 15:14:45 +0100514 struct prototype *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100515 if (func == NULL)
516 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100517
Petr Machatad7e4ca82012-11-28 03:38:47 +0100518again:
Juan Cespedesda9b9532009-04-07 15:33:50 +0200519 if (options.summary) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100520 if (dict_opt_c == NULL) {
521 dict_opt_c = malloc(sizeof(*dict_opt_c));
522 if (dict_opt_c == NULL) {
523 oom:
524 fprintf(stderr,
525 "Can't allocate memory for "
526 "keeping track of -c.\n");
527 free(dict_opt_c);
528 options.summary = 0;
529 goto again;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100530 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100531 DICT_INIT(dict_opt_c, const char *, struct opt_c_struct,
532 dict_hash_string, dict_eq_string, NULL);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100533 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100534
Petr Machata98ff3092013-03-08 22:11:36 +0100535 struct opt_c_struct *st
536 = DICT_FIND_REF(dict_opt_c, &function_name,
537 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100538 if (st == NULL) {
539 const char *na = strdup(function_name);
540 struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
541 if (na == NULL
542 || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
543 free((char *)na);
544 DICT_DESTROY(dict_opt_c, const char *,
545 struct opt_c_struct,
546 free_stringp_cb, NULL, NULL);
547 goto oom;
548 }
Petr Machata98ff3092013-03-08 22:11:36 +0100549 st = DICT_FIND_REF(dict_opt_c, &function_name,
550 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100551 assert(st != NULL);
552 }
553
Juan Cespedesd65efa32003-02-03 00:22:30 +0100554 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
555 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
556 st->tv.tv_sec++;
557 } else {
558 st->tv.tv_usec += current_time_spent.tv_usec;
559 }
560 st->count++;
561 st->tv.tv_sec += current_time_spent.tv_sec;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100562 return;
563 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100564
Paul Gilliam76c61f12006-06-14 06:55:21 +0200565 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100566 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100567 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200568 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200569 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200570 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100571 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100572#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100573 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100574 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100575 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200576#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100577 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100578 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200579#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100580 }
581
Petr Machata94078ec2012-01-05 18:07:02 +0100582 struct callstack_element *stel
583 = &proc->callstack[proc->callstack_depth - 1];
584
Petr Machataf6ec08a2012-01-06 16:58:54 +0100585 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100586
Petr Machataf6ec08a2012-01-06 16:58:54 +0100587 /* Fetch & enter into dictionary the retval first, so that
588 * other values can use it in expressions. */
589 struct value retval;
590 int own_retval = 0;
591 if (context != NULL) {
592 value_init(&retval, proc, NULL, func->return_info, 0);
593 own_retval = 1;
594 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200595 &retval) < 0)
596 value_set_type(&retval, NULL, 0);
597 else if (stel->arguments != NULL
598 && val_dict_push_named(stel->arguments, &retval,
599 "retval", 0) == 0)
600 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200601 }
Petr Machata94078ec2012-01-05 18:07:02 +0100602
Petr Machataf6ec08a2012-01-06 16:58:54 +0100603 if (stel->arguments != NULL)
604 output_params(stel->arguments, stel->out.params_left,
605 val_dict_count(stel->arguments),
606 &stel->out.need_delim);
607
608 current_column += fprintf(options.output, ") ");
609 tabto(options.align - 1);
610 fprintf(options.output, "= ");
611
Petr Machataf7c46bb2012-11-01 22:15:45 +0100612 if (context != NULL && retval.type != NULL) {
613 struct format_argument_data data = { &retval, stel->arguments };
614 format_argument_cb(options.output, &data);
615 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100616
617 if (own_retval)
618 value_destroy(&retval);
619
Juan Cespedesd65efa32003-02-03 00:22:30 +0100620 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100621 fprintf(options.output, " <%lu.%06d>",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100622 (unsigned long)current_time_spent.tv_sec,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100623 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100624 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100625 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700626
627#if defined(HAVE_LIBUNWIND)
628 if (options.bt_depth > 0) {
629 unw_cursor_t cursor;
630 unw_word_t ip, sp;
631 int unwind_depth = options.bt_depth;
632 char fn_name[100];
633
634 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
635 while (unwind_depth) {
636 unw_get_reg(&cursor, UNW_REG_IP, &ip);
637 unw_get_reg(&cursor, UNW_REG_SP, &sp);
638 unw_get_proc_name(&cursor, fn_name, 100, NULL);
639 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
640 if (unw_step(&cursor) <= 0)
641 break;
642 unwind_depth--;
643 }
644 fprintf(options.output, "\n");
645 }
646#endif /* defined(HAVE_LIBUNWIND) */
647
Paul Gilliam76c61f12006-06-14 06:55:21 +0200648 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100649 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100650}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100651
Petr Machataadec2012012-11-01 22:05:04 +0100652int
Petr Machata9c400982012-11-01 22:15:01 +0100653delim_output(FILE *stream, int *need_delimp,
654 int (*writer)(FILE *stream, void *data),
655 void *data)
656{
657 int o;
658
659 /* If we don't need a delimiter, then we don't need to go
660 * through a temporary stream. It's all the same whether
661 * WRITER emits anything or not. */
662 if (!*need_delimp) {
663 o = writer(stream, data);
664
665 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100666 struct memstream ms;
667 if (memstream_init(&ms) < 0)
668 return -1;
669 o = writer(ms.stream, data);
670 if (memstream_close(&ms) < 0)
671 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100672 if (o > 0 && ((*need_delimp
673 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100674 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100675 o = -1;
676
Petr Machata2ae374a2012-12-08 05:08:48 +0100677 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100678 }
679
680 if (o < 0)
681 return -1;
682
683 *need_delimp = *need_delimp || o > 0;
684 return o;
685}
686
687int
Petr Machataadec2012012-11-01 22:05:04 +0100688account_output(int *countp, int c)
689{
690 if (c > 0)
691 *countp += c;
692 return c;
693}
694
Petr Machatad71cf2d2012-01-05 18:28:31 +0100695static void
696do_report(const char *filename, unsigned line_no, const char *severity,
697 const char *fmt, va_list args)
698{
699 char buf[128];
700 vsnprintf(buf, sizeof(buf), fmt, args);
701 buf[sizeof(buf) - 1] = 0;
702 if (filename != NULL)
703 output_line(0, "%s:%d: %s: %s",
704 filename, line_no, severity, buf);
705 else
706 output_line(0, "%s: %s", severity, buf);
707}
708
709void
Petr Machata89c5ca22012-10-27 00:49:38 +0200710report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100711{
712 va_list args;
713 va_start(args, fmt);
714 do_report(filename, line_no, "error", fmt, args);
715 va_end(args);
716}
717
718void
Petr Machata89c5ca22012-10-27 00:49:38 +0200719report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100720{
721 va_list args;
722 va_start(args, fmt);
723 do_report(filename, line_no, "warning", fmt, args);
724 va_end(args);
725}
726
727void
Petr Machata89c5ca22012-10-27 00:49:38 +0200728report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100729{
730 va_list args;
731 va_start(args, fmt);
732 do_report(NULL, 0, "error", fmt, args);
733 va_end(args);
734}