blob: bbd65bc87a800fda3f80ff0528602fb646cb3aa0 [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
2 * This file is part of ltrace.
Petr Machata98ff3092013-03-08 22:11:36 +01003 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata94078ec2012-01-05 18:07:02 +01004 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1997,1998,1999,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes
Petr Machatae99af272012-10-26 00:29:52 +02006 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
Petr Machata94078ec2012-01-05 18:07:02 +01007 * Copyright (C) 2006 Ian Wienand
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
Juan Cespedesac3db291998-04-25 14:31:58 +020025#include "config.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020026
Juan Cespedes3268a161997-08-25 16:45:22 +020027#include <stdio.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +010028#include <stdlib.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020029#include <stdarg.h>
Juan Cespedes1cd999a2001-07-03 00:46:04 +020030#include <string.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020031#include <time.h>
32#include <sys/time.h>
33#include <unistd.h>
Petr Machataf6ec08a2012-01-06 16:58:54 +010034#include <errno.h>
Petr Machata865303f2012-01-06 18:40:38 +010035#include <assert.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020036
Juan Cespedesf7281232009-06-25 16:11:21 +020037#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010038#include "proc.h"
Petr Machata29add4f2012-02-18 16:38:05 +010039#include "library.h"
Petr Machata000e3112012-01-03 17:03:39 +010040#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010041#include "value.h"
42#include "value_dict.h"
Petr Machata865303f2012-01-06 18:40:38 +010043#include "param.h"
Petr Machataf6ec08a2012-01-06 16:58:54 +010044#include "fetch.h"
Petr Machatab7819162012-01-09 04:26:15 +010045#include "lens_default.h"
Petr Machata2ae374a2012-12-08 05:08:48 +010046#include "memstream.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020047
Juan Cespedesf7281232009-06-25 16:11:21 +020048/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010049extern struct timeval current_time_spent;
50
Petr Machatad7e4ca82012-11-28 03:38:47 +010051struct dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010052
Petr Machata929bd572012-12-17 03:20:34 +010053static struct process *current_proc = 0;
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;
78 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020080 struct timeval diff;
81
82 gettimeofday(&tv, &tz);
83
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
85 old_tv.tv_sec = tv.tv_sec;
86 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020087 }
88 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
89 if (tv.tv_usec >= old_tv.tv_usec) {
90 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
91 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020092 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020093 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
94 }
95 old_tv.tv_sec = tv.tv_sec;
96 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010097 current_column += fprintf(options.output, "%3lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +010098 (unsigned long)diff.tv_sec,
99 (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +0200100 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200101 if (opt_t) {
102 struct timeval tv;
103 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200104
105 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100107 current_column += fprintf(options.output, "%lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100108 (unsigned long)tv.tv_sec,
109 (int)tv.tv_usec);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 } else if (opt_t > 1) {
111 struct tm *tmp = localtime(&tv.tv_sec);
112 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100113 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
115 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200116 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100117 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100118 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119 tmp->tm_hour, tmp->tm_min,
120 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200121 }
122 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100123 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100124 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100125 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100126 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100127 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100128 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100129 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100130 }
Petr Machata37b73c02012-01-06 16:00:25 +0100131 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200132 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200133 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200134}
135
Petr Machatab7819162012-01-09 04:26:15 +0100136static struct arg_type_info *
137get_unknown_type(void)
138{
139 static struct arg_type_info *info = NULL;
140 if (info == NULL) {
141 info = malloc(sizeof(*info));
142 if (info == NULL) {
143 report_global_error("malloc: %s", strerror(errno));
144 abort();
145 }
146 *info = *type_get_simple(ARGTYPE_LONG);
147 info->lens = &guess_lens;
148 }
149 return info;
150}
151
Petr Machataf6ec08a2012-01-06 16:58:54 +0100152/* The default prototype is: long X(long, long, long, long). */
Petr Machata3d083b62012-11-22 15:14:45 +0100153static struct prototype *
Petr Machataf6ec08a2012-01-06 16:58:54 +0100154build_default_prototype(void)
155{
Petr Machata3d083b62012-11-22 15:14:45 +0100156 struct prototype *ret = malloc(sizeof(*ret));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100157 size_t i = 0;
158 if (ret == NULL)
159 goto err;
160 memset(ret, 0, sizeof(*ret));
161
Petr Machatab7819162012-01-09 04:26:15 +0100162 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100163
164 ret->return_info = unknown_type;
Petr Machata3a9bf6d2012-01-06 21:44:10 +0100165 ret->own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100166
167 ret->num_params = 4;
Petr Machata865303f2012-01-06 18:40:38 +0100168 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
169 if (ret->params == NULL)
170 goto err;
171
172 for (i = 0; i < ret->num_params; ++i)
173 param_init_type(&ret->params[i], unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100174
175 return ret;
176
177err:
178 report_global_error("malloc: %s", strerror(errno));
Petr Machata865303f2012-01-06 18:40:38 +0100179 if (ret->params != NULL) {
180 while (i-- > 0)
181 param_destroy(&ret->params[i]);
182 free(ret->params);
183 }
184
Petr Machataf6ec08a2012-01-06 16:58:54 +0100185 free(ret);
186
187 return NULL;
188}
189
Petr Machata3d083b62012-11-22 15:14:45 +0100190static struct prototype *
191name2func(char const *name)
192{
193 struct prototype *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100194 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195
Petr Machataf6ec08a2012-01-06 16:58:54 +0100196 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200197 str1 = tmp->name;
198 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100199 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100201 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100202
Petr Machata3d083b62012-11-22 15:14:45 +0100203 static struct prototype *def = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100204 if (def == NULL)
205 def = build_default_prototype();
206
207 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100208}
209
Juan Cespedesf1350522008-12-16 18:19:58 +0100210void
Petr Machata929bd572012-12-17 03:20:34 +0100211output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200212{
213 if (options.summary)
214 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200215
Petr Machataefc94362012-10-27 00:49:05 +0200216 if (current_proc != NULL) {
217 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100218 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200219 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100220 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100221 }
Petr Machataefc94362012-10-27 00:49:05 +0200222 current_proc = NULL;
223 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200224 return;
Petr Machataefc94362012-10-27 00:49:05 +0200225
Petr Machata37b73c02012-01-06 16:00:25 +0100226 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100227
Petr Machataefc94362012-10-27 00:49:05 +0200228 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200229 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100230 vfprintf(options.output, fmt, args);
231 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200232 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200233
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100234 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100235}
236
Juan Cespedesf1350522008-12-16 18:19:58 +0100237static void
238tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100239 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100240 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100241 }
242}
243
Petr Machataf6ec08a2012-01-06 16:58:54 +0100244static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100245output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100246{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100247 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100248}
249
250static int
Petr Machata929bd572012-12-17 03:20:34 +0100251fetch_simple_param(enum tof type, struct process *proc,
252 struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100253 struct value_dict *arguments,
254 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100255 struct value *valuep)
256{
257 /* Arrays decay into pointers per C standard. We check for
258 * this here, because here we also capture arrays that come
259 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100260 if (info->type == ARGTYPE_ARRAY) {
261 struct arg_type_info *tmp = malloc(sizeof(*tmp));
262 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100263 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100264 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100265 info = tmp;
266 own = 1;
267 }
268 }
269
270 struct value value;
271 value_init(&value, proc, NULL, info, own);
272 if (fetch_arg_next(context, type, proc, info, &value) < 0)
273 return -1;
274
275 if (val_dict_push_next(arguments, &value) < 0) {
276 value_destroy(&value);
277 return -1;
278 }
279
280 if (valuep != NULL)
281 *valuep = value;
282
283 return 0;
284}
285
286static void
287fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
288{
289 if (*params_leftp == -1)
290 *params_leftp = val_dict_count(arguments);
291}
292
293static int
Petr Machata929bd572012-12-17 03:20:34 +0100294fetch_param_pack(enum tof type, struct process *proc,
295 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100296 struct value_dict *arguments, struct param *param,
297 ssize_t *params_leftp)
298{
299 struct param_enum *e = param_pack_init(param, arguments);
300 if (e == NULL)
301 return -1;
302
303 int ret = 0;
304 while (1) {
305 int insert_stop = 0;
306 struct arg_type_info *info = malloc(sizeof(*info));
307 if (info == NULL
308 || param_pack_next(param, e, info, &insert_stop) < 0) {
309 fail:
310 free(info);
311 ret = -1;
312 break;
313 }
314
315 if (insert_stop)
316 fetch_param_stop(arguments, params_leftp);
317
Petr Machatabc58f2a2012-10-30 00:25:50 +0100318 if (info->type == ARGTYPE_VOID) {
319 type_destroy(info);
320 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100321 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100322 }
Petr Machata865303f2012-01-06 18:40:38 +0100323
324 struct value val;
325 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100326 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100327 goto fail;
328
329 int stop = 0;
330 switch (param_pack_stop(param, e, &val)) {
331 case PPCB_ERR:
332 goto fail;
333 case PPCB_STOP:
334 stop = 1;
335 case PPCB_CONT:
336 break;
337 }
338
339 if (stop)
340 break;
341 }
342
343 param_pack_done(param, e);
344 return ret;
345}
346
347static int
Petr Machata929bd572012-12-17 03:20:34 +0100348fetch_one_param(enum tof type, struct process *proc,
349 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100350 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100351 ssize_t *params_leftp)
352{
Petr Machata865303f2012-01-06 18:40:38 +0100353 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200354 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100355 case PARAM_FLAVOR_TYPE:
356 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100357 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100358
359 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200360 if (fetch_param_pack_start(context,
361 param->u.pack.ppflavor) < 0)
362 return -1;
363 rc = fetch_param_pack(type, proc, context, arguments,
364 param, params_leftp);
365 fetch_param_pack_end(context);
366 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100367
368 case PARAM_FLAVOR_STOP:
369 fetch_param_stop(arguments, params_leftp);
370 return 0;
371 }
372
373 assert(!"Invalid param flavor!");
374 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100375}
376
377static int
Petr Machata929bd572012-12-17 03:20:34 +0100378fetch_params(enum tof type, struct process *proc,
379 struct fetch_context *context,
Petr Machata3d083b62012-11-22 15:14:45 +0100380 struct value_dict *arguments, struct prototype *func,
381 ssize_t *params_leftp)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100382{
383 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100384 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100385 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100386 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100387 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100388
389 /* Implicit stop at the end of parameter list. */
390 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100391
Petr Machataf6ec08a2012-01-06 16:58:54 +0100392 return 0;
393}
394
Petr Machataf7c46bb2012-11-01 22:15:45 +0100395struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100396{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100397 struct value *value;
398 struct value_dict *arguments;
399};
400
401static int
402format_argument_cb(FILE *stream, void *ptr)
403{
404 struct format_argument_data *data = ptr;
405 int o = format_argument(stream, data->value, data->arguments);
406 if (o < 0)
407 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100408 return o;
409}
410
411static int
412output_params(struct value_dict *arguments, size_t start, size_t end,
413 int *need_delimp)
414{
415 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100416 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100417 struct value *value = val_dict_get_num(arguments, i);
418 if (value == NULL)
419 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100420
421 struct format_argument_data data = { value, arguments };
422 int o = delim_output(options.output, need_delimp,
423 format_argument_cb, &data);
424 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100425 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100426 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100428 return 0;
429}
430
Juan Cespedesf1350522008-12-16 18:19:58 +0100431void
Petr Machata929bd572012-12-17 03:20:34 +0100432output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100433 struct library_symbol *libsym)
434{
435 const char *function_name = libsym->name;
Petr Machata3d083b62012-11-22 15:14:45 +0100436 struct prototype *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100437
Juan Cespedesda9b9532009-04-07 15:33:50 +0200438 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100439 return;
440 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200441 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100442 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100443 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100444 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200445 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100446 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100447 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200448 if (!options.hide_caller && libsym->lib != NULL
449 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100450 /* We don't terribly mind failing this. */
451 account_output(&current_column,
452 fprintf(options.output, "%s->",
453 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100454
455 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100456#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100457 if (options.demangle)
458 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200459#endif
Petr Machataadec2012012-11-01 22:05:04 +0100460 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100461 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100462 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100463
Petr Machata09654202012-12-05 17:07:21 +0100464 if (libsym->lib != NULL
465 && libsym->lib->type != LT_LIBTYPE_MAIN
466 && libsym->plt_type == LS_TOPLT_NONE
467 && account_output(&current_column,
468 fprintf(options.output, "@%s",
469 libsym->lib->soname)) < 0)
470 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100471 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100472
Petr Machata09654202012-12-05 17:07:21 +0100473 account_output(&current_column, fprintf(options.output, "("));
474
475 func = name2func(function_name);
476 if (func == NULL) {
477 account_output(&current_column, fprintf(options.output, "???"));
478 return;
479 }
480
Petr Machataf6ec08a2012-01-06 16:58:54 +0100481 struct fetch_context *context = fetch_arg_init(type, proc,
482 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100483 struct value_dict *arguments = malloc(sizeof(*arguments));
484 if (arguments == NULL)
485 return;
486 val_dict_init(arguments);
487
Petr Machataf6ec08a2012-01-06 16:58:54 +0100488 ssize_t params_left = -1;
489 int need_delim = 0;
490 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
491 || output_params(arguments, 0, params_left, &need_delim) < 0) {
492 val_dict_destroy(arguments);
493 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100494 arguments = NULL;
495 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100496 }
Petr Machata94078ec2012-01-05 18:07:02 +0100497
498 struct callstack_element *stel
499 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100500 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100501 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100502 stel->out.params_left = params_left;
503 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100504}
505
Petr Machatad7e4ca82012-11-28 03:38:47 +0100506static void
507free_stringp_cb(const char **stringp, void *data)
508{
509 free((char *)*stringp);
510}
511
Juan Cespedesf1350522008-12-16 18:19:58 +0100512void
Petr Machata929bd572012-12-17 03:20:34 +0100513output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100514{
Petr Machata29add4f2012-02-18 16:38:05 +0100515 const char *function_name = libsym->name;
Petr Machata3d083b62012-11-22 15:14:45 +0100516 struct prototype *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100517 if (func == NULL)
518 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100519
Petr Machatad7e4ca82012-11-28 03:38:47 +0100520again:
Juan Cespedesda9b9532009-04-07 15:33:50 +0200521 if (options.summary) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100522 if (dict_opt_c == NULL) {
523 dict_opt_c = malloc(sizeof(*dict_opt_c));
524 if (dict_opt_c == NULL) {
525 oom:
526 fprintf(stderr,
527 "Can't allocate memory for "
528 "keeping track of -c.\n");
529 free(dict_opt_c);
530 options.summary = 0;
531 goto again;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100532 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100533 DICT_INIT(dict_opt_c, const char *, struct opt_c_struct,
534 dict_hash_string, dict_eq_string, NULL);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100535 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100536
Petr Machata98ff3092013-03-08 22:11:36 +0100537 struct opt_c_struct *st
538 = DICT_FIND_REF(dict_opt_c, &function_name,
539 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100540 if (st == NULL) {
541 const char *na = strdup(function_name);
542 struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
543 if (na == NULL
544 || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
545 free((char *)na);
546 DICT_DESTROY(dict_opt_c, const char *,
547 struct opt_c_struct,
548 free_stringp_cb, NULL, NULL);
549 goto oom;
550 }
Petr Machata98ff3092013-03-08 22:11:36 +0100551 st = DICT_FIND_REF(dict_opt_c, &function_name,
552 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100553 assert(st != NULL);
554 }
555
Juan Cespedesd65efa32003-02-03 00:22:30 +0100556 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
557 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
558 st->tv.tv_sec++;
559 } else {
560 st->tv.tv_usec += current_time_spent.tv_usec;
561 }
562 st->count++;
563 st->tv.tv_sec += current_time_spent.tv_sec;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100564 return;
565 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100566
Paul Gilliam76c61f12006-06-14 06:55:21 +0200567 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100568 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100569 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200570 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200571 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200572 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100573 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100574#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100575 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100576 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100577 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200578#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100579 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100580 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200581#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100582 }
583
Petr Machata94078ec2012-01-05 18:07:02 +0100584 struct callstack_element *stel
585 = &proc->callstack[proc->callstack_depth - 1];
586
Petr Machataf6ec08a2012-01-06 16:58:54 +0100587 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100588
Petr Machataf6ec08a2012-01-06 16:58:54 +0100589 /* Fetch & enter into dictionary the retval first, so that
590 * other values can use it in expressions. */
591 struct value retval;
592 int own_retval = 0;
593 if (context != NULL) {
594 value_init(&retval, proc, NULL, func->return_info, 0);
595 own_retval = 1;
596 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200597 &retval) < 0)
598 value_set_type(&retval, NULL, 0);
599 else if (stel->arguments != NULL
600 && val_dict_push_named(stel->arguments, &retval,
601 "retval", 0) == 0)
602 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200603 }
Petr Machata94078ec2012-01-05 18:07:02 +0100604
Petr Machataf6ec08a2012-01-06 16:58:54 +0100605 if (stel->arguments != NULL)
606 output_params(stel->arguments, stel->out.params_left,
607 val_dict_count(stel->arguments),
608 &stel->out.need_delim);
609
610 current_column += fprintf(options.output, ") ");
611 tabto(options.align - 1);
612 fprintf(options.output, "= ");
613
Petr Machataf7c46bb2012-11-01 22:15:45 +0100614 if (context != NULL && retval.type != NULL) {
615 struct format_argument_data data = { &retval, stel->arguments };
616 format_argument_cb(options.output, &data);
617 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100618
619 if (own_retval)
620 value_destroy(&retval);
621
Juan Cespedesd65efa32003-02-03 00:22:30 +0100622 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100623 fprintf(options.output, " <%lu.%06d>",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100624 (unsigned long)current_time_spent.tv_sec,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100625 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100626 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100627 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700628
629#if defined(HAVE_LIBUNWIND)
630 if (options.bt_depth > 0) {
631 unw_cursor_t cursor;
632 unw_word_t ip, sp;
633 int unwind_depth = options.bt_depth;
634 char fn_name[100];
635
636 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
637 while (unwind_depth) {
638 unw_get_reg(&cursor, UNW_REG_IP, &ip);
639 unw_get_reg(&cursor, UNW_REG_SP, &sp);
640 unw_get_proc_name(&cursor, fn_name, 100, NULL);
641 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
642 if (unw_step(&cursor) <= 0)
643 break;
644 unwind_depth--;
645 }
646 fprintf(options.output, "\n");
647 }
648#endif /* defined(HAVE_LIBUNWIND) */
649
Paul Gilliam76c61f12006-06-14 06:55:21 +0200650 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100651 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100652}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100653
Petr Machataadec2012012-11-01 22:05:04 +0100654int
Petr Machata9c400982012-11-01 22:15:01 +0100655delim_output(FILE *stream, int *need_delimp,
656 int (*writer)(FILE *stream, void *data),
657 void *data)
658{
659 int o;
660
661 /* If we don't need a delimiter, then we don't need to go
662 * through a temporary stream. It's all the same whether
663 * WRITER emits anything or not. */
664 if (!*need_delimp) {
665 o = writer(stream, data);
666
667 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100668 struct memstream ms;
669 if (memstream_init(&ms) < 0)
670 return -1;
671 o = writer(ms.stream, data);
672 if (memstream_close(&ms) < 0)
673 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100674 if (o > 0 && ((*need_delimp
675 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100676 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100677 o = -1;
678
Petr Machata2ae374a2012-12-08 05:08:48 +0100679 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100680 }
681
682 if (o < 0)
683 return -1;
684
685 *need_delimp = *need_delimp || o > 0;
686 return o;
687}
688
689int
Petr Machataadec2012012-11-01 22:05:04 +0100690account_output(int *countp, int c)
691{
692 if (c > 0)
693 *countp += c;
694 return c;
695}
696
Petr Machatad71cf2d2012-01-05 18:28:31 +0100697static void
698do_report(const char *filename, unsigned line_no, const char *severity,
699 const char *fmt, va_list args)
700{
701 char buf[128];
702 vsnprintf(buf, sizeof(buf), fmt, args);
703 buf[sizeof(buf) - 1] = 0;
704 if (filename != NULL)
705 output_line(0, "%s:%d: %s: %s",
706 filename, line_no, severity, buf);
707 else
708 output_line(0, "%s: %s", severity, buf);
709}
710
711void
Petr Machata89c5ca22012-10-27 00:49:38 +0200712report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100713{
714 va_list args;
715 va_start(args, fmt);
716 do_report(filename, line_no, "error", fmt, args);
717 va_end(args);
718}
719
720void
Petr Machata89c5ca22012-10-27 00:49:38 +0200721report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100722{
723 va_list args;
724 va_start(args, fmt);
725 do_report(filename, line_no, "warning", fmt, args);
726 va_end(args);
727}
728
729void
Petr Machata89c5ca22012-10-27 00:49:38 +0200730report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100731{
732 va_list args;
733 va_start(args, fmt);
734 do_report(NULL, 0, "error", fmt, args);
735 va_end(args);
736}