blob: 6f6b5774ff7de6f418cee4e52baaa57655c04cfa [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 Machata3ac8db62012-11-23 18:43:10 +010039#include "demangle.h"
Petr Machata29add4f2012-02-18 16:38:05 +010040#include "library.h"
Petr Machata8bf82d02012-11-23 18:44:31 +010041#include "options.h"
Petr Machata000e3112012-01-03 17:03:39 +010042#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010043#include "value.h"
44#include "value_dict.h"
Petr Machata865303f2012-01-06 18:40:38 +010045#include "param.h"
Petr Machataf6ec08a2012-01-06 16:58:54 +010046#include "fetch.h"
Petr Machatab7819162012-01-09 04:26:15 +010047#include "lens_default.h"
Petr Machata2ae374a2012-12-08 05:08:48 +010048#include "memstream.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020049
Juan Cespedesf7281232009-06-25 16:11:21 +020050/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010051extern struct timeval current_time_spent;
52
Petr Machatad7e4ca82012-11-28 03:38:47 +010053struct dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010054
Petr Machata929bd572012-12-17 03:20:34 +010055static struct process *current_proc = 0;
Petr Machataba1664b2012-04-28 14:59:05 +020056static size_t current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010057static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020058
Juan Cespedesf1350522008-12-16 18:19:58 +010059static void
Petr Machata929bd572012-12-17 03:20:34 +010060output_indent(struct process *proc)
Petr Machata54004752012-05-03 18:36:48 +020061{
62 int d = options.indent * (proc->callstack_depth - 1);
63 current_column += fprintf(options.output, "%*s", d, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020064}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020065
Juan Cespedesf1350522008-12-16 18:19:58 +010066static void
Petr Machata929bd572012-12-17 03:20:34 +010067begin_of_line(struct process *proc, int is_func, int indent)
Petr Machata37b73c02012-01-06 16:00:25 +010068{
Juan Cespedes5e01f651998-03-08 22:31:44 +010069 current_column = 0;
70 if (!proc) {
71 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020072 }
Juan Cespedesc693f022009-05-21 18:59:41 +020073 if ((options.output != stderr) && (opt_p || options.follow)) {
74 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020075 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010076 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010077 }
Juan Cespedesf666d191998-09-20 23:04:34 +020078 if (opt_r) {
79 struct timeval tv;
80 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020082 struct timeval diff;
83
84 gettimeofday(&tv, &tz);
85
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
87 old_tv.tv_sec = tv.tv_sec;
88 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020089 }
90 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
91 if (tv.tv_usec >= old_tv.tv_usec) {
92 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
93 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020094 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020095 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
96 }
97 old_tv.tv_sec = tv.tv_sec;
98 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010099 current_column += fprintf(options.output, "%3lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100100 (unsigned long)diff.tv_sec,
101 (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +0200102 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200103 if (opt_t) {
104 struct timeval tv;
105 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200106
107 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100109 current_column += fprintf(options.output, "%lu.%06d ",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100110 (unsigned long)tv.tv_sec,
111 (int)tv.tv_usec);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 } else if (opt_t > 1) {
113 struct tm *tmp = localtime(&tv.tv_sec);
114 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100115 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100116 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
117 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200118 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100120 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100121 tmp->tm_hour, tmp->tm_min,
122 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200123 }
124 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100125 if (opt_i) {
Petr Machata37b73c02012-01-06 16:00:25 +0100126 if (is_func)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100127 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100128 proc->return_addr);
Petr Machata37b73c02012-01-06 16:00:25 +0100129 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100130 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100132 }
Petr Machata37b73c02012-01-06 16:00:25 +0100133 if (options.indent > 0 && indent) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200134 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200135 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200136}
137
Petr Machatab7819162012-01-09 04:26:15 +0100138static struct arg_type_info *
139get_unknown_type(void)
140{
141 static struct arg_type_info *info = NULL;
142 if (info == NULL) {
143 info = malloc(sizeof(*info));
144 if (info == NULL) {
145 report_global_error("malloc: %s", strerror(errno));
146 abort();
147 }
148 *info = *type_get_simple(ARGTYPE_LONG);
149 info->lens = &guess_lens;
150 }
151 return info;
152}
153
Petr Machataf6ec08a2012-01-06 16:58:54 +0100154/* The default prototype is: long X(long, long, long, long). */
Petr Machata3d083b62012-11-22 15:14:45 +0100155static struct prototype *
Petr Machataf6ec08a2012-01-06 16:58:54 +0100156build_default_prototype(void)
157{
Petr Machata3d083b62012-11-22 15:14:45 +0100158 struct prototype *ret = malloc(sizeof(*ret));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100159 size_t i = 0;
160 if (ret == NULL)
161 goto err;
162 memset(ret, 0, sizeof(*ret));
163
Petr Machatab7819162012-01-09 04:26:15 +0100164 struct arg_type_info *unknown_type = get_unknown_type();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100165
166 ret->return_info = unknown_type;
Petr Machata3a9bf6d2012-01-06 21:44:10 +0100167 ret->own_return_info = 0;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100168
169 ret->num_params = 4;
Petr Machata865303f2012-01-06 18:40:38 +0100170 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
171 if (ret->params == NULL)
172 goto err;
173
174 for (i = 0; i < ret->num_params; ++i)
175 param_init_type(&ret->params[i], unknown_type, 0);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100176
177 return ret;
178
179err:
180 report_global_error("malloc: %s", strerror(errno));
Petr Machata865303f2012-01-06 18:40:38 +0100181 if (ret->params != NULL) {
182 while (i-- > 0)
183 param_destroy(&ret->params[i]);
184 free(ret->params);
185 }
186
Petr Machataf6ec08a2012-01-06 16:58:54 +0100187 free(ret);
188
189 return NULL;
190}
191
Petr Machata3d083b62012-11-22 15:14:45 +0100192static struct prototype *
193name2func(char const *name)
194{
195 struct prototype *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100196 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197
Petr Machataf6ec08a2012-01-06 16:58:54 +0100198 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200199 str1 = tmp->name;
200 str2 = name;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100201 if (!strcmp(str1, str2))
Juan Cespedes5e01f651998-03-08 22:31:44 +0100202 return tmp;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100203 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100204
Petr Machata3d083b62012-11-22 15:14:45 +0100205 static struct prototype *def = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100206 if (def == NULL)
207 def = build_default_prototype();
208
209 return def;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100210}
211
Juan Cespedesf1350522008-12-16 18:19:58 +0100212void
Petr Machata929bd572012-12-17 03:20:34 +0100213output_line(struct process *proc, const char *fmt, ...)
Petr Machataefc94362012-10-27 00:49:05 +0200214{
215 if (options.summary)
216 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200217
Petr Machataefc94362012-10-27 00:49:05 +0200218 if (current_proc != NULL) {
219 if (current_proc->callstack[current_depth].return_addr)
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100220 fprintf(options.output, " <unfinished ...>\n");
Petr Machataefc94362012-10-27 00:49:05 +0200221 else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100222 fprintf(options.output, " <no return ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100223 }
Petr Machataefc94362012-10-27 00:49:05 +0200224 current_proc = NULL;
225 if (fmt == NULL)
Juan Cespedes28f60191998-04-12 00:04:39 +0200226 return;
Petr Machataefc94362012-10-27 00:49:05 +0200227
Petr Machata37b73c02012-01-06 16:00:25 +0100228 begin_of_line(proc, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100229
Petr Machataefc94362012-10-27 00:49:05 +0200230 va_list args;
Juan Cespedes21c63a12001-07-07 20:56:56 +0200231 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100232 vfprintf(options.output, fmt, args);
233 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200234 va_end(args);
Petr Machataefc94362012-10-27 00:49:05 +0200235
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100236 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100237}
238
Juan Cespedesf1350522008-12-16 18:19:58 +0100239static void
240tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100241 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100242 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100243 }
244}
245
Petr Machataf6ec08a2012-01-06 16:58:54 +0100246static int
Petr Machataf7c46bb2012-11-01 22:15:45 +0100247output_error(FILE *stream)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100248{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100249 return fprintf(stream, "?");
Petr Machataf6ec08a2012-01-06 16:58:54 +0100250}
251
252static int
Petr Machata929bd572012-12-17 03:20:34 +0100253fetch_simple_param(enum tof type, struct process *proc,
254 struct fetch_context *context,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100255 struct value_dict *arguments,
256 struct arg_type_info *info, int own,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100257 struct value *valuep)
258{
259 /* Arrays decay into pointers per C standard. We check for
260 * this here, because here we also capture arrays that come
261 * from parameter packs. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100262 if (info->type == ARGTYPE_ARRAY) {
263 struct arg_type_info *tmp = malloc(sizeof(*tmp));
264 if (tmp != NULL) {
Petr Machatabc58f2a2012-10-30 00:25:50 +0100265 type_init_pointer(tmp, info, own);
Petr Machatab7819162012-01-09 04:26:15 +0100266 tmp->lens = info->lens;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100267 info = tmp;
268 own = 1;
269 }
270 }
271
272 struct value value;
273 value_init(&value, proc, NULL, info, own);
274 if (fetch_arg_next(context, type, proc, info, &value) < 0)
275 return -1;
276
277 if (val_dict_push_next(arguments, &value) < 0) {
278 value_destroy(&value);
279 return -1;
280 }
281
282 if (valuep != NULL)
283 *valuep = value;
284
285 return 0;
286}
287
288static void
289fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
290{
291 if (*params_leftp == -1)
292 *params_leftp = val_dict_count(arguments);
293}
294
295static int
Petr Machata929bd572012-12-17 03:20:34 +0100296fetch_param_pack(enum tof type, struct process *proc,
297 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100298 struct value_dict *arguments, struct param *param,
299 ssize_t *params_leftp)
300{
301 struct param_enum *e = param_pack_init(param, arguments);
302 if (e == NULL)
303 return -1;
304
305 int ret = 0;
306 while (1) {
307 int insert_stop = 0;
308 struct arg_type_info *info = malloc(sizeof(*info));
309 if (info == NULL
310 || param_pack_next(param, e, info, &insert_stop) < 0) {
311 fail:
312 free(info);
313 ret = -1;
314 break;
315 }
316
317 if (insert_stop)
318 fetch_param_stop(arguments, params_leftp);
319
Petr Machatabc58f2a2012-10-30 00:25:50 +0100320 if (info->type == ARGTYPE_VOID) {
321 type_destroy(info);
322 free(info);
Petr Machata865303f2012-01-06 18:40:38 +0100323 break;
Petr Machatabc58f2a2012-10-30 00:25:50 +0100324 }
Petr Machata865303f2012-01-06 18:40:38 +0100325
326 struct value val;
327 if (fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100328 info, 1, &val) < 0)
Petr Machata865303f2012-01-06 18:40:38 +0100329 goto fail;
330
331 int stop = 0;
332 switch (param_pack_stop(param, e, &val)) {
333 case PPCB_ERR:
334 goto fail;
335 case PPCB_STOP:
336 stop = 1;
337 case PPCB_CONT:
338 break;
339 }
340
341 if (stop)
342 break;
343 }
344
345 param_pack_done(param, e);
346 return ret;
347}
348
349static int
Petr Machata929bd572012-12-17 03:20:34 +0100350fetch_one_param(enum tof type, struct process *proc,
351 struct fetch_context *context,
Petr Machata865303f2012-01-06 18:40:38 +0100352 struct value_dict *arguments, struct param *param,
Petr Machataf6ec08a2012-01-06 16:58:54 +0100353 ssize_t *params_leftp)
354{
Petr Machata865303f2012-01-06 18:40:38 +0100355 switch (param->flavor) {
Petr Machatae36298a2012-09-13 17:12:41 +0200356 int rc;
Petr Machata865303f2012-01-06 18:40:38 +0100357 case PARAM_FLAVOR_TYPE:
358 return fetch_simple_param(type, proc, context, arguments,
Petr Machatabc58f2a2012-10-30 00:25:50 +0100359 param->u.type.type, 0, NULL);
Petr Machata865303f2012-01-06 18:40:38 +0100360
361 case PARAM_FLAVOR_PACK:
Petr Machatae36298a2012-09-13 17:12:41 +0200362 if (fetch_param_pack_start(context,
363 param->u.pack.ppflavor) < 0)
364 return -1;
365 rc = fetch_param_pack(type, proc, context, arguments,
366 param, params_leftp);
367 fetch_param_pack_end(context);
368 return rc;
Petr Machata865303f2012-01-06 18:40:38 +0100369
370 case PARAM_FLAVOR_STOP:
371 fetch_param_stop(arguments, params_leftp);
372 return 0;
373 }
374
375 assert(!"Invalid param flavor!");
376 abort();
Petr Machataf6ec08a2012-01-06 16:58:54 +0100377}
378
379static int
Petr Machata929bd572012-12-17 03:20:34 +0100380fetch_params(enum tof type, struct process *proc,
381 struct fetch_context *context,
Petr Machata3d083b62012-11-22 15:14:45 +0100382 struct value_dict *arguments, struct prototype *func,
383 ssize_t *params_leftp)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100384{
385 size_t i;
Petr Machata865303f2012-01-06 18:40:38 +0100386 for (i = 0; i < func->num_params; ++i)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100387 if (fetch_one_param(type, proc, context, arguments,
Petr Machata865303f2012-01-06 18:40:38 +0100388 &func->params[i], params_leftp) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100389 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100390
391 /* Implicit stop at the end of parameter list. */
392 fetch_param_stop(arguments, params_leftp);
Petr Machata865303f2012-01-06 18:40:38 +0100393
Petr Machataf6ec08a2012-01-06 16:58:54 +0100394 return 0;
395}
396
Petr Machataf7c46bb2012-11-01 22:15:45 +0100397struct format_argument_data
Petr Machataf6ec08a2012-01-06 16:58:54 +0100398{
Petr Machataf7c46bb2012-11-01 22:15:45 +0100399 struct value *value;
400 struct value_dict *arguments;
401};
402
403static int
404format_argument_cb(FILE *stream, void *ptr)
405{
406 struct format_argument_data *data = ptr;
407 int o = format_argument(stream, data->value, data->arguments);
408 if (o < 0)
409 o = output_error(stream);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100410 return o;
411}
412
413static int
414output_params(struct value_dict *arguments, size_t start, size_t end,
415 int *need_delimp)
416{
417 size_t i;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100418 for (i = start; i < end; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100419 struct value *value = val_dict_get_num(arguments, i);
420 if (value == NULL)
421 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100422
423 struct format_argument_data data = { value, arguments };
424 int o = delim_output(options.output, need_delimp,
425 format_argument_cb, &data);
426 if (o < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100428 current_column += o;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100429 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100430 return 0;
431}
432
Juan Cespedesf1350522008-12-16 18:19:58 +0100433void
Petr Machata929bd572012-12-17 03:20:34 +0100434output_left(enum tof type, struct process *proc,
Petr Machata29add4f2012-02-18 16:38:05 +0100435 struct library_symbol *libsym)
436{
437 const char *function_name = libsym->name;
Petr Machata3d083b62012-11-22 15:14:45 +0100438 struct prototype *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100439
Juan Cespedesda9b9532009-04-07 15:33:50 +0200440 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100441 return;
442 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200443 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100444 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100445 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100446 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200447 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100448 current_depth = proc->callstack_depth;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100449 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
Petr Machata53bc49b2012-04-25 17:23:02 +0200450 if (!options.hide_caller && libsym->lib != NULL
451 && libsym->plt_type != LS_TOPLT_NONE)
Petr Machata09654202012-12-05 17:07:21 +0100452 /* We don't terribly mind failing this. */
453 account_output(&current_column,
454 fprintf(options.output, "%s->",
455 libsym->lib->soname));
Petr Machataf6ec08a2012-01-06 16:58:54 +0100456
457 const char *name = function_name;
Juan Cespedesd914a202004-11-10 00:15:33 +0100458#ifdef USE_DEMANGLE
Petr Machataf6ec08a2012-01-06 16:58:54 +0100459 if (options.demangle)
460 name = my_demangle(function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200461#endif
Petr Machataadec2012012-11-01 22:05:04 +0100462 if (account_output(&current_column,
Petr Machata09654202012-12-05 17:07:21 +0100463 fprintf(options.output, "%s", name)) < 0)
Petr Machataf6ec08a2012-01-06 16:58:54 +0100464 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100465
Petr Machata09654202012-12-05 17:07:21 +0100466 if (libsym->lib != NULL
467 && libsym->lib->type != LT_LIBTYPE_MAIN
468 && libsym->plt_type == LS_TOPLT_NONE
469 && account_output(&current_column,
470 fprintf(options.output, "@%s",
471 libsym->lib->soname)) < 0)
472 /* We do mind failing this though. */
Petr Machataf6ec08a2012-01-06 16:58:54 +0100473 return;
Petr Machata94078ec2012-01-05 18:07:02 +0100474
Petr Machata09654202012-12-05 17:07:21 +0100475 account_output(&current_column, fprintf(options.output, "("));
476
477 func = name2func(function_name);
478 if (func == NULL) {
479 account_output(&current_column, fprintf(options.output, "???"));
480 return;
481 }
482
Petr Machataf6ec08a2012-01-06 16:58:54 +0100483 struct fetch_context *context = fetch_arg_init(type, proc,
484 func->return_info);
Petr Machata94078ec2012-01-05 18:07:02 +0100485 struct value_dict *arguments = malloc(sizeof(*arguments));
486 if (arguments == NULL)
487 return;
488 val_dict_init(arguments);
489
Petr Machataf6ec08a2012-01-06 16:58:54 +0100490 ssize_t params_left = -1;
491 int need_delim = 0;
492 if (fetch_params(type, proc, context, arguments, func, &params_left) < 0
493 || output_params(arguments, 0, params_left, &need_delim) < 0) {
494 val_dict_destroy(arguments);
495 fetch_arg_done(context);
Petr Machata865303f2012-01-06 18:40:38 +0100496 arguments = NULL;
497 context = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100498 }
Petr Machata94078ec2012-01-05 18:07:02 +0100499
500 struct callstack_element *stel
501 = &proc->callstack[proc->callstack_depth - 1];
Petr Machataf6ec08a2012-01-06 16:58:54 +0100502 stel->fetch_context = context;
Petr Machata94078ec2012-01-05 18:07:02 +0100503 stel->arguments = arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100504 stel->out.params_left = params_left;
505 stel->out.need_delim = need_delim;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100506}
507
Petr Machatad7e4ca82012-11-28 03:38:47 +0100508static void
509free_stringp_cb(const char **stringp, void *data)
510{
511 free((char *)*stringp);
512}
513
Juan Cespedesf1350522008-12-16 18:19:58 +0100514void
Petr Machata929bd572012-12-17 03:20:34 +0100515output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
Petr Machata14184b32012-02-10 13:10:26 +0100516{
Petr Machata29add4f2012-02-18 16:38:05 +0100517 const char *function_name = libsym->name;
Petr Machata3d083b62012-11-22 15:14:45 +0100518 struct prototype *func = name2func(function_name);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100519 if (func == NULL)
520 return;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100521
Petr Machatad7e4ca82012-11-28 03:38:47 +0100522again:
Juan Cespedesda9b9532009-04-07 15:33:50 +0200523 if (options.summary) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100524 if (dict_opt_c == NULL) {
525 dict_opt_c = malloc(sizeof(*dict_opt_c));
526 if (dict_opt_c == NULL) {
527 oom:
528 fprintf(stderr,
529 "Can't allocate memory for "
530 "keeping track of -c.\n");
531 free(dict_opt_c);
532 options.summary = 0;
533 goto again;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100534 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100535 DICT_INIT(dict_opt_c, const char *, struct opt_c_struct,
536 dict_hash_string, dict_eq_string, NULL);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100537 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100538
Petr Machata98ff3092013-03-08 22:11:36 +0100539 struct opt_c_struct *st
540 = DICT_FIND_REF(dict_opt_c, &function_name,
541 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100542 if (st == NULL) {
543 const char *na = strdup(function_name);
544 struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
545 if (na == NULL
546 || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
547 free((char *)na);
548 DICT_DESTROY(dict_opt_c, const char *,
549 struct opt_c_struct,
550 free_stringp_cb, NULL, NULL);
551 goto oom;
552 }
Petr Machata98ff3092013-03-08 22:11:36 +0100553 st = DICT_FIND_REF(dict_opt_c, &function_name,
554 struct opt_c_struct);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100555 assert(st != NULL);
556 }
557
Juan Cespedesd65efa32003-02-03 00:22:30 +0100558 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
559 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
560 st->tv.tv_sec++;
561 } else {
562 st->tv.tv_usec += current_time_spent.tv_usec;
563 }
564 st->count++;
565 st->tv.tv_sec += current_time_spent.tv_sec;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100566 return;
567 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100568
Paul Gilliam76c61f12006-06-14 06:55:21 +0200569 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100570 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100571 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200572 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200573 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200574 if (current_proc != proc) {
Petr Machata37b73c02012-01-06 16:00:25 +0100575 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
Juan Cespedesd914a202004-11-10 00:15:33 +0100576#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100577 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100578 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100579 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200580#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100581 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100582 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200583#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100584 }
585
Petr Machata94078ec2012-01-05 18:07:02 +0100586 struct callstack_element *stel
587 = &proc->callstack[proc->callstack_depth - 1];
588
Petr Machataf6ec08a2012-01-06 16:58:54 +0100589 struct fetch_context *context = stel->fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +0100590
Petr Machataf6ec08a2012-01-06 16:58:54 +0100591 /* Fetch & enter into dictionary the retval first, so that
592 * other values can use it in expressions. */
593 struct value retval;
594 int own_retval = 0;
595 if (context != NULL) {
596 value_init(&retval, proc, NULL, func->return_info, 0);
597 own_retval = 1;
598 if (fetch_retval(context, type, proc, func->return_info,
Petr Machataa6fb1f52012-05-29 17:59:48 +0200599 &retval) < 0)
600 value_set_type(&retval, NULL, 0);
601 else if (stel->arguments != NULL
602 && val_dict_push_named(stel->arguments, &retval,
603 "retval", 0) == 0)
604 own_retval = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200605 }
Petr Machata94078ec2012-01-05 18:07:02 +0100606
Petr Machataf6ec08a2012-01-06 16:58:54 +0100607 if (stel->arguments != NULL)
608 output_params(stel->arguments, stel->out.params_left,
609 val_dict_count(stel->arguments),
610 &stel->out.need_delim);
611
612 current_column += fprintf(options.output, ") ");
613 tabto(options.align - 1);
614 fprintf(options.output, "= ");
615
Petr Machataf7c46bb2012-11-01 22:15:45 +0100616 if (context != NULL && retval.type != NULL) {
617 struct format_argument_data data = { &retval, stel->arguments };
618 format_argument_cb(options.output, &data);
619 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100620
621 if (own_retval)
622 value_destroy(&retval);
623
Juan Cespedesd65efa32003-02-03 00:22:30 +0100624 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100625 fprintf(options.output, " <%lu.%06d>",
Andrey Zonov6bb42012013-02-14 12:32:06 +0100626 (unsigned long)current_time_spent.tv_sec,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100627 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100628 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100629 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700630
631#if defined(HAVE_LIBUNWIND)
632 if (options.bt_depth > 0) {
633 unw_cursor_t cursor;
634 unw_word_t ip, sp;
635 int unwind_depth = options.bt_depth;
636 char fn_name[100];
637
638 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
639 while (unwind_depth) {
640 unw_get_reg(&cursor, UNW_REG_IP, &ip);
641 unw_get_reg(&cursor, UNW_REG_SP, &sp);
642 unw_get_proc_name(&cursor, fn_name, 100, NULL);
643 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
644 if (unw_step(&cursor) <= 0)
645 break;
646 unwind_depth--;
647 }
648 fprintf(options.output, "\n");
649 }
650#endif /* defined(HAVE_LIBUNWIND) */
651
Paul Gilliam76c61f12006-06-14 06:55:21 +0200652 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100653 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100654}
Petr Machatad71cf2d2012-01-05 18:28:31 +0100655
Petr Machataadec2012012-11-01 22:05:04 +0100656int
Petr Machata9c400982012-11-01 22:15:01 +0100657delim_output(FILE *stream, int *need_delimp,
658 int (*writer)(FILE *stream, void *data),
659 void *data)
660{
661 int o;
662
663 /* If we don't need a delimiter, then we don't need to go
664 * through a temporary stream. It's all the same whether
665 * WRITER emits anything or not. */
666 if (!*need_delimp) {
667 o = writer(stream, data);
668
669 } else {
Petr Machata2ae374a2012-12-08 05:08:48 +0100670 struct memstream ms;
671 if (memstream_init(&ms) < 0)
672 return -1;
673 o = writer(ms.stream, data);
674 if (memstream_close(&ms) < 0)
675 o = -1;
Petr Machata9c400982012-11-01 22:15:01 +0100676 if (o > 0 && ((*need_delimp
677 && account_output(&o, fprintf(stream, ", ")) < 0)
Petr Machata2ae374a2012-12-08 05:08:48 +0100678 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
Petr Machata9c400982012-11-01 22:15:01 +0100679 o = -1;
680
Petr Machata2ae374a2012-12-08 05:08:48 +0100681 memstream_destroy(&ms);
Petr Machata9c400982012-11-01 22:15:01 +0100682 }
683
684 if (o < 0)
685 return -1;
686
687 *need_delimp = *need_delimp || o > 0;
688 return o;
689}
690
691int
Petr Machataadec2012012-11-01 22:05:04 +0100692account_output(int *countp, int c)
693{
694 if (c > 0)
695 *countp += c;
696 return c;
697}
698
Petr Machatad71cf2d2012-01-05 18:28:31 +0100699static void
700do_report(const char *filename, unsigned line_no, const char *severity,
701 const char *fmt, va_list args)
702{
703 char buf[128];
704 vsnprintf(buf, sizeof(buf), fmt, args);
705 buf[sizeof(buf) - 1] = 0;
706 if (filename != NULL)
707 output_line(0, "%s:%d: %s: %s",
708 filename, line_no, severity, buf);
709 else
710 output_line(0, "%s: %s", severity, buf);
711}
712
713void
Petr Machata89c5ca22012-10-27 00:49:38 +0200714report_error(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100715{
716 va_list args;
717 va_start(args, fmt);
718 do_report(filename, line_no, "error", fmt, args);
719 va_end(args);
720}
721
722void
Petr Machata89c5ca22012-10-27 00:49:38 +0200723report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100724{
725 va_list args;
726 va_start(args, fmt);
727 do_report(filename, line_no, "warning", fmt, args);
728 va_end(args);
729}
730
731void
Petr Machata89c5ca22012-10-27 00:49:38 +0200732report_global_error(const char *fmt, ...)
Petr Machatad71cf2d2012-01-05 18:28:31 +0100733{
734 va_list args;
735 va_start(args, fmt);
736 do_report(NULL, 0, "error", fmt, args);
737 va_end(args);
738}