blob: 877fa931cdf56d247e1173c5ef56a0e755f7b017 [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
Petr Machataebc56a72012-12-17 04:37:57 +010037#include "output.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 Machataebc56a72012-12-17 04:37:57 +010046#include "prototype.h"
Petr Machata000e3112012-01-03 17:03:39 +010047#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010048#include "value.h"
49#include "value_dict.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
Petr Machatad7e4ca82012-11-28 03:38:47 +010054struct dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010055
Petr Machata929bd572012-12-17 03:20:34 +010056static struct 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 Machata929bd572012-12-17 03:20:34 +010061output_indent(struct process *proc)
Petr Machata54004752012-05-03 18:36:48 +020062{
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 Machata929bd572012-12-17 03:20:34 +010068begin_of_line(struct process *proc, int is_func, int indent)
Petr Machata37b73c02012-01-06 16:00:25 +010069{
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;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020082 struct timeval diff;
83
Petr Machata75642272012-11-23 19:02:13 +010084 gettimeofday(&tv, NULL);
Juan Cespedesf666d191998-09-20 23:04:34 +020085
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
87 old_tv.tv_sec = tv.tv_sec;
88 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020089 }
90 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
91 if (tv.tv_usec >= old_tv.tv_usec) {
92 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
93 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020094 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020095 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
96 }
97 old_tv.tv_sec = tv.tv_sec;
98 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010099 current_column += fprintf(options.output, "%3lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100100 (unsigned long)diff.tv_sec,
101 (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;
Petr Machata75642272012-11-23 19:02:13 +0100105 gettimeofday(&tv, NULL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100107 current_column += fprintf(options.output, "%lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100108 (unsigned long)tv.tv_sec,
109 (int)tv.tv_usec);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 } else if (opt_t > 1) {
111 struct tm *tmp = localtime(&tv.tv_sec);
112 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100113 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
115 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200116 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100117 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100118 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119 tmp->tm_hour, tmp->tm_min,
120 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200121 }
122 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100123 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100124 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100125 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100127 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100128 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100129 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100130 }
Petr Machata37b73c02012-01-06 16:00:25 +0100131 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200132 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200133 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200134}
135
Petr Machatab7819162012-01-09 04:26:15 +0100136static struct arg_type_info *
137get_unknown_type(void)
138{
139 static struct arg_type_info *info = NULL;
140 if (info == NULL) {
141 info = malloc(sizeof(*info));
142 if (info == NULL) {
143 report_global_error("malloc: %s", strerror(errno));
144 abort();
145 }
146 *info = *type_get_simple(ARGTYPE_LONG);
147 info->lens = &guess_lens;
148 }
149 return info;
150}
151
Petr Machataf6ec08a2012-01-06 16:58:54 +0100152/* The default prototype is: long X(long, long, long, long). */
Petr Machata3d083b62012-11-22 15:14:45 +0100153static struct prototype *
Petr Machataf6ec08a2012-01-06 16:58:54 +0100154build_default_prototype(void)
155{
Petr Machataebc56a72012-12-17 04:37:57 +0100156 static struct prototype *ret = NULL;
157 if (ret != NULL)
158 return ret;
159
160 static struct prototype proto;
161 prototype_init(&proto);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100162
Petr Machatab7819162012-01-09 04:26:15 +0100163 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataebc56a72012-12-17 04:37:57 +0100164 assert(unknown_type != NULL);
165 proto.return_info = unknown_type;
166 proto.own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100167
Petr Machataebc56a72012-12-17 04:37:57 +0100168 struct param unknown_param;
169 param_init_type(&unknown_param, unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100170
Petr Machataebc56a72012-12-17 04:37:57 +0100171 size_t i;
172 for (i = 0; i < 4; ++i)
173 if (prototype_push_param(&proto, &unknown_param) < 0) {
174 report_global_error("build_default_prototype: %s",
175 strerror(errno));
176 prototype_destroy(&proto);
177 return NULL;
178 }
Petr Machata865303f2012-01-06 18:40:38 +0100179
Petr Machataebc56a72012-12-17 04:37:57 +0100180 ret = &proto;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100181 return ret;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100182}
183
Petr Machata3d083b62012-11-22 15:14:45 +0100184static struct prototype *
Petr Machataa94d9df2012-12-17 04:41:16 +0100185library_get_prototype(struct library *lib, const char *name)
186{
187 if (lib->protolib == NULL)
188 lib->protolib = protolib_cache_search(&g_protocache,
189 lib->soname, 1);
190 if (lib->protolib == NULL)
191 return NULL;
192
193 return protolib_lookup_prototype(lib->protolib, name);
194}
195
196struct find_proto_data {
197 const char *name;
198 struct prototype *ret;
199};
200
201static enum callback_status
202find_proto_cb(struct process *proc, struct library *lib, void *d)
203{
204 struct find_proto_data *data = d;
205 data->ret = library_get_prototype(lib, data->name);
206 return CBS_STOP_IF(data->ret != NULL);
207}
208
209static struct prototype *
210lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
Petr Machata3d083b62012-11-22 15:14:45 +0100211{
Petr Machata08cdf322012-12-05 01:46:13 +0100212 struct library *lib = libsym->lib;
213 if (lib != NULL) {
Petr Machataa94d9df2012-12-17 04:41:16 +0100214 struct find_proto_data data = { libsym->name };
215 data.ret = library_get_prototype(lib, libsym->name);
216 if (data.ret == NULL
217 && libsym->plt_type == LS_TOPLT_EXEC)
218 proc_each_library(proc, NULL, find_proto_cb, &data);
219 if (data.ret != NULL)
220 return data.ret;
Petr Machata08cdf322012-12-05 01:46:13 +0100221 }
222
Petr Machataebc56a72012-12-17 04:37:57 +0100223 return build_default_prototype();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100224}
225
Juan Cespedesf1350522008-12-16 18:19:58 +0100226void
Petr Machata929bd572012-12-17 03:20:34 +0100227output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200228{
229 if (options.summary)
230 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200231
Petr Machataefc94362012-10-27 00:49:05 +0200232 if (current_proc != NULL) {
233 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100234 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200235 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100236 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100237 }
Petr Machataefc94362012-10-27 00:49:05 +0200238 current_proc = NULL;
239 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200240 return;
Petr Machataefc94362012-10-27 00:49:05 +0200241
Petr Machata37b73c02012-01-06 16:00:25 +0100242 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100243
Petr Machataefc94362012-10-27 00:49:05 +0200244 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200245 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100246 vfprintf(options.output, fmt, args);
247 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200248 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200249
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100251}
252
Juan Cespedesf1350522008-12-16 18:19:58 +0100253static void
254tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100255 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100256 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100257 }
258}
259
Petr Machataf6ec08a2012-01-06 16:58:54 +0100260static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100261output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100262{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100263 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100264}
265
266static int
Petr Machata929bd572012-12-17 03:20:34 +0100267fetch_simple_param(enum tof type, struct process *proc,
268 struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100269 struct value_dict *arguments,
270 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100271 struct value *valuep)
272{
273 /* Arrays decay into pointers per C standard. We check for
274 * this here, because here we also capture arrays that come
275 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100276 if (info->type == ARGTYPE_ARRAY) {
277 struct arg_type_info *tmp = malloc(sizeof(*tmp));
278 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100279 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100280 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100281 info = tmp;
282 own = 1;
283 }
284 }
285
286 struct value value;
287 value_init(&value, proc, NULL, info, own);
288 if (fetch_arg_next(context, type, proc, info, &value) < 0)
289 return -1;
290
291 if (val_dict_push_next(arguments, &value) < 0) {
292 value_destroy(&value);
293 return -1;
294 }
295
296 if (valuep != NULL)
297 *valuep = value;
298
299 return 0;
300}
301
302static void
303fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
304{
305 if (*params_leftp == -1)
306 *params_leftp = val_dict_count(arguments);
307}
308
309static int
Petr Machata929bd572012-12-17 03:20:34 +0100310fetch_param_pack(enum tof type, struct process *proc,
311 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100312 struct value_dict *arguments, struct param *param,
313 ssize_t *params_leftp)
314{
315 struct param_enum *e = param_pack_init(param, arguments);
316 if (e == NULL)
317 return -1;
318
319 int ret = 0;
320 while (1) {
321 int insert_stop = 0;
322 struct arg_type_info *info = malloc(sizeof(*info));
323 if (info == NULL
324 || param_pack_next(param, e, info, &insert_stop) < 0) {
325 fail:
326 free(info);
327 ret = -1;
328 break;
329 }
330
331 if (insert_stop)
332 fetch_param_stop(arguments, params_leftp);
333
Petr Machatabc58f2a2012-10-30 00:25:50 +0100334 if (info->type == ARGTYPE_VOID) {
335 type_destroy(info);
336 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100337 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100338 }
Petr Machata865303f2012-01-06 18:40:38 +0100339
340 struct value val;
341 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100342 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100343 goto fail;
344
345 int stop = 0;
346 switch (param_pack_stop(param, e, &val)) {
347 case PPCB_ERR:
348 goto fail;
349 case PPCB_STOP:
350 stop = 1;
351 case PPCB_CONT:
352 break;
353 }
354
355 if (stop)
356 break;
357 }
358
359 param_pack_done(param, e);
360 return ret;
361}
362
363static int
Petr Machata929bd572012-12-17 03:20:34 +0100364fetch_one_param(enum tof type, struct process *proc,
365 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100366 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100367 ssize_t *params_leftp)
368{
Petr Machata865303f2012-01-06 18:40:38 +0100369 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200370 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100371 case PARAM_FLAVOR_TYPE:
372 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100373 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100374
375 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200376 if (fetch_param_pack_start(context,
377 param->u.pack.ppflavor) < 0)
378 return -1;
379 rc = fetch_param_pack(type, proc, context, arguments,
380 param, params_leftp);
381 fetch_param_pack_end(context);
382 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100383
384 case PARAM_FLAVOR_STOP:
385 fetch_param_stop(arguments, params_leftp);
386 return 0;
387 }
388
389 assert(!"Invalid param flavor!");
390 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100391}
392
Petr Machataebc56a72012-12-17 04:37:57 +0100393struct fetch_one_param_data
394{
395 struct process *proc;
396 struct fetch_context *context;
397 struct value_dict *arguments;
398 ssize_t *params_leftp;
399 enum tof tof;
400};
401
402static enum callback_status
403fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
404{
405 struct fetch_one_param_data *cb_data = data;
Petr Machataa94d9df2012-12-17 04:41:16 +0100406 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
407 cb_data->context,
408 cb_data->arguments, param,
409 cb_data->params_leftp) < 0);
Petr Machataebc56a72012-12-17 04:37:57 +0100410}
411
Petr Machataf6ec08a2012-01-06 16:58:54 +0100412static int
Petr Machata929bd572012-12-17 03:20:34 +0100413fetch_params(enum tof type, struct process *proc,
414 struct fetch_context *context,
Petr Machata3d083b62012-11-22 15:14:45 +0100415 struct value_dict *arguments, struct prototype *func,
416 ssize_t *params_leftp)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100417{
Petr Machataebc56a72012-12-17 04:37:57 +0100418 struct fetch_one_param_data cb_data
419 = { proc, context, arguments, params_leftp, type };
420 if (prototype_each_param(func, NULL,
421 &fetch_one_param_cb, &cb_data) != NULL)
422 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100423
424 /* Implicit stop at the end of parameter list. */
425 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100426
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 return 0;
428}
429
Petr Machataf7c46bb2012-11-01 22:15:45 +0100430struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100431{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100432 struct value *value;
433 struct value_dict *arguments;
434};
435
436static int
437format_argument_cb(FILE *stream, void *ptr)
438{
439 struct format_argument_data *data = ptr;
440 int o = format_argument(stream, data->value, data->arguments);
441 if (o < 0)
442 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100443 return o;
444}
445
446static int
447output_params(struct value_dict *arguments, size_t start, size_t end,
448 int *need_delimp)
449{
450 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100451 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100452 struct value *value = val_dict_get_num(arguments, i);
453 if (value == NULL)
454 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100455
456 struct format_argument_data data = { value, arguments };
457 int o = delim_output(options.output, need_delimp,
458 format_argument_cb, &data);
459 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100460 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100461 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100462 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100463 return 0;
464}
465
Juan Cespedesf1350522008-12-16 18:19:58 +0100466void
Petr Machata929bd572012-12-17 03:20:34 +0100467output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100468 struct library_symbol *libsym)
469{
Juan Cespedesda9b9532009-04-07 15:33:50 +0200470 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100471 return;
472 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200473 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100474 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100475 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100476 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200477 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100478 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100479 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200480 if (!options.hide_caller && libsym->lib != NULL
481 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100482 /* We don't terribly mind failing this. */
483 account_output(&current_column,
484 fprintf(options.output, "%s->",
485 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100486
Petr Machata08cdf322012-12-05 01:46:13 +0100487 const char *name = libsym->name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100488#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100489 if (options.demangle)
Petr Machata08cdf322012-12-05 01:46:13 +0100490 name = my_demangle(libsym->name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200491#endif
Petr Machataadec2012012-11-01 22:05:04 +0100492 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100493 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100494 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100495
Petr Machata09654202012-12-05 17:07:21 +0100496 if (libsym->lib != NULL
497 && libsym->lib->type != LT_LIBTYPE_MAIN
498 && libsym->plt_type == LS_TOPLT_NONE
499 && account_output(&current_column,
500 fprintf(options.output, "@%s",
501 libsym->lib->soname)) < 0)
502 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100503 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100504
Petr Machata09654202012-12-05 17:07:21 +0100505 account_output(&current_column, fprintf(options.output, "("));
506
Petr Machataa94d9df2012-12-17 04:41:16 +0100507 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machata09654202012-12-05 17:07:21 +0100508 if (func == NULL) {
509 account_output(&current_column, fprintf(options.output, "???"));
510 return;
511 }
512
Petr Machataf6ec08a2012-01-06 16:58:54 +0100513 struct fetch_context *context = fetch_arg_init(type, proc,
514 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100515 struct value_dict *arguments = malloc(sizeof(*arguments));
516 if (arguments == NULL)
517 return;
518 val_dict_init(arguments);
519
Petr Machataf6ec08a2012-01-06 16:58:54 +0100520 ssize_t params_left = -1;
521 int need_delim = 0;
522 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
523 || output_params(arguments, 0, params_left, &need_delim) < 0) {
524 val_dict_destroy(arguments);
525 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100526 arguments = NULL;
527 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100528 }
Petr Machata94078ec2012-01-05 18:07:02 +0100529
530 struct callstack_element *stel
531 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100532 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100533 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100534 stel->out.params_left = params_left;
535 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100536}
537
Petr Machatad7e4ca82012-11-28 03:38:47 +0100538static void
539free_stringp_cb(const char **stringp, void *data)
540{
541 free((char *)*stringp);
542}
543
Juan Cespedesf1350522008-12-16 18:19:58 +0100544void
Petr Machata929bd572012-12-17 03:20:34 +0100545output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100546{
Petr Machataa94d9df2012-12-17 04:41:16 +0100547 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100548 if (func == NULL)
549 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100550
Petr Machatad7e4ca82012-11-28 03:38:47 +0100551again:
Juan Cespedesda9b9532009-04-07 15:33:50 +0200552 if (options.summary) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100553 if (dict_opt_c == NULL) {
554 dict_opt_c = malloc(sizeof(*dict_opt_c));
555 if (dict_opt_c == NULL) {
556 oom:
557 fprintf(stderr,
558 "Can't allocate memory for "
559 "keeping track of -c.\n");
560 free(dict_opt_c);
561 options.summary = 0;
562 goto again;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100563 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100564 DICT_INIT(dict_opt_c, const char *, struct opt_c_struct,
565 dict_hash_string, dict_eq_string, NULL);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100566 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100567
Petr Machata98ff3092013-03-08 22:11:36 +0100568 struct opt_c_struct *st
Petr Machata08cdf322012-12-05 01:46:13 +0100569 = DICT_FIND_REF(dict_opt_c, &libsym->name,
Petr Machata98ff3092013-03-08 22:11:36 +0100570 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100571 if (st == NULL) {
Petr Machata08cdf322012-12-05 01:46:13 +0100572 const char *na = strdup(libsym->name);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100573 struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
574 if (na == NULL
575 || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
576 free((char *)na);
577 DICT_DESTROY(dict_opt_c, const char *,
578 struct opt_c_struct,
579 free_stringp_cb, NULL, NULL);
580 goto oom;
581 }
Petr Machata08cdf322012-12-05 01:46:13 +0100582 st = DICT_FIND_REF(dict_opt_c, &libsym->name,
Petr Machata98ff3092013-03-08 22:11:36 +0100583 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100584 assert(st != NULL);
585 }
586
Juan Cespedesd65efa32003-02-03 00:22:30 +0100587 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
588 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
589 st->tv.tv_sec++;
590 } else {
591 st->tv.tv_usec += current_time_spent.tv_usec;
592 }
593 st->count++;
594 st->tv.tv_sec += current_time_spent.tv_sec;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100595 return;
596 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100597
Paul Gilliam76c61f12006-06-14 06:55:21 +0200598 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100599 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100600 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200601 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200602 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200603 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100604 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100605#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100606 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100607 fprintf(options.output, "<... %s resumed> ",
Petr Machata08cdf322012-12-05 01:46:13 +0100608 options.demangle ? my_demangle(libsym->name)
609 : libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200610#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100611 current_column +=
Petr Machata08cdf322012-12-05 01:46:13 +0100612 fprintf(options.output, "<... %s resumed> ", libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200613#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100614 }
615
Petr Machata94078ec2012-01-05 18:07:02 +0100616 struct callstack_element *stel
617 = &proc->callstack[proc->callstack_depth - 1];
618
Petr Machataf6ec08a2012-01-06 16:58:54 +0100619 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100620
Petr Machataf6ec08a2012-01-06 16:58:54 +0100621 /* Fetch & enter into dictionary the retval first, so that
622 * other values can use it in expressions. */
623 struct value retval;
624 int own_retval = 0;
625 if (context != NULL) {
626 value_init(&retval, proc, NULL, func->return_info, 0);
627 own_retval = 1;
628 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200629 &retval) < 0)
630 value_set_type(&retval, NULL, 0);
631 else if (stel->arguments != NULL
632 && val_dict_push_named(stel->arguments, &retval,
633 "retval", 0) == 0)
634 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200635 }
Petr Machata94078ec2012-01-05 18:07:02 +0100636
Petr Machataf6ec08a2012-01-06 16:58:54 +0100637 if (stel->arguments != NULL)
638 output_params(stel->arguments, stel->out.params_left,
639 val_dict_count(stel->arguments),
640 &stel->out.need_delim);
641
642 current_column += fprintf(options.output, ") ");
643 tabto(options.align - 1);
644 fprintf(options.output, "= ");
645
Petr Machataf7c46bb2012-11-01 22:15:45 +0100646 if (context != NULL && retval.type != NULL) {
647 struct format_argument_data data = { &retval, stel->arguments };
648 format_argument_cb(options.output, &data);
649 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100650
651 if (own_retval)
652 value_destroy(&retval);
653
Juan Cespedesd65efa32003-02-03 00:22:30 +0100654 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100655 fprintf(options.output, " <%lu.%06d>",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100656 (unsigned long)current_time_spent.tv_sec,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100657 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100658 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100659 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700660
661#if defined(HAVE_LIBUNWIND)
662 if (options.bt_depth > 0) {
663 unw_cursor_t cursor;
664 unw_word_t ip, sp;
665 int unwind_depth = options.bt_depth;
666 char fn_name[100];
667
668 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
669 while (unwind_depth) {
670 unw_get_reg(&cursor, UNW_REG_IP, &ip);
671 unw_get_reg(&cursor, UNW_REG_SP, &sp);
672 unw_get_proc_name(&cursor, fn_name, 100, NULL);
673 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
674 if (unw_step(&cursor) <= 0)
675 break;
676 unwind_depth--;
677 }
678 fprintf(options.output, "\n");
679 }
680#endif /* defined(HAVE_LIBUNWIND) */
681
Paul Gilliam76c61f12006-06-14 06:55:21 +0200682 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100683 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100684}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100685
Petr Machataadec2012012-11-01 22:05:04 +0100686int
Petr Machata9c400982012-11-01 22:15:01 +0100687delim_output(FILE *stream, int *need_delimp,
688 int (*writer)(FILE *stream, void *data),
689 void *data)
690{
691 int o;
692
693 /* If we don't need a delimiter, then we don't need to go
694 * through a temporary stream. It's all the same whether
695 * WRITER emits anything or not. */
696 if (!*need_delimp) {
697 o = writer(stream, data);
698
699 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100700 struct memstream ms;
701 if (memstream_init(&ms) < 0)
702 return -1;
703 o = writer(ms.stream, data);
704 if (memstream_close(&ms) < 0)
705 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100706 if (o > 0 && ((*need_delimp
707 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100708 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100709 o = -1;
710
Petr Machata2ae374a2012-12-08 05:08:48 +0100711 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100712 }
713
714 if (o < 0)
715 return -1;
716
717 *need_delimp = *need_delimp || o > 0;
718 return o;
719}
720
721int
Petr Machataadec2012012-11-01 22:05:04 +0100722account_output(int *countp, int c)
723{
724 if (c > 0)
725 *countp += c;
726 return c;
727}
728
Petr Machatad71cf2d2012-01-05 18:28:31 +0100729static void
730do_report(const char *filename, unsigned line_no, const char *severity,
731 const char *fmt, va_list args)
732{
733 char buf[128];
734 vsnprintf(buf, sizeof(buf), fmt, args);
735 buf[sizeof(buf) - 1] = 0;
736 if (filename != NULL)
737 output_line(0, "%s:%d: %s: %s",
738 filename, line_no, severity, buf);
739 else
740 output_line(0, "%s: %s", severity, buf);
741}
742
743void
Petr Machata89c5ca22012-10-27 00:49:38 +0200744report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100745{
746 va_list args;
747 va_start(args, fmt);
748 do_report(filename, line_no, "error", fmt, args);
749 va_end(args);
750}
751
752void
Petr Machata89c5ca22012-10-27 00:49:38 +0200753report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100754{
755 va_list args;
756 va_start(args, fmt);
757 do_report(filename, line_no, "warning", fmt, args);
758 va_end(args);
759}
760
761void
Petr Machata89c5ca22012-10-27 00:49:38 +0200762report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100763{
764 va_list args;
765 va_start(args, fmt);
766 do_report(NULL, 0, "error", fmt, args);
767 va_end(args);
768}