| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 3 | * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc. |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 4 | * Copyright (C) 2010 Joe Damato |
| 5 | * Copyright (C) 1997,1998,1999,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes |
| Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006 Paul Gilliam, IBM Corporation |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 7 | * 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 Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 25 | #include "config.h" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 26 | |
| Juan Cespedes | 3268a16 | 1997-08-25 16:45:22 +0200 | [diff] [blame] | 27 | #include <stdio.h> |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 28 | #include <stdlib.h> |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 29 | #include <stdarg.h> |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 30 | #include <string.h> |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 31 | #include <time.h> |
| 32 | #include <sys/time.h> |
| 33 | #include <unistd.h> |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 34 | #include <errno.h> |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 35 | #include <assert.h> |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 36 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 37 | #include "output.h" |
| Petr Machata | 3ac8db6 | 2012-11-23 18:43:10 +0100 | [diff] [blame] | 38 | #include "demangle.h" |
| Petr Machata | 9bc290b | 2012-11-23 19:08:22 +0100 | [diff] [blame] | 39 | #include "fetch.h" |
| 40 | #include "lens_default.h" |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 41 | #include "library.h" |
| Petr Machata | 9bc290b | 2012-11-23 19:08:22 +0100 | [diff] [blame] | 42 | #include "memstream.h" |
| Petr Machata | 8bf82d0 | 2012-11-23 18:44:31 +0100 | [diff] [blame] | 43 | #include "options.h" |
| Petr Machata | 9bc290b | 2012-11-23 19:08:22 +0100 | [diff] [blame] | 44 | #include "param.h" |
| 45 | #include "proc.h" |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 46 | #include "prototype.h" |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 47 | #include "summary.h" |
| Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 48 | #include "type.h" |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 49 | #include "value.h" |
| 50 | #include "value_dict.h" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 51 | |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 52 | static struct process *current_proc = NULL; |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 53 | static size_t current_depth = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 54 | static int current_column = 0; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 55 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 56 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 57 | output_indent(struct process *proc) |
| Petr Machata | 5400475 | 2012-05-03 18:36:48 +0200 | [diff] [blame] | 58 | { |
| 59 | int d = options.indent * (proc->callstack_depth - 1); |
| 60 | current_column += fprintf(options.output, "%*s", d, ""); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 61 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 62 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 63 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 64 | begin_of_line(struct process *proc, int is_func, int indent) |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 65 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 66 | current_column = 0; |
| 67 | if (!proc) { |
| 68 | return; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 69 | } |
| Juan Cespedes | c693f02 | 2009-05-21 18:59:41 +0200 | [diff] [blame] | 70 | if ((options.output != stderr) && (opt_p || options.follow)) { |
| 71 | current_column += fprintf(options.output, "%u ", proc->pid); |
| Juan Cespedes | e12df4c | 2009-05-28 19:06:35 +0200 | [diff] [blame] | 72 | } else if (options.follow) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 73 | current_column += fprintf(options.output, "[pid %u] ", proc->pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 74 | } |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 75 | if (opt_r) { |
| 76 | struct timeval tv; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 77 | static struct timeval old_tv = { 0, 0 }; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 78 | struct timeval diff; |
| 79 | |
| Petr Machata | 7564227 | 2012-11-23 19:02:13 +0100 | [diff] [blame] | 80 | gettimeofday(&tv, NULL); |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 81 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 82 | if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) { |
| 83 | old_tv.tv_sec = tv.tv_sec; |
| 84 | old_tv.tv_usec = tv.tv_usec; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 85 | } |
| 86 | diff.tv_sec = tv.tv_sec - old_tv.tv_sec; |
| 87 | if (tv.tv_usec >= old_tv.tv_usec) { |
| 88 | diff.tv_usec = tv.tv_usec - old_tv.tv_usec; |
| 89 | } else { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 90 | diff.tv_sec--; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 91 | diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec; |
| 92 | } |
| 93 | old_tv.tv_sec = tv.tv_sec; |
| 94 | old_tv.tv_usec = tv.tv_usec; |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 95 | current_column += fprintf(options.output, "%3lu.%06d ", |
| Andrey Zonov | 6bb4201 | 2013-02-14 12:32:06 +0100 | [diff] [blame] | 96 | (unsigned long)diff.tv_sec, |
| 97 | (int)diff.tv_usec); |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 98 | } |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 99 | if (opt_t) { |
| 100 | struct timeval tv; |
| Petr Machata | 7564227 | 2012-11-23 19:02:13 +0100 | [diff] [blame] | 101 | gettimeofday(&tv, NULL); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 102 | if (opt_t > 2) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 103 | current_column += fprintf(options.output, "%lu.%06d ", |
| Andrey Zonov | 6bb4201 | 2013-02-14 12:32:06 +0100 | [diff] [blame] | 104 | (unsigned long)tv.tv_sec, |
| 105 | (int)tv.tv_usec); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 106 | } else if (opt_t > 1) { |
| 107 | struct tm *tmp = localtime(&tv.tv_sec); |
| 108 | current_column += |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 109 | fprintf(options.output, "%02d:%02d:%02d.%06d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec, |
| 111 | (int)tv.tv_usec); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 112 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 113 | struct tm *tmp = localtime(&tv.tv_sec); |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 114 | current_column += fprintf(options.output, "%02d:%02d:%02d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 115 | tmp->tm_hour, tmp->tm_min, |
| 116 | tmp->tm_sec); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 119 | if (opt_i) { |
| Petr Machata | 4dcc389 | 2013-01-30 23:11:35 +0100 | [diff] [blame] | 120 | if (is_func) { |
| 121 | struct callstack_element *stel |
| 122 | = &proc->callstack[proc->callstack_depth - 1]; |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 123 | current_column += fprintf(options.output, "[%p] ", |
| Petr Machata | 4dcc389 | 2013-01-30 23:11:35 +0100 | [diff] [blame] | 124 | stel->return_addr); |
| 125 | } else { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 126 | current_column += fprintf(options.output, "[%p] ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 127 | proc->instruction_pointer); |
| Petr Machata | 4dcc389 | 2013-01-30 23:11:35 +0100 | [diff] [blame] | 128 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 129 | } |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 130 | if (options.indent > 0 && indent) { |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 131 | output_indent(proc); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 132 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 135 | static struct arg_type_info * |
| 136 | get_unknown_type(void) |
| 137 | { |
| Petr Machata | f1bd48b | 2012-12-25 05:15:47 +0100 | [diff] [blame] | 138 | static struct arg_type_info *ret = NULL; |
| 139 | if (ret != NULL) |
| 140 | return ret; |
| 141 | |
| 142 | static struct arg_type_info info; |
| 143 | info = *type_get_simple(ARGTYPE_LONG); |
| 144 | info.lens = &guess_lens; |
| 145 | ret = &info; |
| 146 | return ret; |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 149 | /* The default prototype is: long X(long, long, long, long). */ |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 150 | static struct prototype * |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 151 | build_default_prototype(void) |
| 152 | { |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 153 | static struct prototype *ret = NULL; |
| 154 | if (ret != NULL) |
| 155 | return ret; |
| 156 | |
| 157 | static struct prototype proto; |
| 158 | prototype_init(&proto); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 159 | |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 160 | struct arg_type_info *unknown_type = get_unknown_type(); |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 161 | assert(unknown_type != NULL); |
| 162 | proto.return_info = unknown_type; |
| 163 | proto.own_return_info = 0; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 164 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 165 | struct param unknown_param; |
| 166 | param_init_type(&unknown_param, unknown_type, 0); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 167 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 168 | size_t i; |
| 169 | for (i = 0; i < 4; ++i) |
| 170 | if (prototype_push_param(&proto, &unknown_param) < 0) { |
| 171 | report_global_error("build_default_prototype: %s", |
| 172 | strerror(errno)); |
| 173 | prototype_destroy(&proto); |
| 174 | return NULL; |
| 175 | } |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 176 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 177 | ret = &proto; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 178 | return ret; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| Petr Machata | 98a7dce | 2013-10-23 17:37:47 +0200 | [diff] [blame] | 181 | static bool |
| 182 | snip_period(char *buf) |
| 183 | { |
| 184 | char *period = strrchr(buf, '.'); |
| 185 | if (period != NULL && strcmp(period, ".so") != 0) { |
| 186 | *period = 0; |
| 187 | return true; |
| 188 | } else { |
| 189 | return false; |
| 190 | } |
| 191 | } |
| 192 | |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 193 | static struct prototype * |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 194 | library_get_prototype(struct library *lib, const char *name) |
| 195 | { |
| Petr Machata | 98a7dce | 2013-10-23 17:37:47 +0200 | [diff] [blame] | 196 | if (lib->protolib == NULL) { |
| 197 | size_t sz = strlen(lib->soname); |
| 198 | char buf[sz + 1]; |
| Petr Machata | ac52f26 | 2013-10-24 14:45:21 +0200 | [diff] [blame] | 199 | memcpy(buf, lib->soname, sz + 1); |
| Petr Machata | 98a7dce | 2013-10-23 17:37:47 +0200 | [diff] [blame] | 200 | |
| 201 | do { |
| 202 | if (protolib_cache_maybe_load(&g_protocache, buf, 0, |
| 203 | true, &lib->protolib) < 0) |
| 204 | return NULL; |
| 205 | } while (lib->protolib == NULL |
| 206 | && lib->type == LT_LIBTYPE_DSO |
| 207 | && snip_period(buf)); |
| 208 | |
| 209 | if (lib->protolib == NULL) |
| 210 | lib->protolib = protolib_cache_default(&g_protocache, |
| 211 | buf, 0); |
| 212 | } |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 213 | if (lib->protolib == NULL) |
| 214 | return NULL; |
| 215 | |
| Petr Machata | 82f748d | 2013-10-23 00:39:23 +0200 | [diff] [blame] | 216 | return protolib_lookup_prototype(lib->protolib, name, |
| 217 | lib->type != LT_LIBTYPE_SYSCALL); |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | struct find_proto_data { |
| 221 | const char *name; |
| 222 | struct prototype *ret; |
| 223 | }; |
| 224 | |
| 225 | static enum callback_status |
| 226 | find_proto_cb(struct process *proc, struct library *lib, void *d) |
| 227 | { |
| 228 | struct find_proto_data *data = d; |
| 229 | data->ret = library_get_prototype(lib, data->name); |
| 230 | return CBS_STOP_IF(data->ret != NULL); |
| 231 | } |
| 232 | |
| 233 | static struct prototype * |
| 234 | lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym) |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 235 | { |
| Petr Machata | b8f0d8b | 2013-10-16 14:25:45 +0200 | [diff] [blame] | 236 | if (libsym->proto != NULL) |
| 237 | return libsym->proto; |
| 238 | |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 239 | struct library *lib = libsym->lib; |
| 240 | if (lib != NULL) { |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 241 | struct find_proto_data data = { libsym->name }; |
| 242 | data.ret = library_get_prototype(lib, libsym->name); |
| 243 | if (data.ret == NULL |
| 244 | && libsym->plt_type == LS_TOPLT_EXEC) |
| 245 | proc_each_library(proc, NULL, find_proto_cb, &data); |
| 246 | if (data.ret != NULL) |
| 247 | return data.ret; |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 250 | return build_default_prototype(); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 253 | void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 254 | output_line(struct process *proc, const char *fmt, ...) |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 255 | { |
| 256 | if (options.summary) |
| 257 | return; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 258 | |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 259 | if (current_proc != NULL) { |
| 260 | if (current_proc->callstack[current_depth].return_addr) |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 261 | fprintf(options.output, " <unfinished ...>\n"); |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 262 | else |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 263 | fprintf(options.output, " <no return ...>\n"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 264 | } |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 265 | current_proc = NULL; |
| 266 | if (fmt == NULL) |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 267 | return; |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 268 | |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 269 | begin_of_line(proc, 0, 0); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 270 | |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 271 | va_list args; |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 272 | va_start(args, fmt); |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 273 | vfprintf(options.output, fmt, args); |
| 274 | fprintf(options.output, "\n"); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 275 | va_end(args); |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 276 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 277 | current_column = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 280 | static void |
| 281 | tabto(int col) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 282 | if (current_column < col) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 283 | fprintf(options.output, "%*s", col - current_column, ""); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 287 | static int |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 288 | output_error(FILE *stream) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 289 | { |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 290 | return fprintf(stream, "?"); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 294 | fetch_simple_param(enum tof type, struct process *proc, |
| 295 | struct fetch_context *context, |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 296 | struct value_dict *arguments, |
| 297 | struct arg_type_info *info, int own, |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 298 | struct value *valuep) |
| 299 | { |
| 300 | /* Arrays decay into pointers per C standard. We check for |
| 301 | * this here, because here we also capture arrays that come |
| 302 | * from parameter packs. */ |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 303 | if (info->type == ARGTYPE_ARRAY) { |
| 304 | struct arg_type_info *tmp = malloc(sizeof(*tmp)); |
| 305 | if (tmp != NULL) { |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 306 | type_init_pointer(tmp, info, own); |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 307 | tmp->lens = info->lens; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 308 | info = tmp; |
| 309 | own = 1; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | struct value value; |
| 314 | value_init(&value, proc, NULL, info, own); |
| 315 | if (fetch_arg_next(context, type, proc, info, &value) < 0) |
| 316 | return -1; |
| 317 | |
| 318 | if (val_dict_push_next(arguments, &value) < 0) { |
| 319 | value_destroy(&value); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | if (valuep != NULL) |
| 324 | *valuep = value; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static void |
| 330 | fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp) |
| 331 | { |
| 332 | if (*params_leftp == -1) |
| 333 | *params_leftp = val_dict_count(arguments); |
| 334 | } |
| 335 | |
| 336 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 337 | fetch_param_pack(enum tof type, struct process *proc, |
| 338 | struct fetch_context *context, |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 339 | struct value_dict *arguments, struct param *param, |
| 340 | ssize_t *params_leftp) |
| 341 | { |
| 342 | struct param_enum *e = param_pack_init(param, arguments); |
| 343 | if (e == NULL) |
| 344 | return -1; |
| 345 | |
| 346 | int ret = 0; |
| 347 | while (1) { |
| 348 | int insert_stop = 0; |
| 349 | struct arg_type_info *info = malloc(sizeof(*info)); |
| 350 | if (info == NULL |
| 351 | || param_pack_next(param, e, info, &insert_stop) < 0) { |
| 352 | fail: |
| 353 | free(info); |
| 354 | ret = -1; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (insert_stop) |
| 359 | fetch_param_stop(arguments, params_leftp); |
| 360 | |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 361 | if (info->type == ARGTYPE_VOID) { |
| 362 | type_destroy(info); |
| 363 | free(info); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 364 | break; |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 365 | } |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 366 | |
| 367 | struct value val; |
| 368 | if (fetch_simple_param(type, proc, context, arguments, |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 369 | info, 1, &val) < 0) |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 370 | goto fail; |
| 371 | |
| 372 | int stop = 0; |
| 373 | switch (param_pack_stop(param, e, &val)) { |
| 374 | case PPCB_ERR: |
| 375 | goto fail; |
| 376 | case PPCB_STOP: |
| 377 | stop = 1; |
| 378 | case PPCB_CONT: |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | if (stop) |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | param_pack_done(param, e); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 391 | fetch_one_param(enum tof type, struct process *proc, |
| 392 | struct fetch_context *context, |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 393 | struct value_dict *arguments, struct param *param, |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 394 | ssize_t *params_leftp) |
| 395 | { |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 396 | switch (param->flavor) { |
| Petr Machata | e36298a | 2012-09-13 17:12:41 +0200 | [diff] [blame] | 397 | int rc; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 398 | case PARAM_FLAVOR_TYPE: |
| 399 | return fetch_simple_param(type, proc, context, arguments, |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 400 | param->u.type.type, 0, NULL); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 401 | |
| 402 | case PARAM_FLAVOR_PACK: |
| Petr Machata | e36298a | 2012-09-13 17:12:41 +0200 | [diff] [blame] | 403 | if (fetch_param_pack_start(context, |
| 404 | param->u.pack.ppflavor) < 0) |
| 405 | return -1; |
| 406 | rc = fetch_param_pack(type, proc, context, arguments, |
| 407 | param, params_leftp); |
| 408 | fetch_param_pack_end(context); |
| 409 | return rc; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 410 | |
| 411 | case PARAM_FLAVOR_STOP: |
| 412 | fetch_param_stop(arguments, params_leftp); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | assert(!"Invalid param flavor!"); |
| 417 | abort(); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 420 | struct fetch_one_param_data |
| 421 | { |
| 422 | struct process *proc; |
| 423 | struct fetch_context *context; |
| 424 | struct value_dict *arguments; |
| 425 | ssize_t *params_leftp; |
| 426 | enum tof tof; |
| 427 | }; |
| 428 | |
| 429 | static enum callback_status |
| 430 | fetch_one_param_cb(struct prototype *proto, struct param *param, void *data) |
| 431 | { |
| 432 | struct fetch_one_param_data *cb_data = data; |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 433 | return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc, |
| 434 | cb_data->context, |
| 435 | cb_data->arguments, param, |
| 436 | cb_data->params_leftp) < 0); |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 439 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 440 | fetch_params(enum tof type, struct process *proc, |
| 441 | struct fetch_context *context, |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 442 | struct value_dict *arguments, struct prototype *func, |
| 443 | ssize_t *params_leftp) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 444 | { |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 445 | struct fetch_one_param_data cb_data |
| 446 | = { proc, context, arguments, params_leftp, type }; |
| 447 | if (prototype_each_param(func, NULL, |
| 448 | &fetch_one_param_cb, &cb_data) != NULL) |
| 449 | return -1; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 450 | |
| 451 | /* Implicit stop at the end of parameter list. */ |
| 452 | fetch_param_stop(arguments, params_leftp); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 453 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 457 | struct format_argument_data |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 458 | { |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 459 | struct value *value; |
| 460 | struct value_dict *arguments; |
| 461 | }; |
| 462 | |
| 463 | static int |
| 464 | format_argument_cb(FILE *stream, void *ptr) |
| 465 | { |
| 466 | struct format_argument_data *data = ptr; |
| 467 | int o = format_argument(stream, data->value, data->arguments); |
| 468 | if (o < 0) |
| 469 | o = output_error(stream); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 470 | return o; |
| 471 | } |
| 472 | |
| 473 | static int |
| 474 | output_params(struct value_dict *arguments, size_t start, size_t end, |
| 475 | int *need_delimp) |
| 476 | { |
| 477 | size_t i; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 478 | for (i = start; i < end; ++i) { |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 479 | struct value *value = val_dict_get_num(arguments, i); |
| 480 | if (value == NULL) |
| 481 | return -1; |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 482 | |
| 483 | struct format_argument_data data = { value, arguments }; |
| 484 | int o = delim_output(options.output, need_delimp, |
| 485 | format_argument_cb, &data); |
| 486 | if (o < 0) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 487 | return -1; |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 488 | current_column += o; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 489 | } |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 493 | void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 494 | output_left(enum tof type, struct process *proc, |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 495 | struct library_symbol *libsym) |
| 496 | { |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 497 | assert(! options.summary); |
| 498 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 499 | if (current_proc) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 500 | fprintf(options.output, " <unfinished ...>\n"); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 501 | current_column = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 502 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 503 | current_proc = proc; |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 504 | current_depth = proc->callstack_depth; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 505 | begin_of_line(proc, type == LT_TOF_FUNCTION, 1); |
| Petr Machata | 53bc49b | 2012-04-25 17:23:02 +0200 | [diff] [blame] | 506 | if (!options.hide_caller && libsym->lib != NULL |
| 507 | && libsym->plt_type != LS_TOPLT_NONE) |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 508 | /* We don't terribly mind failing this. */ |
| 509 | account_output(¤t_column, |
| 510 | fprintf(options.output, "%s->", |
| 511 | libsym->lib->soname)); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 512 | |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 513 | const char *name = libsym->name; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 514 | #ifdef USE_DEMANGLE |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 515 | if (options.demangle) |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 516 | name = my_demangle(libsym->name); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 517 | #endif |
| Petr Machata | adec201 | 2012-11-01 22:05:04 +0100 | [diff] [blame] | 518 | if (account_output(¤t_column, |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 519 | fprintf(options.output, "%s", name)) < 0) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 520 | return; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 521 | |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 522 | if (libsym->lib != NULL |
| 523 | && libsym->lib->type != LT_LIBTYPE_MAIN |
| 524 | && libsym->plt_type == LS_TOPLT_NONE |
| 525 | && account_output(¤t_column, |
| 526 | fprintf(options.output, "@%s", |
| 527 | libsym->lib->soname)) < 0) |
| 528 | /* We do mind failing this though. */ |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 529 | return; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 530 | |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 531 | account_output(¤t_column, fprintf(options.output, "(")); |
| 532 | |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 533 | struct prototype *func = lookup_symbol_prototype(proc, libsym); |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 534 | if (func == NULL) { |
| Petr Machata | 1b02c73 | 2013-01-07 18:22:51 +0100 | [diff] [blame] | 535 | fail: |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 536 | account_output(¤t_column, fprintf(options.output, "???")); |
| 537 | return; |
| 538 | } |
| 539 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 540 | struct fetch_context *context = fetch_arg_init(type, proc, |
| 541 | func->return_info); |
| Petr Machata | 1b02c73 | 2013-01-07 18:22:51 +0100 | [diff] [blame] | 542 | if (context == NULL) |
| 543 | goto fail; |
| 544 | |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 545 | struct value_dict *arguments = malloc(sizeof(*arguments)); |
| Petr Machata | 1b02c73 | 2013-01-07 18:22:51 +0100 | [diff] [blame] | 546 | if (arguments == NULL) { |
| 547 | fetch_arg_done(context); |
| 548 | goto fail; |
| 549 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 550 | val_dict_init(arguments); |
| 551 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 552 | ssize_t params_left = -1; |
| 553 | int need_delim = 0; |
| 554 | if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0 |
| 555 | || output_params(arguments, 0, params_left, &need_delim) < 0) { |
| 556 | val_dict_destroy(arguments); |
| 557 | fetch_arg_done(context); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 558 | arguments = NULL; |
| 559 | context = NULL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 560 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 561 | |
| 562 | struct callstack_element *stel |
| 563 | = &proc->callstack[proc->callstack_depth - 1]; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 564 | stel->fetch_context = context; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 565 | stel->arguments = arguments; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 566 | stel->out.params_left = params_left; |
| 567 | stel->out.need_delim = need_delim; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 568 | } |
| 569 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 570 | void |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 571 | output_right(enum tof type, struct process *proc, struct library_symbol *libsym, |
| 572 | struct timedelta *spent) |
| Petr Machata | 14184b3 | 2012-02-10 13:10:26 +0100 | [diff] [blame] | 573 | { |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 574 | assert(! options.summary); |
| 575 | |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 576 | struct prototype *func = lookup_symbol_prototype(proc, libsym); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 577 | if (func == NULL) |
| 578 | return; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 579 | |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 580 | if (current_proc != NULL |
| 581 | && (current_proc != proc |
| 582 | || current_depth != proc->callstack_depth)) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 583 | fprintf(options.output, " <unfinished ...>\n"); |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 584 | current_proc = NULL; |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 585 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 586 | if (current_proc != proc) { |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 587 | begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 588 | #ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 589 | current_column += |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 590 | fprintf(options.output, "<... %s resumed> ", |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 591 | options.demangle ? my_demangle(libsym->name) |
| 592 | : libsym->name); |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 593 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 594 | current_column += |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 595 | fprintf(options.output, "<... %s resumed> ", libsym->name); |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 596 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 599 | struct callstack_element *stel |
| 600 | = &proc->callstack[proc->callstack_depth - 1]; |
| 601 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 602 | struct fetch_context *context = stel->fetch_context; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 603 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 604 | /* Fetch & enter into dictionary the retval first, so that |
| 605 | * other values can use it in expressions. */ |
| 606 | struct value retval; |
| 607 | int own_retval = 0; |
| 608 | if (context != NULL) { |
| 609 | value_init(&retval, proc, NULL, func->return_info, 0); |
| 610 | own_retval = 1; |
| 611 | if (fetch_retval(context, type, proc, func->return_info, |
| Petr Machata | a6fb1f5 | 2012-05-29 17:59:48 +0200 | [diff] [blame] | 612 | &retval) < 0) |
| 613 | value_set_type(&retval, NULL, 0); |
| 614 | else if (stel->arguments != NULL |
| 615 | && val_dict_push_named(stel->arguments, &retval, |
| 616 | "retval", 0) == 0) |
| 617 | own_retval = 0; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 618 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 619 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 620 | if (stel->arguments != NULL) |
| 621 | output_params(stel->arguments, stel->out.params_left, |
| 622 | val_dict_count(stel->arguments), |
| 623 | &stel->out.need_delim); |
| 624 | |
| 625 | current_column += fprintf(options.output, ") "); |
| 626 | tabto(options.align - 1); |
| 627 | fprintf(options.output, "= "); |
| 628 | |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 629 | if (context != NULL && retval.type != NULL) { |
| 630 | struct format_argument_data data = { &retval, stel->arguments }; |
| 631 | format_argument_cb(options.output, &data); |
| 632 | } |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 633 | |
| 634 | if (own_retval) |
| 635 | value_destroy(&retval); |
| 636 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 637 | if (opt_T) { |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 638 | assert(spent != NULL); |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 639 | fprintf(options.output, " <%lu.%06d>", |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 640 | (unsigned long) spent->tm.tv_sec, |
| 641 | (int) spent->tm.tv_usec); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 642 | } |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 643 | |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 644 | fprintf(options.output, "\n"); |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 645 | |
| 646 | #if defined(HAVE_LIBUNWIND) |
| Petr Machata | af1e603 | 2013-01-06 17:16:24 +0100 | [diff] [blame] | 647 | if (options.bt_depth > 0 |
| 648 | && proc->unwind_priv != NULL |
| 649 | && proc->unwind_as != NULL) { |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 650 | unw_cursor_t cursor; |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 651 | arch_addr_t ip, function_offset; |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 652 | struct library *lib = NULL; |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 653 | int unwind_depth = options.bt_depth; |
| 654 | char fn_name[100]; |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 655 | const char *lib_name; |
| 656 | size_t distance; |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 657 | |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 658 | /* Verify that we can safely cast arch_addr_t* to |
| 659 | * unw_word_t*. */ |
| 660 | (void)sizeof(char[1 - 2*(sizeof(unw_word_t) |
| 661 | != sizeof(arch_addr_t))]); |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 662 | unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv); |
| 663 | while (unwind_depth) { |
| Luca Clementi | af452c6 | 2014-01-03 22:05:03 -0800 | [diff] [blame^] | 664 | |
| 665 | if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip)) { |
| 666 | fprintf(options.output, " > stacktrace_error\n"); |
| 667 | continue; |
| 668 | } |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 669 | |
| 670 | /* We are looking for the library with the base address |
| 671 | * closest to the current ip. */ |
| 672 | lib_name = "unmapped_area"; |
| 673 | distance = (size_t) -1; |
| 674 | lib = proc->libraries; |
| 675 | while (lib != NULL) { |
| 676 | /* N.B.: Assumes sizeof(size_t) == |
| 677 | * sizeof(arch_addr_t). |
| 678 | * Keyword: double cast. */ |
| 679 | if ((ip >= lib->base) && |
| 680 | ((size_t)(ip - lib->base) |
| 681 | < distance)) { |
| 682 | distance = ip - lib->base; |
| 683 | lib_name = lib->pathname; |
| 684 | } |
| 685 | lib = lib->next; |
| 686 | } |
| 687 | |
| Luca Clementi | af452c6 | 2014-01-03 22:05:03 -0800 | [diff] [blame^] | 688 | own_retval = unw_get_proc_name(&cursor, fn_name, sizeof(fn_name), |
| 689 | (unw_word_t *) &function_offset); |
| 690 | if ((own_retval == 0) || (own_retval == -UNW_ENOMEM)) |
| 691 | fprintf(options.output, " > %s(%s+%p) [%p]\n", |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 692 | lib_name, fn_name, function_offset, ip); |
| Luca Clementi | af452c6 | 2014-01-03 22:05:03 -0800 | [diff] [blame^] | 693 | else |
| 694 | fprintf(options.output, " > %s(??\?) [%p]\n", |
| 695 | lib_name, ip); |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 696 | |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 697 | if (unw_step(&cursor) <= 0) |
| 698 | break; |
| 699 | unwind_depth--; |
| 700 | } |
| 701 | fprintf(options.output, "\n"); |
| 702 | } |
| 703 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 704 | |
| Petr Machata | 8a730f3 | 2013-11-21 20:43:51 +0100 | [diff] [blame] | 705 | current_proc = NULL; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 706 | current_column = 0; |
| Juan Cespedes | c40e64a | 1997-10-26 20:34:00 +0100 | [diff] [blame] | 707 | } |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 708 | |
| Petr Machata | adec201 | 2012-11-01 22:05:04 +0100 | [diff] [blame] | 709 | int |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 710 | delim_output(FILE *stream, int *need_delimp, |
| 711 | int (*writer)(FILE *stream, void *data), |
| 712 | void *data) |
| 713 | { |
| 714 | int o; |
| 715 | |
| 716 | /* If we don't need a delimiter, then we don't need to go |
| 717 | * through a temporary stream. It's all the same whether |
| 718 | * WRITER emits anything or not. */ |
| 719 | if (!*need_delimp) { |
| 720 | o = writer(stream, data); |
| 721 | |
| 722 | } else { |
| Petr Machata | 2ae374a | 2012-12-08 05:08:48 +0100 | [diff] [blame] | 723 | struct memstream ms; |
| 724 | if (memstream_init(&ms) < 0) |
| 725 | return -1; |
| 726 | o = writer(ms.stream, data); |
| 727 | if (memstream_close(&ms) < 0) |
| 728 | o = -1; |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 729 | if (o > 0 && ((*need_delimp |
| 730 | && account_output(&o, fprintf(stream, ", ")) < 0) |
| Petr Machata | 2ae374a | 2012-12-08 05:08:48 +0100 | [diff] [blame] | 731 | || fwrite(ms.buf, 1, ms.size, stream) != ms.size)) |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 732 | o = -1; |
| 733 | |
| Petr Machata | 2ae374a | 2012-12-08 05:08:48 +0100 | [diff] [blame] | 734 | memstream_destroy(&ms); |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (o < 0) |
| 738 | return -1; |
| 739 | |
| 740 | *need_delimp = *need_delimp || o > 0; |
| 741 | return o; |
| 742 | } |
| 743 | |
| 744 | int |
| Petr Machata | adec201 | 2012-11-01 22:05:04 +0100 | [diff] [blame] | 745 | account_output(int *countp, int c) |
| 746 | { |
| 747 | if (c > 0) |
| 748 | *countp += c; |
| 749 | return c; |
| 750 | } |
| 751 | |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 752 | static void |
| 753 | do_report(const char *filename, unsigned line_no, const char *severity, |
| 754 | const char *fmt, va_list args) |
| 755 | { |
| 756 | char buf[128]; |
| 757 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 758 | buf[sizeof(buf) - 1] = 0; |
| 759 | if (filename != NULL) |
| 760 | output_line(0, "%s:%d: %s: %s", |
| 761 | filename, line_no, severity, buf); |
| 762 | else |
| 763 | output_line(0, "%s: %s", severity, buf); |
| 764 | } |
| 765 | |
| 766 | void |
| Petr Machata | 89c5ca2 | 2012-10-27 00:49:38 +0200 | [diff] [blame] | 767 | report_error(const char *filename, unsigned line_no, const char *fmt, ...) |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 768 | { |
| 769 | va_list args; |
| 770 | va_start(args, fmt); |
| 771 | do_report(filename, line_no, "error", fmt, args); |
| 772 | va_end(args); |
| 773 | } |
| 774 | |
| 775 | void |
| Petr Machata | 89c5ca2 | 2012-10-27 00:49:38 +0200 | [diff] [blame] | 776 | report_warning(const char *filename, unsigned line_no, const char *fmt, ...) |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 777 | { |
| 778 | va_list args; |
| 779 | va_start(args, fmt); |
| 780 | do_report(filename, line_no, "warning", fmt, args); |
| 781 | va_end(args); |
| 782 | } |
| 783 | |
| 784 | void |
| Petr Machata | 89c5ca2 | 2012-10-27 00:49:38 +0200 | [diff] [blame] | 785 | report_global_error(const char *fmt, ...) |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 786 | { |
| 787 | va_list args; |
| 788 | va_start(args, fmt); |
| 789 | do_report(NULL, 0, "error", fmt, args); |
| 790 | va_end(args); |
| 791 | } |