blob: 0cec6538d5c386e2875636c1c6509c2173d2199a [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
2 * This file is part of ltrace.
Petr Machata0f6f30c2014-01-07 11:57:36 +01003 * Copyright (C) 2011,2012,2013,2014 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>
Mark Wielaarddfefa9f2014-01-07 21:00:44 +010036#include <inttypes.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020037
Petr Machataebc56a72012-12-17 04:37:57 +010038#include "output.h"
Petr Machata3ac8db62012-11-23 18:43:10 +010039#include "demangle.h"
Petr Machata9bc290b2012-11-23 19:08:22 +010040#include "fetch.h"
41#include "lens_default.h"
Petr Machata29add4f2012-02-18 16:38:05 +010042#include "library.h"
Petr Machata9bc290b2012-11-23 19:08:22 +010043#include "memstream.h"
Petr Machata8bf82d02012-11-23 18:44:31 +010044#include "options.h"
Petr Machata9bc290b2012-11-23 19:08:22 +010045#include "param.h"
46#include "proc.h"
Petr Machataebc56a72012-12-17 04:37:57 +010047#include "prototype.h"
Petr Machata8a730f32013-11-21 20:43:51 +010048#include "summary.h"
Petr Machata000e3112012-01-03 17:03:39 +010049#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010050#include "value.h"
51#include "value_dict.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020052
Petr Machata8a730f32013-11-21 20:43:51 +010053static struct process *current_proc = NULL;
Petr Machataba1664b2012-04-28 14:59:05 +020054static size_t current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010055static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020056
Juan Cespedesf1350522008-12-16 18:19:58 +010057static void
Petr Machata929bd572012-12-17 03:20:34 +010058output_indent(struct process *proc)
Petr Machata54004752012-05-03 18:36:48 +020059{
60 int d = options.indent * (proc->callstack_depth - 1);
61 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020062}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020063
Juan Cespedesf1350522008-12-16 18:19:58 +010064static void
Petr Machata929bd572012-12-17 03:20:34 +010065begin_of_line(struct process *proc, int is_func, int indent)
Petr Machata37b73c02012-01-06 16:00:25 +010066{
Juan Cespedes5e01f651998-03-08 22:31:44 +010067 current_column = 0;
68 if (!proc) {
69 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020070 }
Juan Cespedesc693f022009-05-21 18:59:41 +020071 if ((options.output != stderr) && (opt_p || options.follow)) {
72 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020073 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010074 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 }
Juan Cespedesf666d191998-09-20 23:04:34 +020076 if (opt_r) {
77 struct timeval tv;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020079 struct timeval diff;
80
Petr Machata75642272012-11-23 19:02:13 +010081 gettimeofday(&tv, NULL);
Juan Cespedesf666d191998-09-20 23:04:34 +020082
Ian Wienand2d45b1a2006-02-20 22:48:07 +010083 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
84 old_tv.tv_sec = tv.tv_sec;
85 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020086 }
87 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
88 if (tv.tv_usec >= old_tv.tv_usec) {
89 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
90 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020091 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020092 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
93 }
94 old_tv.tv_sec = tv.tv_sec;
95 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010096 current_column += fprintf(options.output, "%3lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +010097 (unsigned long)diff.tv_sec,
98 (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020099 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200100 if (opt_t) {
101 struct timeval tv;
Petr Machata75642272012-11-23 19:02:13 +0100102 gettimeofday(&tv, NULL);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100104 current_column += fprintf(options.output, "%lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100105 (unsigned long)tv.tv_sec,
106 (int)tv.tv_usec);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100107 } else if (opt_t > 1) {
108 struct tm *tmp = localtime(&tv.tv_sec);
109 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100110 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100111 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
112 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200113 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100115 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100116 tmp->tm_hour, tmp->tm_min,
117 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200118 }
119 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100120 if (opt_i) {
Petr Machata4dcc3892013-01-30 23:11:35 +0100121 if (is_func) {
122 struct callstack_element *stel
123 = &proc->callstack[proc->callstack_depth - 1];
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100124 current_column += fprintf(options.output, "[%p] ",
Petr Machata4dcc3892013-01-30 23:11:35 +0100125 stel->return_addr);
126 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100127 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128 proc->instruction_pointer);
Petr Machata4dcc3892013-01-30 23:11:35 +0100129 }
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{
Petr Machataf1bd48b2012-12-25 05:15:47 +0100139 static struct arg_type_info *ret = NULL;
140 if (ret != NULL)
141 return ret;
142
143 static struct arg_type_info info;
144 info = *type_get_simple(ARGTYPE_LONG);
145 info.lens = &guess_lens;
146 ret = &info;
147 return ret;
Petr Machatab7819162012-01-09 04:26:15 +0100148}
149
Petr Machataf6ec08a2012-01-06 16:58:54 +0100150/* The default prototype is: long X(long, long, long, long). */
Petr Machata3d083b62012-11-22 15:14:45 +0100151static struct prototype *
Petr Machataf6ec08a2012-01-06 16:58:54 +0100152build_default_prototype(void)
153{
Petr Machataebc56a72012-12-17 04:37:57 +0100154 static struct prototype *ret = NULL;
155 if (ret != NULL)
156 return ret;
157
158 static struct prototype proto;
159 prototype_init(&proto);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100160
Petr Machatab7819162012-01-09 04:26:15 +0100161 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataebc56a72012-12-17 04:37:57 +0100162 assert(unknown_type != NULL);
163 proto.return_info = unknown_type;
164 proto.own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100165
Petr Machataebc56a72012-12-17 04:37:57 +0100166 struct param unknown_param;
167 param_init_type(&unknown_param, unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100168
Petr Machataebc56a72012-12-17 04:37:57 +0100169 size_t i;
170 for (i = 0; i < 4; ++i)
171 if (prototype_push_param(&proto, &unknown_param) < 0) {
172 report_global_error("build_default_prototype: %s",
173 strerror(errno));
174 prototype_destroy(&proto);
175 return NULL;
176 }
Petr Machata865303f2012-01-06 18:40:38 +0100177
Petr Machataebc56a72012-12-17 04:37:57 +0100178 ret = &proto;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100179 return ret;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100180}
181
Petr Machata98a7dce2013-10-23 17:37:47 +0200182static bool
183snip_period(char *buf)
184{
185 char *period = strrchr(buf, '.');
186 if (period != NULL && strcmp(period, ".so") != 0) {
187 *period = 0;
188 return true;
189 } else {
190 return false;
191 }
192}
193
Petr Machata3d083b62012-11-22 15:14:45 +0100194static struct prototype *
Petr Machataa94d9df2012-12-17 04:41:16 +0100195library_get_prototype(struct library *lib, const char *name)
196{
Petr Machata98a7dce2013-10-23 17:37:47 +0200197 if (lib->protolib == NULL) {
198 size_t sz = strlen(lib->soname);
199 char buf[sz + 1];
Petr Machataac52f262013-10-24 14:45:21 +0200200 memcpy(buf, lib->soname, sz + 1);
Petr Machata98a7dce2013-10-23 17:37:47 +0200201
202 do {
203 if (protolib_cache_maybe_load(&g_protocache, buf, 0,
204 true, &lib->protolib) < 0)
205 return NULL;
206 } while (lib->protolib == NULL
207 && lib->type == LT_LIBTYPE_DSO
208 && snip_period(buf));
209
210 if (lib->protolib == NULL)
211 lib->protolib = protolib_cache_default(&g_protocache,
212 buf, 0);
213 }
Petr Machataa94d9df2012-12-17 04:41:16 +0100214 if (lib->protolib == NULL)
215 return NULL;
216
Petr Machata82f748d2013-10-23 00:39:23 +0200217 return protolib_lookup_prototype(lib->protolib, name,
218 lib->type != LT_LIBTYPE_SYSCALL);
Petr Machataa94d9df2012-12-17 04:41:16 +0100219}
220
221struct find_proto_data {
222 const char *name;
223 struct prototype *ret;
224};
225
226static enum callback_status
227find_proto_cb(struct process *proc, struct library *lib, void *d)
228{
229 struct find_proto_data *data = d;
230 data->ret = library_get_prototype(lib, data->name);
231 return CBS_STOP_IF(data->ret != NULL);
232}
233
234static struct prototype *
235lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
Petr Machata3d083b62012-11-22 15:14:45 +0100236{
Petr Machatab8f0d8b2013-10-16 14:25:45 +0200237 if (libsym->proto != NULL)
238 return libsym->proto;
239
Petr Machata08cdf322012-12-05 01:46:13 +0100240 struct library *lib = libsym->lib;
241 if (lib != NULL) {
Petr Machataa94d9df2012-12-17 04:41:16 +0100242 struct find_proto_data data = { libsym->name };
243 data.ret = library_get_prototype(lib, libsym->name);
244 if (data.ret == NULL
245 && libsym->plt_type == LS_TOPLT_EXEC)
246 proc_each_library(proc, NULL, find_proto_cb, &data);
247 if (data.ret != NULL)
248 return data.ret;
Petr Machata08cdf322012-12-05 01:46:13 +0100249 }
250
Petr Machataebc56a72012-12-17 04:37:57 +0100251 return build_default_prototype();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100252}
253
Juan Cespedesf1350522008-12-16 18:19:58 +0100254void
Petr Machata929bd572012-12-17 03:20:34 +0100255output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200256{
257 if (options.summary)
258 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200259
Petr Machataefc94362012-10-27 00:49:05 +0200260 if (current_proc != NULL) {
261 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100262 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200263 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100264 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100265 }
Petr Machataefc94362012-10-27 00:49:05 +0200266 current_proc = NULL;
267 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200268 return;
Petr Machataefc94362012-10-27 00:49:05 +0200269
Petr Machata37b73c02012-01-06 16:00:25 +0100270 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100271
Petr Machataefc94362012-10-27 00:49:05 +0200272 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200273 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100274 vfprintf(options.output, fmt, args);
275 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200276 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200277
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279}
280
Juan Cespedesf1350522008-12-16 18:19:58 +0100281static void
282tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100284 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100285 }
286}
287
Petr Machataf6ec08a2012-01-06 16:58:54 +0100288static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100289output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100290{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100291 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100292}
293
294static int
Petr Machata929bd572012-12-17 03:20:34 +0100295fetch_simple_param(enum tof type, struct process *proc,
296 struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100297 struct value_dict *arguments,
298 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100299 struct value *valuep)
300{
301 /* Arrays decay into pointers per C standard. We check for
302 * this here, because here we also capture arrays that come
303 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100304 if (info->type == ARGTYPE_ARRAY) {
305 struct arg_type_info *tmp = malloc(sizeof(*tmp));
306 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100307 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100308 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100309 info = tmp;
310 own = 1;
311 }
312 }
313
314 struct value value;
315 value_init(&value, proc, NULL, info, own);
316 if (fetch_arg_next(context, type, proc, info, &value) < 0)
317 return -1;
318
319 if (val_dict_push_next(arguments, &value) < 0) {
320 value_destroy(&value);
321 return -1;
322 }
323
324 if (valuep != NULL)
325 *valuep = value;
326
327 return 0;
328}
329
330static void
331fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
332{
333 if (*params_leftp == -1)
334 *params_leftp = val_dict_count(arguments);
335}
336
337static int
Petr Machata929bd572012-12-17 03:20:34 +0100338fetch_param_pack(enum tof type, struct process *proc,
339 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100340 struct value_dict *arguments, struct param *param,
341 ssize_t *params_leftp)
342{
343 struct param_enum *e = param_pack_init(param, arguments);
344 if (e == NULL)
345 return -1;
346
347 int ret = 0;
348 while (1) {
349 int insert_stop = 0;
350 struct arg_type_info *info = malloc(sizeof(*info));
351 if (info == NULL
352 || param_pack_next(param, e, info, &insert_stop) < 0) {
353 fail:
354 free(info);
355 ret = -1;
356 break;
357 }
358
359 if (insert_stop)
360 fetch_param_stop(arguments, params_leftp);
361
Petr Machatabc58f2a2012-10-30 00:25:50 +0100362 if (info->type == ARGTYPE_VOID) {
363 type_destroy(info);
364 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100365 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100366 }
Petr Machata865303f2012-01-06 18:40:38 +0100367
368 struct value val;
369 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100370 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100371 goto fail;
372
373 int stop = 0;
374 switch (param_pack_stop(param, e, &val)) {
375 case PPCB_ERR:
376 goto fail;
377 case PPCB_STOP:
378 stop = 1;
379 case PPCB_CONT:
380 break;
381 }
382
383 if (stop)
384 break;
385 }
386
387 param_pack_done(param, e);
388 return ret;
389}
390
391static int
Petr Machata929bd572012-12-17 03:20:34 +0100392fetch_one_param(enum tof type, struct process *proc,
393 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100394 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100395 ssize_t *params_leftp)
396{
Petr Machata865303f2012-01-06 18:40:38 +0100397 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200398 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100399 case PARAM_FLAVOR_TYPE:
400 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100401 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100402
403 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200404 if (fetch_param_pack_start(context,
405 param->u.pack.ppflavor) < 0)
406 return -1;
407 rc = fetch_param_pack(type, proc, context, arguments,
408 param, params_leftp);
409 fetch_param_pack_end(context);
410 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100411
412 case PARAM_FLAVOR_STOP:
413 fetch_param_stop(arguments, params_leftp);
414 return 0;
415 }
416
417 assert(!"Invalid param flavor!");
418 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100419}
420
Petr Machataebc56a72012-12-17 04:37:57 +0100421struct fetch_one_param_data
422{
423 struct process *proc;
424 struct fetch_context *context;
425 struct value_dict *arguments;
426 ssize_t *params_leftp;
427 enum tof tof;
428};
429
430static enum callback_status
431fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
432{
433 struct fetch_one_param_data *cb_data = data;
Petr Machataa94d9df2012-12-17 04:41:16 +0100434 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
435 cb_data->context,
436 cb_data->arguments, param,
437 cb_data->params_leftp) < 0);
Petr Machataebc56a72012-12-17 04:37:57 +0100438}
439
Petr Machataf6ec08a2012-01-06 16:58:54 +0100440static int
Petr Machata929bd572012-12-17 03:20:34 +0100441fetch_params(enum tof type, struct process *proc,
442 struct fetch_context *context,
Petr Machata3d083b62012-11-22 15:14:45 +0100443 struct value_dict *arguments, struct prototype *func,
444 ssize_t *params_leftp)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100445{
Petr Machataebc56a72012-12-17 04:37:57 +0100446 struct fetch_one_param_data cb_data
447 = { proc, context, arguments, params_leftp, type };
448 if (prototype_each_param(func, NULL,
449 &fetch_one_param_cb, &cb_data) != NULL)
450 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100451
452 /* Implicit stop at the end of parameter list. */
453 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100454
Petr Machataf6ec08a2012-01-06 16:58:54 +0100455 return 0;
456}
457
Petr Machataf7c46bb2012-11-01 22:15:45 +0100458struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100459{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100460 struct value *value;
461 struct value_dict *arguments;
462};
463
464static int
465format_argument_cb(FILE *stream, void *ptr)
466{
467 struct format_argument_data *data = ptr;
468 int o = format_argument(stream, data->value, data->arguments);
469 if (o < 0)
470 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100471 return o;
472}
473
474static int
475output_params(struct value_dict *arguments, size_t start, size_t end,
476 int *need_delimp)
477{
478 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100479 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100480 struct value *value = val_dict_get_num(arguments, i);
481 if (value == NULL)
482 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100483
484 struct format_argument_data data = { value, arguments };
485 int o = delim_output(options.output, need_delimp,
486 format_argument_cb, &data);
487 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100488 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100489 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100490 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100491 return 0;
492}
493
Juan Cespedesf1350522008-12-16 18:19:58 +0100494void
Petr Machata929bd572012-12-17 03:20:34 +0100495output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100496 struct library_symbol *libsym)
497{
Petr Machata8a730f32013-11-21 20:43:51 +0100498 assert(! options.summary);
499
Paul Gilliam76c61f12006-06-14 06:55:21 +0200500 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100501 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100502 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100503 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200504 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100505 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100506 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200507 if (!options.hide_caller && libsym->lib != NULL
508 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100509 /* We don't terribly mind failing this. */
510 account_output(&current_column,
511 fprintf(options.output, "%s->",
512 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100513
Petr Machata08cdf322012-12-05 01:46:13 +0100514 const char *name = libsym->name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100515#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100516 if (options.demangle)
Petr Machata08cdf322012-12-05 01:46:13 +0100517 name = my_demangle(libsym->name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200518#endif
Petr Machataadec2012012-11-01 22:05:04 +0100519 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100520 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100521 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100522
Petr Machata09654202012-12-05 17:07:21 +0100523 if (libsym->lib != NULL
524 && libsym->lib->type != LT_LIBTYPE_MAIN
525 && libsym->plt_type == LS_TOPLT_NONE
526 && account_output(&current_column,
527 fprintf(options.output, "@%s",
528 libsym->lib->soname)) < 0)
529 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100530 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100531
Petr Machata09654202012-12-05 17:07:21 +0100532 account_output(&current_column, fprintf(options.output, "("));
533
Petr Machataa94d9df2012-12-17 04:41:16 +0100534 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machata09654202012-12-05 17:07:21 +0100535 if (func == NULL) {
Petr Machata1b02c732013-01-07 18:22:51 +0100536 fail:
Petr Machata09654202012-12-05 17:07:21 +0100537 account_output(&current_column, fprintf(options.output, "???"));
538 return;
539 }
540
Petr Machataf6ec08a2012-01-06 16:58:54 +0100541 struct fetch_context *context = fetch_arg_init(type, proc,
542 func->return_info);
Petr Machata1b02c732013-01-07 18:22:51 +0100543 if (context == NULL)
544 goto fail;
545
Petr Machata94078ec2012-01-05 18:07:02 +0100546 struct value_dict *arguments = malloc(sizeof(*arguments));
Petr Machata1b02c732013-01-07 18:22:51 +0100547 if (arguments == NULL) {
548 fetch_arg_done(context);
549 goto fail;
550 }
Petr Machata94078ec2012-01-05 18:07:02 +0100551 val_dict_init(arguments);
552
Petr Machataf6ec08a2012-01-06 16:58:54 +0100553 ssize_t params_left = -1;
554 int need_delim = 0;
555 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
556 || output_params(arguments, 0, params_left, &need_delim) < 0) {
557 val_dict_destroy(arguments);
558 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100559 arguments = NULL;
560 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100561 }
Petr Machata94078ec2012-01-05 18:07:02 +0100562
563 struct callstack_element *stel
564 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100565 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100566 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100567 stel->out.params_left = params_left;
568 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100569}
570
Mark Wielaarddfefa9f2014-01-07 21:00:44 +0100571#if defined(HAVE_LIBDW)
572/* Prints information about one frame of a thread. Called by
573 dwfl_getthread_frames in output_right. Returns 1 when done (max
574 number of frames reached). Returns -1 on error. Returns 0 on
575 success (if there are more frames in the thread, call us again). */
576static int
577frame_callback (Dwfl_Frame *state, void *arg)
578{
579 Dwarf_Addr pc;
580 bool isactivation;
581
582 int *frames = (int *) arg;
583
584 if (!dwfl_frame_pc(state, &pc, &isactivation))
585 return -1;
586
587 if (!isactivation)
588 pc--;
589
590 Dwfl *dwfl = dwfl_thread_dwfl(dwfl_frame_thread(state));
591 Dwfl_Module *mod = dwfl_addrmodule(dwfl, pc);
592 const char *modname = NULL;
593 const char *symname = NULL;
594 GElf_Off off = 0;
595 if (mod != NULL) {
596 GElf_Sym sym;
597 modname = dwfl_module_info(mod, NULL, NULL, NULL, NULL,
598 NULL, NULL, NULL);
599 symname = dwfl_module_addrinfo(mod, pc, &off, &sym,
600 NULL, NULL, NULL);
601 }
602
603 /* This mimics the output produced by libunwind below. */
604 fprintf(options.output, " > %s(%s+0x%" PRIx64 ") [%" PRIx64 "]\n",
605 modname, symname, off, pc);
606
607 /* See if we can extract the source line too and print it on
608 the next line if we can find it. */
609 if (mod != NULL) {
610 Dwfl_Line *l = dwfl_module_getsrc(mod, pc);
611 if (l != NULL) {
612 int line, col;
613 line = col = -1;
614 const char *src = dwfl_lineinfo(l, NULL, &line, &col,
615 NULL, NULL);
616 if (src != NULL) {
617 fprintf(options.output, "\t%s", src);
618 if (line > 0) {
619 fprintf(options.output, ":%d", line);
620 if (col > 0)
621 fprintf(options.output,
622 ":%d", col);
623 }
624 fprintf(options.output, "\n");
625 }
626
627 }
628 }
629
630 /* Max number of frames to print reached? */
631 if ((*frames)-- == 0)
632 return 1;
633
634 return 0;
635}
636#endif /* defined(HAVE_LIBDW) */
637
Juan Cespedesf1350522008-12-16 18:19:58 +0100638void
Petr Machata8a730f32013-11-21 20:43:51 +0100639output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
640 struct timedelta *spent)
Petr Machata14184b32012-02-10 13:10:26 +0100641{
Petr Machata8a730f32013-11-21 20:43:51 +0100642 assert(! options.summary);
643
Petr Machataa94d9df2012-12-17 04:41:16 +0100644 struct prototype *func = lookup_symbol_prototype(proc, libsym);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100645 if (func == NULL)
646 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100647
Petr Machata8a730f32013-11-21 20:43:51 +0100648 if (current_proc != NULL
649 && (current_proc != proc
650 || current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100651 fprintf(options.output, " <unfinished ...>\n");
Petr Machata8a730f32013-11-21 20:43:51 +0100652 current_proc = NULL;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200653 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200654 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100655 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100656#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100657 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100658 fprintf(options.output, "<... %s resumed> ",
Petr Machata08cdf322012-12-05 01:46:13 +0100659 options.demangle ? my_demangle(libsym->name)
660 : libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200661#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100662 current_column +=
Petr Machata08cdf322012-12-05 01:46:13 +0100663 fprintf(options.output, "<... %s resumed> ", libsym->name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200664#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100665 }
666
Petr Machata94078ec2012-01-05 18:07:02 +0100667 struct callstack_element *stel
668 = &proc->callstack[proc->callstack_depth - 1];
669
Petr Machataf6ec08a2012-01-06 16:58:54 +0100670 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100671
Petr Machataf6ec08a2012-01-06 16:58:54 +0100672 /* Fetch & enter into dictionary the retval first, so that
673 * other values can use it in expressions. */
674 struct value retval;
Petr Machata0f6f30c2014-01-07 11:57:36 +0100675 bool own_retval = false;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100676 if (context != NULL) {
677 value_init(&retval, proc, NULL, func->return_info, 0);
Petr Machata0f6f30c2014-01-07 11:57:36 +0100678 own_retval = true;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100679 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200680 &retval) < 0)
681 value_set_type(&retval, NULL, 0);
682 else if (stel->arguments != NULL
Petr Machata0f6f30c2014-01-07 11:57:36 +0100683 && val_dict_push_named(stel->arguments, &retval,
684 "retval", 0) == 0)
685 own_retval = false;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200686 }
Petr Machata94078ec2012-01-05 18:07:02 +0100687
Petr Machataf6ec08a2012-01-06 16:58:54 +0100688 if (stel->arguments != NULL)
689 output_params(stel->arguments, stel->out.params_left,
690 val_dict_count(stel->arguments),
691 &stel->out.need_delim);
692
693 current_column += fprintf(options.output, ") ");
694 tabto(options.align - 1);
695 fprintf(options.output, "= ");
696
Petr Machataf7c46bb2012-11-01 22:15:45 +0100697 if (context != NULL && retval.type != NULL) {
698 struct format_argument_data data = { &retval, stel->arguments };
699 format_argument_cb(options.output, &data);
700 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100701
702 if (own_retval)
703 value_destroy(&retval);
704
Juan Cespedesd65efa32003-02-03 00:22:30 +0100705 if (opt_T) {
Petr Machata8a730f32013-11-21 20:43:51 +0100706 assert(spent != NULL);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100707 fprintf(options.output, " <%lu.%06d>",
Petr Machata8a730f32013-11-21 20:43:51 +0100708 (unsigned long) spent->tm.tv_sec,
709 (int) spent->tm.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100710 }
Petr Machata8a730f32013-11-21 20:43:51 +0100711
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100712 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700713
714#if defined(HAVE_LIBUNWIND)
Petr Machataaf1e6032013-01-06 17:16:24 +0100715 if (options.bt_depth > 0
716 && proc->unwind_priv != NULL
717 && proc->unwind_as != NULL) {
Joe Damatoab3b72c2010-10-31 00:21:53 -0700718 unw_cursor_t cursor;
Luca Clementi29276482013-10-17 19:48:38 -0700719 arch_addr_t ip, function_offset;
Luca Clementi29276482013-10-17 19:48:38 -0700720 struct library *lib = NULL;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700721 int unwind_depth = options.bt_depth;
722 char fn_name[100];
Luca Clementi29276482013-10-17 19:48:38 -0700723 const char *lib_name;
724 size_t distance;
Joe Damatoab3b72c2010-10-31 00:21:53 -0700725
Luca Clementi29276482013-10-17 19:48:38 -0700726 /* Verify that we can safely cast arch_addr_t* to
727 * unw_word_t*. */
Mohamad Ayyash55720812014-04-07 18:31:23 -0700728 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
Luca Clementi29276482013-10-17 19:48:38 -0700729 != sizeof(arch_addr_t))]);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700730 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
731 while (unwind_depth) {
Luca Clementiaf452c62014-01-03 22:05:03 -0800732
Petr Machata0f6f30c2014-01-07 11:57:36 +0100733 int rc = unw_get_reg(&cursor, UNW_REG_IP,
734 (unw_word_t *) &ip);
735 if (rc < 0) {
Luca Clementi5d3be332014-01-07 00:00:59 -0800736 fprintf(options.output, " > Error: %s\n",
Petr Machata0f6f30c2014-01-07 11:57:36 +0100737 unw_strerror(rc));
Luca Clementi5d3be332014-01-07 00:00:59 -0800738 goto cont;
Luca Clementiaf452c62014-01-03 22:05:03 -0800739 }
Luca Clementi29276482013-10-17 19:48:38 -0700740
741 /* We are looking for the library with the base address
742 * closest to the current ip. */
743 lib_name = "unmapped_area";
744 distance = (size_t) -1;
745 lib = proc->libraries;
746 while (lib != NULL) {
747 /* N.B.: Assumes sizeof(size_t) ==
748 * sizeof(arch_addr_t).
749 * Keyword: double cast. */
750 if ((ip >= lib->base) &&
751 ((size_t)(ip - lib->base)
752 < distance)) {
753 distance = ip - lib->base;
754 lib_name = lib->pathname;
755 }
756 lib = lib->next;
757 }
758
Petr Machata0f6f30c2014-01-07 11:57:36 +0100759 rc = unw_get_proc_name(&cursor, fn_name,
760 sizeof(fn_name),
761 (unw_word_t *) &function_offset);
762 if (rc == 0 || rc == -UNW_ENOMEM)
Luca Clementiaf452c62014-01-03 22:05:03 -0800763 fprintf(options.output, " > %s(%s+%p) [%p]\n",
Luca Clementi29276482013-10-17 19:48:38 -0700764 lib_name, fn_name, function_offset, ip);
Luca Clementiaf452c62014-01-03 22:05:03 -0800765 else
766 fprintf(options.output, " > %s(??\?) [%p]\n",
767 lib_name, ip);
Luca Clementi29276482013-10-17 19:48:38 -0700768
Luca Clementi5d3be332014-01-07 00:00:59 -0800769 cont:
Joe Damatoab3b72c2010-10-31 00:21:53 -0700770 if (unw_step(&cursor) <= 0)
771 break;
772 unwind_depth--;
773 }
774 fprintf(options.output, "\n");
775 }
776#endif /* defined(HAVE_LIBUNWIND) */
777
Mark Wielaarddfefa9f2014-01-07 21:00:44 +0100778#if defined(HAVE_LIBDW)
779 if (options.bt_depth > 0 && proc->leader->dwfl != NULL) {
780 int frames = options.bt_depth;
781 if (dwfl_getthread_frames(proc->leader->dwfl, proc->pid,
782 frame_callback, &frames) < 0) {
783 // Only print an error if we couldn't show anything.
784 // Otherwise just show there might be more...
785 if (frames == options.bt_depth)
786 fprintf(stderr,
787 "dwfl_getthread_frames tid %d: %s\n",
788 proc->pid, dwfl_errmsg(-1));
789 else
790 fprintf(options.output, " > [...]\n");
791 }
792 fprintf(options.output, "\n");
793 }
794#endif /* defined(HAVE_LIBDW) */
795
Petr Machata8a730f32013-11-21 20:43:51 +0100796 current_proc = NULL;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100797 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100798}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100799
Petr Machataadec2012012-11-01 22:05:04 +0100800int
Petr Machata9c400982012-11-01 22:15:01 +0100801delim_output(FILE *stream, int *need_delimp,
802 int (*writer)(FILE *stream, void *data),
803 void *data)
804{
805 int o;
806
807 /* If we don't need a delimiter, then we don't need to go
808 * through a temporary stream. It's all the same whether
809 * WRITER emits anything or not. */
810 if (!*need_delimp) {
811 o = writer(stream, data);
812
813 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100814 struct memstream ms;
815 if (memstream_init(&ms) < 0)
816 return -1;
817 o = writer(ms.stream, data);
818 if (memstream_close(&ms) < 0)
819 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100820 if (o > 0 && ((*need_delimp
821 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100822 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100823 o = -1;
824
Petr Machata2ae374a2012-12-08 05:08:48 +0100825 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100826 }
827
828 if (o < 0)
829 return -1;
830
831 *need_delimp = *need_delimp || o > 0;
832 return o;
833}
834
835int
Petr Machataadec2012012-11-01 22:05:04 +0100836account_output(int *countp, int c)
837{
838 if (c > 0)
839 *countp += c;
840 return c;
841}
842
Petr Machatad71cf2d2012-01-05 18:28:31 +0100843static void
844do_report(const char *filename, unsigned line_no, const char *severity,
845 const char *fmt, va_list args)
846{
847 char buf[128];
848 vsnprintf(buf, sizeof(buf), fmt, args);
849 buf[sizeof(buf) - 1] = 0;
850 if (filename != NULL)
851 output_line(0, "%s:%d: %s: %s",
852 filename, line_no, severity, buf);
853 else
854 output_line(0, "%s: %s", severity, buf);
855}
856
857void
Petr Machata89c5ca22012-10-27 00:49:38 +0200858report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100859{
860 va_list args;
861 va_start(args, fmt);
862 do_report(filename, line_no, "error", fmt, args);
863 va_end(args);
864}
865
866void
Petr Machata89c5ca22012-10-27 00:49:38 +0200867report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100868{
869 va_list args;
870 va_start(args, fmt);
871 do_report(filename, line_no, "warning", fmt, args);
872 va_end(args);
873}
874
875void
Petr Machata89c5ca22012-10-27 00:49:38 +0200876report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100877{
878 va_list args;
879 va_start(args, fmt);
880 do_report(NULL, 0, "error", fmt, args);
881 va_end(args);
882}