blob: f804cd05a29b4792986b2224c1e40524b0ded63a [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 Machata8a730f32013-11-21 20:43:51 +010047#include "summary.h"
Petr Machata000e3112012-01-03 17:03:39 +010048#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010049#include "value.h"
50#include "value_dict.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020051
Petr Machata8a730f32013-11-21 20:43:51 +010052static struct process *current_proc = NULL;
Petr Machataba1664b2012-04-28 14:59:05 +020053static size_t current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010054static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020055
Juan Cespedesf1350522008-12-16 18:19:58 +010056static void
Petr Machata929bd572012-12-17 03:20:34 +010057output_indent(struct process *proc)
Petr Machata54004752012-05-03 18:36:48 +020058{
59 int d = options.indent * (proc->callstack_depth - 1);
60 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020061}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020062
Juan Cespedesf1350522008-12-16 18:19:58 +010063static void
Petr Machata929bd572012-12-17 03:20:34 +010064begin_of_line(struct process *proc, int is_func, int indent)
Petr Machata37b73c02012-01-06 16:00:25 +010065{
Juan Cespedes5e01f651998-03-08 22:31:44 +010066 current_column = 0;
67 if (!proc) {
68 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020069 }
Juan Cespedesc693f022009-05-21 18:59:41 +020070 if ((options.output != stderr) && (opt_p || options.follow)) {
71 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020072 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010073 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010074 }
Juan Cespedesf666d191998-09-20 23:04:34 +020075 if (opt_r) {
76 struct timeval tv;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020078 struct timeval diff;
79
Petr Machata75642272012-11-23 19:02:13 +010080 gettimeofday(&tv, NULL);
Juan Cespedesf666d191998-09-20 23:04:34 +020081
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
83 old_tv.tv_sec = tv.tv_sec;
84 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020085 }
86 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
87 if (tv.tv_usec >= old_tv.tv_usec) {
88 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
89 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020090 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020091 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
92 }
93 old_tv.tv_sec = tv.tv_sec;
94 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010095 current_column += fprintf(options.output, "%3lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +010096 (unsigned long)diff.tv_sec,
97 (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020098 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020099 if (opt_t) {
100 struct timeval tv;
Petr Machata75642272012-11-23 19:02:13 +0100101 gettimeofday(&tv, NULL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100103 current_column += fprintf(options.output, "%lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100104 (unsigned long)tv.tv_sec,
105 (int)tv.tv_usec);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 } else if (opt_t > 1) {
107 struct tm *tmp = localtime(&tv.tv_sec);
108 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100109 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
111 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200112 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100114 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115 tmp->tm_hour, tmp->tm_min,
116 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200117 }
118 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100119 if (opt_i) {
Petr Machata4dcc3892013-01-30 23:11:35 +0100120 if (is_func) {
121 struct callstack_element *stel
122 = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100123 current_column += fprintf(options.output, "[%p] ",
Petr Machata4dcc3892013-01-30 23:11:35 +0100124 stel->return_addr);
125 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100126 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 proc->instruction_pointer);
Petr Machata4dcc3892013-01-30 23:11:35 +0100128 }
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{
Petr Machataf1bd48b2012-12-25 05:15:47 +0100138 static struct arg_type_info *ret = NULL;
139 if (ret != NULL)
140 return ret;
141
142 static struct arg_type_info info;
143 info = *type_get_simple(ARGTYPE_LONG);
144 info.lens = &guess_lens;
145 ret = &info;
146 return ret;
Petr Machatab7819162012-01-09 04:26:15 +0100147}
148
Petr Machataf6ec08a2012-01-06 16:58:54 +0100149/* The default prototype is: long X(long, long, long, long). */
Petr Machata3d083b62012-11-22 15:14:45 +0100150static struct prototype *
Petr Machataf6ec08a2012-01-06 16:58:54 +0100151build_default_prototype(void)
152{
Petr Machataebc56a72012-12-17 04:37:57 +0100153 static struct prototype *ret = NULL;
154 if (ret != NULL)
155 return ret;
156
157 static struct prototype proto;
158 prototype_init(&proto);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100159
Petr Machatab7819162012-01-09 04:26:15 +0100160 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataebc56a72012-12-17 04:37:57 +0100161 assert(unknown_type != NULL);
162 proto.return_info = unknown_type;
163 proto.own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100164
Petr Machataebc56a72012-12-17 04:37:57 +0100165 struct param unknown_param;
166 param_init_type(&unknown_param, unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100167
Petr Machataebc56a72012-12-17 04:37:57 +0100168 size_t i;
169 for (i = 0; i < 4; ++i)
170 if (prototype_push_param(&proto, &unknown_param) < 0) {
171 report_global_error("build_default_prototype: %s",
172 strerror(errno));
173 prototype_destroy(&proto);
174 return NULL;
175 }
Petr Machata865303f2012-01-06 18:40:38 +0100176
Petr Machataebc56a72012-12-17 04:37:57 +0100177 ret = &proto;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100178 return ret;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100179}
180
Petr Machata98a7dce2013-10-23 17:37:47 +0200181static bool
182snip_period(char *buf)
183{
184 char *period = strrchr(buf, '.');
185 if (period != NULL && strcmp(period, ".so") != 0) {
186 *period = 0;
187 return true;
188 } else {
189 return false;
190 }
191}
192
Petr Machata3d083b62012-11-22 15:14:45 +0100193static struct prototype *
Petr Machataa94d9df2012-12-17 04:41:16 +0100194library_get_prototype(struct library *lib, const char *name)
195{
Petr Machata98a7dce2013-10-23 17:37:47 +0200196 if (lib->protolib == NULL) {
197 size_t sz = strlen(lib->soname);
198 char buf[sz + 1];
Petr Machataac52f262013-10-24 14:45:21 +0200199 memcpy(buf, lib->soname, sz + 1);
Petr Machata98a7dce2013-10-23 17:37:47 +0200200
201 do {
202 if (protolib_cache_maybe_load(&g_protocache, buf, 0,
203 true, &lib->protolib) < 0)
204 return NULL;
205 } while (lib->protolib == NULL
206 && lib->type == LT_LIBTYPE_DSO
207 && snip_period(buf));
208
209 if (lib->protolib == NULL)
210 lib->protolib = protolib_cache_default(&g_protocache,
211 buf, 0);
212 }
Petr Machataa94d9df2012-12-17 04:41:16 +0100213 if (lib->protolib == NULL)
214 return NULL;
215
Petr Machata82f748d2013-10-23 00:39:23 +0200216 return protolib_lookup_prototype(lib->protolib, name,
217 lib->type != LT_LIBTYPE_SYSCALL);
Petr Machataa94d9df2012-12-17 04:41:16 +0100218}
219
220struct find_proto_data {
221 const char *name;
222 struct prototype *ret;
223};
224
225static enum callback_status
226find_proto_cb(struct process *proc, struct library *lib, void *d)
227{
228 struct find_proto_data *data = d;
229 data->ret = library_get_prototype(lib, data->name);
230 return CBS_STOP_IF(data->ret != NULL);
231}
232
233static struct prototype *
234lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
Petr Machata3d083b62012-11-22 15:14:45 +0100235{
Petr Machatab8f0d8b2013-10-16 14:25:45 +0200236 if (libsym->proto != NULL)
237 return libsym->proto;
238
Petr Machata08cdf322012-12-05 01:46:13 +0100239 struct library *lib = libsym->lib;
240 if (lib != NULL) {
Petr Machataa94d9df2012-12-17 04:41:16 +0100241 struct find_proto_data data = { libsym->name };
242 data.ret = library_get_prototype(lib, libsym->name);
243 if (data.ret == NULL
244 && libsym->plt_type == LS_TOPLT_EXEC)
245 proc_each_library(proc, NULL, find_proto_cb, &data);
246 if (data.ret != NULL)
247 return data.ret;
Petr Machata08cdf322012-12-05 01:46:13 +0100248 }
249
Petr Machataebc56a72012-12-17 04:37:57 +0100250 return build_default_prototype();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100251}
252
Juan Cespedesf1350522008-12-16 18:19:58 +0100253void
Petr Machata929bd572012-12-17 03:20:34 +0100254output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200255{
256 if (options.summary)
257 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200258
Petr Machataefc94362012-10-27 00:49:05 +0200259 if (current_proc != NULL) {
260 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100261 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200262 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100263 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100264 }
Petr Machataefc94362012-10-27 00:49:05 +0200265 current_proc = NULL;
266 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200267 return;
Petr Machataefc94362012-10-27 00:49:05 +0200268
Petr Machata37b73c02012-01-06 16:00:25 +0100269 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270
Petr Machataefc94362012-10-27 00:49:05 +0200271 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200272 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100273 vfprintf(options.output, fmt, args);
274 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200275 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200276
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100277 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100278}
279
Juan Cespedesf1350522008-12-16 18:19:58 +0100280static void
281tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100282 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100283 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100284 }
285}
286
Petr Machataf6ec08a2012-01-06 16:58:54 +0100287static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100288output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100289{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100290 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100291}
292
293static int
Petr Machata929bd572012-12-17 03:20:34 +0100294fetch_simple_param(enum tof type, struct process *proc,
295 struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100296 struct value_dict *arguments,
297 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100298 struct value *valuep)
299{
300 /* Arrays decay into pointers per C standard. We check for
301 * this here, because here we also capture arrays that come
302 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100303 if (info->type == ARGTYPE_ARRAY) {
304 struct arg_type_info *tmp = malloc(sizeof(*tmp));
305 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100306 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100307 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100308 info = tmp;
309 own = 1;
310 }
311 }
312
313 struct value value;
314 value_init(&value, proc, NULL, info, own);
315 if (fetch_arg_next(context, type, proc, info, &value) < 0)
316 return -1;
317
318 if (val_dict_push_next(arguments, &value) < 0) {
319 value_destroy(&value);
320 return -1;
321 }
322
323 if (valuep != NULL)
324 *valuep = value;
325
326 return 0;
327}
328
329static void
330fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
331{
332 if (*params_leftp == -1)
333 *params_leftp = val_dict_count(arguments);
334}
335
336static int
Petr Machata929bd572012-12-17 03:20:34 +0100337fetch_param_pack(enum tof type, struct process *proc,
338 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100339 struct value_dict *arguments, struct param *param,
340 ssize_t *params_leftp)
341{
342 struct param_enum *e = param_pack_init(param, arguments);
343 if (e == NULL)
344 return -1;
345
346 int ret = 0;
347 while (1) {
348 int insert_stop = 0;
349 struct arg_type_info *info = malloc(sizeof(*info));
350 if (info == NULL
351 || param_pack_next(param, e, info, &insert_stop) < 0) {
352 fail:
353 free(info);
354 ret = -1;
355 break;
356 }
357
358 if (insert_stop)
359 fetch_param_stop(arguments, params_leftp);
360
Petr Machatabc58f2a2012-10-30 00:25:50 +0100361 if (info->type == ARGTYPE_VOID) {
362 type_destroy(info);
363 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100364 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100365 }
Petr Machata865303f2012-01-06 18:40:38 +0100366
367 struct value val;
368 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100369 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100370 goto fail;
371
372 int stop = 0;
373 switch (param_pack_stop(param, e, &val)) {
374 case PPCB_ERR:
375 goto fail;
376 case PPCB_STOP:
377 stop = 1;
378 case PPCB_CONT:
379 break;
380 }
381
382 if (stop)
383 break;
384 }
385
386 param_pack_done(param, e);
387 return ret;
388}
389
390static int
Petr Machata929bd572012-12-17 03:20:34 +0100391fetch_one_param(enum tof type, struct process *proc,
392 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100393 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100394 ssize_t *params_leftp)
395{
Petr Machata865303f2012-01-06 18:40:38 +0100396 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200397 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100398 case PARAM_FLAVOR_TYPE:
399 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100400 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100401
402 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200403 if (fetch_param_pack_start(context,
404 param->u.pack.ppflavor) < 0)
405 return -1;
406 rc = fetch_param_pack(type, proc, context, arguments,
407 param, params_leftp);
408 fetch_param_pack_end(context);
409 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100410
411 case PARAM_FLAVOR_STOP:
412 fetch_param_stop(arguments, params_leftp);
413 return 0;
414 }
415
416 assert(!"Invalid param flavor!");
417 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100418}
419
Petr Machataebc56a72012-12-17 04:37:57 +0100420struct fetch_one_param_data
421{
422 struct process *proc;
423 struct fetch_context *context;
424 struct value_dict *arguments;
425 ssize_t *params_leftp;
426 enum tof tof;
427};
428
429static enum callback_status
430fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
431{
432 struct fetch_one_param_data *cb_data = data;
Petr Machataa94d9df2012-12-17 04:41:16 +0100433 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
434 cb_data->context,
435 cb_data->arguments, param,
436 cb_data->params_leftp) < 0);
Petr Machataebc56a72012-12-17 04:37:57 +0100437}
438
Petr Machataf6ec08a2012-01-06 16:58:54 +0100439static int
Petr Machata929bd572012-12-17 03:20:34 +0100440fetch_params(enum tof type, struct process *proc,
441 struct fetch_context *context,
Petr Machata3d083b62012-11-22 15:14:45 +0100442 struct value_dict *arguments, struct prototype *func,
443 ssize_t *params_leftp)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100444{
Petr Machataebc56a72012-12-17 04:37:57 +0100445 struct fetch_one_param_data cb_data
446 = { proc, context, arguments, params_leftp, type };
447 if (prototype_each_param(func, NULL,
448 &fetch_one_param_cb, &cb_data) != NULL)
449 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100450
451 /* Implicit stop at the end of parameter list. */
452 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100453
Petr Machataf6ec08a2012-01-06 16:58:54 +0100454 return 0;
455}
456
Petr Machataf7c46bb2012-11-01 22:15:45 +0100457struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100458{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100459 struct value *value;
460 struct value_dict *arguments;
461};
462
463static int
464format_argument_cb(FILE *stream, void *ptr)
465{
466 struct format_argument_data *data = ptr;
467 int o = format_argument(stream, data->value, data->arguments);
468 if (o < 0)
469 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100470 return o;
471}
472
473static int
474output_params(struct value_dict *arguments, size_t start, size_t end,
475 int *need_delimp)
476{
477 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100478 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100479 struct value *value = val_dict_get_num(arguments, i);
480 if (value == NULL)
481 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100482
483 struct format_argument_data data = { value, arguments };
484 int o = delim_output(options.output, need_delimp,
485 format_argument_cb, &data);
486 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100487 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100488 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100489 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100490 return 0;
491}
492
Juan Cespedesf1350522008-12-16 18:19:58 +0100493void
Petr Machata929bd572012-12-17 03:20:34 +0100494output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100495 struct library_symbol *libsym)
496{
Petr Machata8a730f32013-11-21 20:43:51 +0100497 assert(! options.summary);
498
Paul Gilliam76c61f12006-06-14 06:55:21 +0200499 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100500 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100501 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100502 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200503 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100504 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100505 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200506 if (!options.hide_caller && libsym->lib != NULL
507 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100508 /* We don't terribly mind failing this. */
509 account_output(&current_column,
510 fprintf(options.output, "%s->",
511 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100512
Petr Machata08cdf322012-12-05 01:46:13 +0100513 const char *name = libsym->name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100514#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100515 if (options.demangle)
Petr Machata08cdf322012-12-05 01:46:13 +0100516 name = my_demangle(libsym->name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200517#endif
Petr Machataadec2012012-11-01 22:05:04 +0100518 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100519 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100520 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100521
Petr Machata09654202012-12-05 17:07:21 +0100522 if (libsym->lib != NULL
523 && libsym->lib->type != LT_LIBTYPE_MAIN
524 && libsym->plt_type == LS_TOPLT_NONE
525 && account_output(&current_column,
526 fprintf(options.output, "@%s",
527 libsym->lib->soname)) < 0)
528 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100529 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100530
Petr Machata09654202012-12-05 17:07:21 +0100531 account_output(&current_column, fprintf(options.output, "("));
532
Petr Machataa94d9df2012-12-17 04:41:16 +0100533 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machata09654202012-12-05 17:07:21 +0100534 if (func == NULL) {
Petr Machata1b02c732013-01-07 18:22:51 +0100535 fail:
Petr Machata09654202012-12-05 17:07:21 +0100536 account_output(&current_column, fprintf(options.output, "???"));
537 return;
538 }
539
Petr Machataf6ec08a2012-01-06 16:58:54 +0100540 struct fetch_context *context = fetch_arg_init(type, proc,
541 func->return_info);
Petr Machata1b02c732013-01-07 18:22:51 +0100542 if (context == NULL)
543 goto fail;
544
Petr Machata94078ec2012-01-05 18:07:02 +0100545 struct value_dict *arguments = malloc(sizeof(*arguments));
Petr Machata1b02c732013-01-07 18:22:51 +0100546 if (arguments == NULL) {
547 fetch_arg_done(context);
548 goto fail;
549 }
Petr Machata94078ec2012-01-05 18:07:02 +0100550 val_dict_init(arguments);
551
Petr Machataf6ec08a2012-01-06 16:58:54 +0100552 ssize_t params_left = -1;
553 int need_delim = 0;
554 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
555 || output_params(arguments, 0, params_left, &need_delim) < 0) {
556 val_dict_destroy(arguments);
557 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100558 arguments = NULL;
559 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100560 }
Petr Machata94078ec2012-01-05 18:07:02 +0100561
562 struct callstack_element *stel
563 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100564 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100565 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100566 stel->out.params_left = params_left;
567 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100568}
569
Juan Cespedesf1350522008-12-16 18:19:58 +0100570void
Petr Machata8a730f32013-11-21 20:43:51 +0100571output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
572 struct timedelta *spent)
Petr Machata14184b32012-02-10 13:10:26 +0100573{
Petr Machata8a730f32013-11-21 20:43:51 +0100574 assert(! options.summary);
575
Petr Machataa94d9df2012-12-17 04:41:16 +0100576 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100577 if (func == NULL)
578 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100579
Petr Machata8a730f32013-11-21 20:43:51 +0100580 if (current_proc != NULL
581 && (current_proc != proc
582 || current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100583 fprintf(options.output, " <unfinished ...>\n");
Petr Machata8a730f32013-11-21 20:43:51 +0100584 current_proc = NULL;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200585 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200586 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100587 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100588#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100589 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100590 fprintf(options.output, "<... %s resumed> ",
Petr Machata08cdf322012-12-05 01:46:13 +0100591 options.demangle ? my_demangle(libsym->name)
592 : libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200593#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100594 current_column +=
Petr Machata08cdf322012-12-05 01:46:13 +0100595 fprintf(options.output, "<... %s resumed> ", libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200596#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100597 }
598
Petr Machata94078ec2012-01-05 18:07:02 +0100599 struct callstack_element *stel
600 = &proc->callstack[proc->callstack_depth - 1];
601
Petr Machataf6ec08a2012-01-06 16:58:54 +0100602 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100603
Petr Machataf6ec08a2012-01-06 16:58:54 +0100604 /* Fetch & enter into dictionary the retval first, so that
605 * other values can use it in expressions. */
606 struct value retval;
607 int own_retval = 0;
608 if (context != NULL) {
609 value_init(&retval, proc, NULL, func->return_info, 0);
610 own_retval = 1;
611 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200612 &retval) < 0)
613 value_set_type(&retval, NULL, 0);
614 else if (stel->arguments != NULL
615 && val_dict_push_named(stel->arguments, &retval,
616 "retval", 0) == 0)
617 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200618 }
Petr Machata94078ec2012-01-05 18:07:02 +0100619
Petr Machataf6ec08a2012-01-06 16:58:54 +0100620 if (stel->arguments != NULL)
621 output_params(stel->arguments, stel->out.params_left,
622 val_dict_count(stel->arguments),
623 &stel->out.need_delim);
624
625 current_column += fprintf(options.output, ") ");
626 tabto(options.align - 1);
627 fprintf(options.output, "= ");
628
Petr Machataf7c46bb2012-11-01 22:15:45 +0100629 if (context != NULL && retval.type != NULL) {
630 struct format_argument_data data = { &retval, stel->arguments };
631 format_argument_cb(options.output, &data);
632 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100633
634 if (own_retval)
635 value_destroy(&retval);
636
Juan Cespedesd65efa32003-02-03 00:22:30 +0100637 if (opt_T) {
Petr Machata8a730f32013-11-21 20:43:51 +0100638 assert(spent != NULL);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100639 fprintf(options.output, " <%lu.%06d>",
Petr Machata8a730f32013-11-21 20:43:51 +0100640 (unsigned long) spent->tm.tv_sec,
641 (int) spent->tm.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100642 }
Petr Machata8a730f32013-11-21 20:43:51 +0100643
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100644 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700645
646#if defined(HAVE_LIBUNWIND)
Petr Machataaf1e6032013-01-06 17:16:24 +0100647 if (options.bt_depth > 0
648 && proc->unwind_priv != NULL
649 && proc->unwind_as != NULL) {
Joe Damatoab3b72c2010-10-31 00:21:53 -0700650 unw_cursor_t cursor;
Luca Clementi29276482013-10-17 19:48:38 -0700651 arch_addr_t ip, function_offset;
Luca Clementi29276482013-10-17 19:48:38 -0700652 struct library *lib = NULL;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700653 int unwind_depth = options.bt_depth;
654 char fn_name[100];
Luca Clementi29276482013-10-17 19:48:38 -0700655 const char *lib_name;
656 size_t distance;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700657
Luca Clementi29276482013-10-17 19:48:38 -0700658 /* Verify that we can safely cast arch_addr_t* to
659 * unw_word_t*. */
660 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
661 != sizeof(arch_addr_t))]);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700662 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
663 while (unwind_depth) {
Luca Clementiaf452c62014-01-03 22:05:03 -0800664
665 if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip)) {
666 fprintf(options.output, " > stacktrace_error\n");
667 continue;
668 }
Luca Clementi29276482013-10-17 19:48:38 -0700669
670 /* We are looking for the library with the base address
671 * closest to the current ip. */
672 lib_name = "unmapped_area";
673 distance = (size_t) -1;
674 lib = proc->libraries;
675 while (lib != NULL) {
676 /* N.B.: Assumes sizeof(size_t) ==
677 * sizeof(arch_addr_t).
678 * Keyword: double cast. */
679 if ((ip >= lib->base) &&
680 ((size_t)(ip - lib->base)
681 < distance)) {
682 distance = ip - lib->base;
683 lib_name = lib->pathname;
684 }
685 lib = lib->next;
686 }
687
Luca Clementiaf452c62014-01-03 22:05:03 -0800688 own_retval = unw_get_proc_name(&cursor, fn_name, sizeof(fn_name),
689 (unw_word_t *) &function_offset);
690 if ((own_retval == 0) || (own_retval == -UNW_ENOMEM))
691 fprintf(options.output, " > %s(%s+%p) [%p]\n",
Luca Clementi29276482013-10-17 19:48:38 -0700692 lib_name, fn_name, function_offset, ip);
Luca Clementiaf452c62014-01-03 22:05:03 -0800693 else
694 fprintf(options.output, " > %s(??\?) [%p]\n",
695 lib_name, ip);
Luca Clementi29276482013-10-17 19:48:38 -0700696
Joe Damatoab3b72c2010-10-31 00:21:53 -0700697 if (unw_step(&cursor) <= 0)
698 break;
699 unwind_depth--;
700 }
701 fprintf(options.output, "\n");
702 }
703#endif /* defined(HAVE_LIBUNWIND) */
704
Petr Machata8a730f32013-11-21 20:43:51 +0100705 current_proc = NULL;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100706 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100707}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100708
Petr Machataadec2012012-11-01 22:05:04 +0100709int
Petr Machata9c400982012-11-01 22:15:01 +0100710delim_output(FILE *stream, int *need_delimp,
711 int (*writer)(FILE *stream, void *data),
712 void *data)
713{
714 int o;
715
716 /* If we don't need a delimiter, then we don't need to go
717 * through a temporary stream. It's all the same whether
718 * WRITER emits anything or not. */
719 if (!*need_delimp) {
720 o = writer(stream, data);
721
722 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100723 struct memstream ms;
724 if (memstream_init(&ms) < 0)
725 return -1;
726 o = writer(ms.stream, data);
727 if (memstream_close(&ms) < 0)
728 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100729 if (o > 0 && ((*need_delimp
730 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100731 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100732 o = -1;
733
Petr Machata2ae374a2012-12-08 05:08:48 +0100734 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100735 }
736
737 if (o < 0)
738 return -1;
739
740 *need_delimp = *need_delimp || o > 0;
741 return o;
742}
743
744int
Petr Machataadec2012012-11-01 22:05:04 +0100745account_output(int *countp, int c)
746{
747 if (c > 0)
748 *countp += c;
749 return c;
750}
751
Petr Machatad71cf2d2012-01-05 18:28:31 +0100752static void
753do_report(const char *filename, unsigned line_no, const char *severity,
754 const char *fmt, va_list args)
755{
756 char buf[128];
757 vsnprintf(buf, sizeof(buf), fmt, args);
758 buf[sizeof(buf) - 1] = 0;
759 if (filename != NULL)
760 output_line(0, "%s:%d: %s: %s",
761 filename, line_no, severity, buf);
762 else
763 output_line(0, "%s: %s", severity, buf);
764}
765
766void
Petr Machata89c5ca22012-10-27 00:49:38 +0200767report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100768{
769 va_list args;
770 va_start(args, fmt);
771 do_report(filename, line_no, "error", fmt, args);
772 va_end(args);
773}
774
775void
Petr Machata89c5ca22012-10-27 00:49:38 +0200776report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100777{
778 va_list args;
779 va_start(args, fmt);
780 do_report(filename, line_no, "warning", fmt, args);
781 va_end(args);
782}
783
784void
Petr Machata89c5ca22012-10-27 00:49:38 +0200785report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100786{
787 va_list args;
788 va_start(args, fmt);
789 do_report(NULL, 0, "error", fmt, args);
790 va_end(args);
791}