blob: b09cabf59a5004e67c6d65252311b1760b03dfff [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 Machata4dcc3892013-01-30 23:11:35 +0100124 if (is_func) {
125 struct callstack_element *stel
126 = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100127 current_column += fprintf(options.output, "[%p] ",
Petr Machata4dcc3892013-01-30 23:11:35 +0100128 stel->return_addr);
129 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100130 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 proc->instruction_pointer);
Petr Machata4dcc3892013-01-30 23:11:35 +0100132 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100133 }
Petr Machata37b73c02012-01-06 16:00:25 +0100134 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200135 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200136 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200137}
138
Petr Machatab7819162012-01-09 04:26:15 +0100139static struct arg_type_info *
140get_unknown_type(void)
141{
Petr Machataf1bd48b2012-12-25 05:15:47 +0100142 static struct arg_type_info *ret = NULL;
143 if (ret != NULL)
144 return ret;
145
146 static struct arg_type_info info;
147 info = *type_get_simple(ARGTYPE_LONG);
148 info.lens = &guess_lens;
149 ret = &info;
150 return ret;
Petr Machatab7819162012-01-09 04:26:15 +0100151}
152
Petr Machataf6ec08a2012-01-06 16:58:54 +0100153/* The default prototype is: long X(long, long, long, long). */
Petr Machata3d083b62012-11-22 15:14:45 +0100154static struct prototype *
Petr Machataf6ec08a2012-01-06 16:58:54 +0100155build_default_prototype(void)
156{
Petr Machataebc56a72012-12-17 04:37:57 +0100157 static struct prototype *ret = NULL;
158 if (ret != NULL)
159 return ret;
160
161 static struct prototype proto;
162 prototype_init(&proto);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100163
Petr Machatab7819162012-01-09 04:26:15 +0100164 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataebc56a72012-12-17 04:37:57 +0100165 assert(unknown_type != NULL);
166 proto.return_info = unknown_type;
167 proto.own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100168
Petr Machataebc56a72012-12-17 04:37:57 +0100169 struct param unknown_param;
170 param_init_type(&unknown_param, unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100171
Petr Machataebc56a72012-12-17 04:37:57 +0100172 size_t i;
173 for (i = 0; i < 4; ++i)
174 if (prototype_push_param(&proto, &unknown_param) < 0) {
175 report_global_error("build_default_prototype: %s",
176 strerror(errno));
177 prototype_destroy(&proto);
178 return NULL;
179 }
Petr Machata865303f2012-01-06 18:40:38 +0100180
Petr Machataebc56a72012-12-17 04:37:57 +0100181 ret = &proto;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100182 return ret;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100183}
184
Petr Machata3d083b62012-11-22 15:14:45 +0100185static struct prototype *
Petr Machataa94d9df2012-12-17 04:41:16 +0100186library_get_prototype(struct library *lib, const char *name)
187{
188 if (lib->protolib == NULL)
189 lib->protolib = protolib_cache_search(&g_protocache,
Petr Machataa5e983b2012-12-05 23:51:17 +0100190 lib->soname, 0, 1);
Petr Machataa94d9df2012-12-17 04:41:16 +0100191 if (lib->protolib == NULL)
192 return NULL;
193
Petr Machata82f748d2013-10-23 00:39:23 +0200194 return protolib_lookup_prototype(lib->protolib, name,
195 lib->type != LT_LIBTYPE_SYSCALL);
Petr Machataa94d9df2012-12-17 04:41:16 +0100196}
197
198struct find_proto_data {
199 const char *name;
200 struct prototype *ret;
201};
202
203static enum callback_status
204find_proto_cb(struct process *proc, struct library *lib, void *d)
205{
206 struct find_proto_data *data = d;
207 data->ret = library_get_prototype(lib, data->name);
208 return CBS_STOP_IF(data->ret != NULL);
209}
210
211static struct prototype *
212lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
Petr Machata3d083b62012-11-22 15:14:45 +0100213{
Petr Machatab8f0d8b2013-10-16 14:25:45 +0200214 if (libsym->proto != NULL)
215 return libsym->proto;
216
Petr Machata08cdf322012-12-05 01:46:13 +0100217 struct library *lib = libsym->lib;
218 if (lib != NULL) {
Petr Machataa94d9df2012-12-17 04:41:16 +0100219 struct find_proto_data data = { libsym->name };
220 data.ret = library_get_prototype(lib, libsym->name);
221 if (data.ret == NULL
222 && libsym->plt_type == LS_TOPLT_EXEC)
223 proc_each_library(proc, NULL, find_proto_cb, &data);
224 if (data.ret != NULL)
225 return data.ret;
Petr Machata08cdf322012-12-05 01:46:13 +0100226 }
227
Petr Machataebc56a72012-12-17 04:37:57 +0100228 return build_default_prototype();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100229}
230
Juan Cespedesf1350522008-12-16 18:19:58 +0100231void
Petr Machata929bd572012-12-17 03:20:34 +0100232output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200233{
234 if (options.summary)
235 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200236
Petr Machataefc94362012-10-27 00:49:05 +0200237 if (current_proc != NULL) {
238 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100239 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200240 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100241 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100242 }
Petr Machataefc94362012-10-27 00:49:05 +0200243 current_proc = NULL;
244 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200245 return;
Petr Machataefc94362012-10-27 00:49:05 +0200246
Petr Machata37b73c02012-01-06 16:00:25 +0100247 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100248
Petr Machataefc94362012-10-27 00:49:05 +0200249 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200250 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100251 vfprintf(options.output, fmt, args);
252 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200253 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200254
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100256}
257
Juan Cespedesf1350522008-12-16 18:19:58 +0100258static void
259tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100260 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100261 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100262 }
263}
264
Petr Machataf6ec08a2012-01-06 16:58:54 +0100265static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100266output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100267{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100268 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100269}
270
271static int
Petr Machata929bd572012-12-17 03:20:34 +0100272fetch_simple_param(enum tof type, struct process *proc,
273 struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100274 struct value_dict *arguments,
275 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100276 struct value *valuep)
277{
278 /* Arrays decay into pointers per C standard. We check for
279 * this here, because here we also capture arrays that come
280 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100281 if (info->type == ARGTYPE_ARRAY) {
282 struct arg_type_info *tmp = malloc(sizeof(*tmp));
283 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100284 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100285 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100286 info = tmp;
287 own = 1;
288 }
289 }
290
291 struct value value;
292 value_init(&value, proc, NULL, info, own);
293 if (fetch_arg_next(context, type, proc, info, &value) < 0)
294 return -1;
295
296 if (val_dict_push_next(arguments, &value) < 0) {
297 value_destroy(&value);
298 return -1;
299 }
300
301 if (valuep != NULL)
302 *valuep = value;
303
304 return 0;
305}
306
307static void
308fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
309{
310 if (*params_leftp == -1)
311 *params_leftp = val_dict_count(arguments);
312}
313
314static int
Petr Machata929bd572012-12-17 03:20:34 +0100315fetch_param_pack(enum tof type, struct process *proc,
316 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100317 struct value_dict *arguments, struct param *param,
318 ssize_t *params_leftp)
319{
320 struct param_enum *e = param_pack_init(param, arguments);
321 if (e == NULL)
322 return -1;
323
324 int ret = 0;
325 while (1) {
326 int insert_stop = 0;
327 struct arg_type_info *info = malloc(sizeof(*info));
328 if (info == NULL
329 || param_pack_next(param, e, info, &insert_stop) < 0) {
330 fail:
331 free(info);
332 ret = -1;
333 break;
334 }
335
336 if (insert_stop)
337 fetch_param_stop(arguments, params_leftp);
338
Petr Machatabc58f2a2012-10-30 00:25:50 +0100339 if (info->type == ARGTYPE_VOID) {
340 type_destroy(info);
341 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100342 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100343 }
Petr Machata865303f2012-01-06 18:40:38 +0100344
345 struct value val;
346 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100347 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100348 goto fail;
349
350 int stop = 0;
351 switch (param_pack_stop(param, e, &val)) {
352 case PPCB_ERR:
353 goto fail;
354 case PPCB_STOP:
355 stop = 1;
356 case PPCB_CONT:
357 break;
358 }
359
360 if (stop)
361 break;
362 }
363
364 param_pack_done(param, e);
365 return ret;
366}
367
368static int
Petr Machata929bd572012-12-17 03:20:34 +0100369fetch_one_param(enum tof type, struct process *proc,
370 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100371 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100372 ssize_t *params_leftp)
373{
Petr Machata865303f2012-01-06 18:40:38 +0100374 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200375 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100376 case PARAM_FLAVOR_TYPE:
377 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100378 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100379
380 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200381 if (fetch_param_pack_start(context,
382 param->u.pack.ppflavor) < 0)
383 return -1;
384 rc = fetch_param_pack(type, proc, context, arguments,
385 param, params_leftp);
386 fetch_param_pack_end(context);
387 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100388
389 case PARAM_FLAVOR_STOP:
390 fetch_param_stop(arguments, params_leftp);
391 return 0;
392 }
393
394 assert(!"Invalid param flavor!");
395 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100396}
397
Petr Machataebc56a72012-12-17 04:37:57 +0100398struct fetch_one_param_data
399{
400 struct process *proc;
401 struct fetch_context *context;
402 struct value_dict *arguments;
403 ssize_t *params_leftp;
404 enum tof tof;
405};
406
407static enum callback_status
408fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
409{
410 struct fetch_one_param_data *cb_data = data;
Petr Machataa94d9df2012-12-17 04:41:16 +0100411 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
412 cb_data->context,
413 cb_data->arguments, param,
414 cb_data->params_leftp) < 0);
Petr Machataebc56a72012-12-17 04:37:57 +0100415}
416
Petr Machataf6ec08a2012-01-06 16:58:54 +0100417static int
Petr Machata929bd572012-12-17 03:20:34 +0100418fetch_params(enum tof type, struct process *proc,
419 struct fetch_context *context,
Petr Machata3d083b62012-11-22 15:14:45 +0100420 struct value_dict *arguments, struct prototype *func,
421 ssize_t *params_leftp)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100422{
Petr Machataebc56a72012-12-17 04:37:57 +0100423 struct fetch_one_param_data cb_data
424 = { proc, context, arguments, params_leftp, type };
425 if (prototype_each_param(func, NULL,
426 &fetch_one_param_cb, &cb_data) != NULL)
427 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100428
429 /* Implicit stop at the end of parameter list. */
430 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100431
Petr Machataf6ec08a2012-01-06 16:58:54 +0100432 return 0;
433}
434
Petr Machataf7c46bb2012-11-01 22:15:45 +0100435struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100436{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100437 struct value *value;
438 struct value_dict *arguments;
439};
440
441static int
442format_argument_cb(FILE *stream, void *ptr)
443{
444 struct format_argument_data *data = ptr;
445 int o = format_argument(stream, data->value, data->arguments);
446 if (o < 0)
447 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100448 return o;
449}
450
451static int
452output_params(struct value_dict *arguments, size_t start, size_t end,
453 int *need_delimp)
454{
455 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100456 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100457 struct value *value = val_dict_get_num(arguments, i);
458 if (value == NULL)
459 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100460
461 struct format_argument_data data = { value, arguments };
462 int o = delim_output(options.output, need_delimp,
463 format_argument_cb, &data);
464 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100465 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100466 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100467 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100468 return 0;
469}
470
Juan Cespedesf1350522008-12-16 18:19:58 +0100471void
Petr Machata929bd572012-12-17 03:20:34 +0100472output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100473 struct library_symbol *libsym)
474{
Juan Cespedesda9b9532009-04-07 15:33:50 +0200475 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100476 return;
477 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200478 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100479 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100480 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100481 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200482 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100483 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100484 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200485 if (!options.hide_caller && libsym->lib != NULL
486 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100487 /* We don't terribly mind failing this. */
488 account_output(&current_column,
489 fprintf(options.output, "%s->",
490 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100491
Petr Machata08cdf322012-12-05 01:46:13 +0100492 const char *name = libsym->name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100493#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100494 if (options.demangle)
Petr Machata08cdf322012-12-05 01:46:13 +0100495 name = my_demangle(libsym->name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200496#endif
Petr Machataadec2012012-11-01 22:05:04 +0100497 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100498 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100499 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100500
Petr Machata09654202012-12-05 17:07:21 +0100501 if (libsym->lib != NULL
502 && libsym->lib->type != LT_LIBTYPE_MAIN
503 && libsym->plt_type == LS_TOPLT_NONE
504 && account_output(&current_column,
505 fprintf(options.output, "@%s",
506 libsym->lib->soname)) < 0)
507 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100508 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100509
Petr Machata09654202012-12-05 17:07:21 +0100510 account_output(&current_column, fprintf(options.output, "("));
511
Petr Machataa94d9df2012-12-17 04:41:16 +0100512 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machata09654202012-12-05 17:07:21 +0100513 if (func == NULL) {
Petr Machata1b02c732013-01-07 18:22:51 +0100514 fail:
Petr Machata09654202012-12-05 17:07:21 +0100515 account_output(&current_column, fprintf(options.output, "???"));
516 return;
517 }
518
Petr Machataf6ec08a2012-01-06 16:58:54 +0100519 struct fetch_context *context = fetch_arg_init(type, proc,
520 func->return_info);
Petr Machata1b02c732013-01-07 18:22:51 +0100521 if (context == NULL)
522 goto fail;
523
Petr Machata94078ec2012-01-05 18:07:02 +0100524 struct value_dict *arguments = malloc(sizeof(*arguments));
Petr Machata1b02c732013-01-07 18:22:51 +0100525 if (arguments == NULL) {
526 fetch_arg_done(context);
527 goto fail;
528 }
Petr Machata94078ec2012-01-05 18:07:02 +0100529 val_dict_init(arguments);
530
Petr Machataf6ec08a2012-01-06 16:58:54 +0100531 ssize_t params_left = -1;
532 int need_delim = 0;
533 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
534 || output_params(arguments, 0, params_left, &need_delim) < 0) {
535 val_dict_destroy(arguments);
536 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100537 arguments = NULL;
538 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100539 }
Petr Machata94078ec2012-01-05 18:07:02 +0100540
541 struct callstack_element *stel
542 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100543 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100544 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100545 stel->out.params_left = params_left;
546 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100547}
548
Petr Machatad7e4ca82012-11-28 03:38:47 +0100549static void
550free_stringp_cb(const char **stringp, void *data)
551{
552 free((char *)*stringp);
553}
554
Juan Cespedesf1350522008-12-16 18:19:58 +0100555void
Petr Machata929bd572012-12-17 03:20:34 +0100556output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100557{
Petr Machataa94d9df2012-12-17 04:41:16 +0100558 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100559 if (func == NULL)
560 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100561
Petr Machatad7e4ca82012-11-28 03:38:47 +0100562again:
Juan Cespedesda9b9532009-04-07 15:33:50 +0200563 if (options.summary) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100564 if (dict_opt_c == NULL) {
565 dict_opt_c = malloc(sizeof(*dict_opt_c));
566 if (dict_opt_c == NULL) {
567 oom:
568 fprintf(stderr,
569 "Can't allocate memory for "
570 "keeping track of -c.\n");
571 free(dict_opt_c);
572 options.summary = 0;
573 goto again;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100574 }
Peter Wu97485502013-09-26 00:55:56 +0200575 DICT_INIT(dict_opt_c, char *, struct opt_c_struct,
Petr Machatad7e4ca82012-11-28 03:38:47 +0100576 dict_hash_string, dict_eq_string, NULL);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100577 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100578
Petr Machata98ff3092013-03-08 22:11:36 +0100579 struct opt_c_struct *st
Petr Machata08cdf322012-12-05 01:46:13 +0100580 = DICT_FIND_REF(dict_opt_c, &libsym->name,
Petr Machata98ff3092013-03-08 22:11:36 +0100581 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100582 if (st == NULL) {
Petr Machata08cdf322012-12-05 01:46:13 +0100583 const char *na = strdup(libsym->name);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100584 struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
585 if (na == NULL
586 || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
587 free((char *)na);
588 DICT_DESTROY(dict_opt_c, const char *,
589 struct opt_c_struct,
590 free_stringp_cb, NULL, NULL);
591 goto oom;
592 }
Petr Machata08cdf322012-12-05 01:46:13 +0100593 st = DICT_FIND_REF(dict_opt_c, &libsym->name,
Petr Machata98ff3092013-03-08 22:11:36 +0100594 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100595 assert(st != NULL);
596 }
597
Juan Cespedesd65efa32003-02-03 00:22:30 +0100598 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
599 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
600 st->tv.tv_sec++;
601 } else {
602 st->tv.tv_usec += current_time_spent.tv_usec;
603 }
604 st->count++;
605 st->tv.tv_sec += current_time_spent.tv_sec;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100606 return;
607 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100608
Paul Gilliam76c61f12006-06-14 06:55:21 +0200609 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100610 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100611 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200612 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200613 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200614 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100615 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100616#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100617 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100618 fprintf(options.output, "<... %s resumed> ",
Petr Machata08cdf322012-12-05 01:46:13 +0100619 options.demangle ? my_demangle(libsym->name)
620 : libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200621#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100622 current_column +=
Petr Machata08cdf322012-12-05 01:46:13 +0100623 fprintf(options.output, "<... %s resumed> ", libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200624#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100625 }
626
Petr Machata94078ec2012-01-05 18:07:02 +0100627 struct callstack_element *stel
628 = &proc->callstack[proc->callstack_depth - 1];
629
Petr Machataf6ec08a2012-01-06 16:58:54 +0100630 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100631
Petr Machataf6ec08a2012-01-06 16:58:54 +0100632 /* Fetch & enter into dictionary the retval first, so that
633 * other values can use it in expressions. */
634 struct value retval;
635 int own_retval = 0;
636 if (context != NULL) {
637 value_init(&retval, proc, NULL, func->return_info, 0);
638 own_retval = 1;
639 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200640 &retval) < 0)
641 value_set_type(&retval, NULL, 0);
642 else if (stel->arguments != NULL
643 && val_dict_push_named(stel->arguments, &retval,
644 "retval", 0) == 0)
645 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200646 }
Petr Machata94078ec2012-01-05 18:07:02 +0100647
Petr Machataf6ec08a2012-01-06 16:58:54 +0100648 if (stel->arguments != NULL)
649 output_params(stel->arguments, stel->out.params_left,
650 val_dict_count(stel->arguments),
651 &stel->out.need_delim);
652
653 current_column += fprintf(options.output, ") ");
654 tabto(options.align - 1);
655 fprintf(options.output, "= ");
656
Petr Machataf7c46bb2012-11-01 22:15:45 +0100657 if (context != NULL && retval.type != NULL) {
658 struct format_argument_data data = { &retval, stel->arguments };
659 format_argument_cb(options.output, &data);
660 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100661
662 if (own_retval)
663 value_destroy(&retval);
664
Juan Cespedesd65efa32003-02-03 00:22:30 +0100665 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100666 fprintf(options.output, " <%lu.%06d>",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100667 (unsigned long)current_time_spent.tv_sec,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100668 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100669 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100670 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700671
672#if defined(HAVE_LIBUNWIND)
Petr Machataaf1e6032013-01-06 17:16:24 +0100673 if (options.bt_depth > 0
674 && proc->unwind_priv != NULL
675 && proc->unwind_as != NULL) {
Joe Damatoab3b72c2010-10-31 00:21:53 -0700676 unw_cursor_t cursor;
Luca Clementi29276482013-10-17 19:48:38 -0700677 arch_addr_t ip, function_offset;
Luca Clementi29276482013-10-17 19:48:38 -0700678 struct library *lib = NULL;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700679 int unwind_depth = options.bt_depth;
680 char fn_name[100];
Luca Clementi29276482013-10-17 19:48:38 -0700681 const char *lib_name;
682 size_t distance;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700683
Luca Clementi29276482013-10-17 19:48:38 -0700684 /* Verify that we can safely cast arch_addr_t* to
685 * unw_word_t*. */
686 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
687 != sizeof(arch_addr_t))]);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700688 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
689 while (unwind_depth) {
Luca Clementi29276482013-10-17 19:48:38 -0700690 unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip);
Luca Clementi29276482013-10-17 19:48:38 -0700691 unw_get_proc_name(&cursor, fn_name, sizeof(fn_name),
692 (unw_word_t *) &function_offset);
693
694 /* We are looking for the library with the base address
695 * closest to the current ip. */
696 lib_name = "unmapped_area";
697 distance = (size_t) -1;
698 lib = proc->libraries;
699 while (lib != NULL) {
700 /* N.B.: Assumes sizeof(size_t) ==
701 * sizeof(arch_addr_t).
702 * Keyword: double cast. */
703 if ((ip >= lib->base) &&
704 ((size_t)(ip - lib->base)
705 < distance)) {
706 distance = ip - lib->base;
707 lib_name = lib->pathname;
708 }
709 lib = lib->next;
710 }
711
712 fprintf(options.output, " > %s(%s+0x%p) [%p]\n",
713 lib_name, fn_name, function_offset, ip);
714
Joe Damatoab3b72c2010-10-31 00:21:53 -0700715 if (unw_step(&cursor) <= 0)
716 break;
717 unwind_depth--;
718 }
719 fprintf(options.output, "\n");
720 }
721#endif /* defined(HAVE_LIBUNWIND) */
722
Paul Gilliam76c61f12006-06-14 06:55:21 +0200723 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100724 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100725}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100726
Petr Machataadec2012012-11-01 22:05:04 +0100727int
Petr Machata9c400982012-11-01 22:15:01 +0100728delim_output(FILE *stream, int *need_delimp,
729 int (*writer)(FILE *stream, void *data),
730 void *data)
731{
732 int o;
733
734 /* If we don't need a delimiter, then we don't need to go
735 * through a temporary stream. It's all the same whether
736 * WRITER emits anything or not. */
737 if (!*need_delimp) {
738 o = writer(stream, data);
739
740 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100741 struct memstream ms;
742 if (memstream_init(&ms) < 0)
743 return -1;
744 o = writer(ms.stream, data);
745 if (memstream_close(&ms) < 0)
746 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100747 if (o > 0 && ((*need_delimp
748 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100749 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100750 o = -1;
751
Petr Machata2ae374a2012-12-08 05:08:48 +0100752 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100753 }
754
755 if (o < 0)
756 return -1;
757
758 *need_delimp = *need_delimp || o > 0;
759 return o;
760}
761
762int
Petr Machataadec2012012-11-01 22:05:04 +0100763account_output(int *countp, int c)
764{
765 if (c > 0)
766 *countp += c;
767 return c;
768}
769
Petr Machatad71cf2d2012-01-05 18:28:31 +0100770static void
771do_report(const char *filename, unsigned line_no, const char *severity,
772 const char *fmt, va_list args)
773{
774 char buf[128];
775 vsnprintf(buf, sizeof(buf), fmt, args);
776 buf[sizeof(buf) - 1] = 0;
777 if (filename != NULL)
778 output_line(0, "%s:%d: %s: %s",
779 filename, line_no, severity, buf);
780 else
781 output_line(0, "%s: %s", severity, buf);
782}
783
784void
Petr Machata89c5ca22012-10-27 00:49:38 +0200785report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100786{
787 va_list args;
788 va_start(args, fmt);
789 do_report(filename, line_no, "error", fmt, args);
790 va_end(args);
791}
792
793void
Petr Machata89c5ca22012-10-27 00:49:38 +0200794report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100795{
796 va_list args;
797 va_start(args, fmt);
798 do_report(filename, line_no, "warning", fmt, args);
799 va_end(args);
800}
801
802void
Petr Machata89c5ca22012-10-27 00:49:38 +0200803report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100804{
805 va_list args;
806 va_start(args, fmt);
807 do_report(NULL, 0, "error", fmt, args);
808 va_end(args);
809}