Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * probe-event.c : perf-probe definition to kprobe_events format converter |
| 3 | * |
| 4 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #define _GNU_SOURCE |
| 23 | #include <sys/utsname.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 32 | #include <stdarg.h> |
| 33 | #include <limits.h> |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 34 | |
| 35 | #undef _GNU_SOURCE |
| 36 | #include "event.h" |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 37 | #include "string.h" |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 38 | #include "strlist.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 39 | #include "debug.h" |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 40 | #include "cache.h" |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 41 | #include "color.h" |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 42 | #include "parse-events.h" /* For debugfs_path */ |
| 43 | #include "probe-event.h" |
| 44 | |
| 45 | #define MAX_CMDLEN 256 |
| 46 | #define MAX_PROBE_ARGS 128 |
| 47 | #define PERFPROBE_GROUP "probe" |
| 48 | |
| 49 | #define semantic_error(msg ...) die("Semantic error :" msg) |
| 50 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 51 | /* If there is no space to write, returns -E2BIG. */ |
| 52 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 53 | __attribute__((format(printf, 3, 4))); |
| 54 | |
| 55 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 56 | { |
| 57 | int ret; |
| 58 | va_list ap; |
| 59 | va_start(ap, format); |
| 60 | ret = vsnprintf(str, size, format, ap); |
| 61 | va_end(ap); |
| 62 | if (ret >= (int)size) |
| 63 | ret = -E2BIG; |
| 64 | return ret; |
| 65 | } |
| 66 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 67 | void parse_line_range_desc(const char *arg, struct line_range *lr) |
| 68 | { |
| 69 | const char *ptr; |
| 70 | char *tmp; |
| 71 | /* |
| 72 | * <Syntax> |
| 73 | * SRC:SLN[+NUM|-ELN] |
| 74 | * FUNC[:SLN[+NUM|-ELN]] |
| 75 | */ |
| 76 | ptr = strchr(arg, ':'); |
| 77 | if (ptr) { |
| 78 | lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0); |
| 79 | if (*tmp == '+') |
| 80 | lr->end = lr->start + (unsigned int)strtoul(tmp + 1, |
| 81 | &tmp, 0); |
| 82 | else if (*tmp == '-') |
| 83 | lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0); |
| 84 | else |
| 85 | lr->end = 0; |
| 86 | pr_debug("Line range is %u to %u\n", lr->start, lr->end); |
| 87 | if (lr->end && lr->start > lr->end) |
| 88 | semantic_error("Start line must be smaller" |
| 89 | " than end line."); |
| 90 | if (*tmp != '\0') |
| 91 | semantic_error("Tailing with invalid character '%d'.", |
| 92 | *tmp); |
| 93 | tmp = strndup(arg, (ptr - arg)); |
| 94 | } else |
| 95 | tmp = strdup(arg); |
| 96 | |
| 97 | if (strchr(tmp, '.')) |
| 98 | lr->file = tmp; |
| 99 | else |
| 100 | lr->function = tmp; |
| 101 | } |
| 102 | |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 103 | /* Check the name is good for event/group */ |
| 104 | static bool check_event_name(const char *name) |
| 105 | { |
| 106 | if (!isalpha(*name) && *name != '_') |
| 107 | return false; |
| 108 | while (*++name != '\0') { |
| 109 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 110 | return false; |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 115 | /* Parse probepoint definition. */ |
| 116 | static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) |
| 117 | { |
| 118 | char *ptr, *tmp; |
| 119 | char c, nc = 0; |
| 120 | /* |
| 121 | * <Syntax> |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 122 | * perf probe [EVENT=]SRC:LN |
| 123 | * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC] |
| 124 | * |
| 125 | * TODO:Group name support |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 126 | */ |
| 127 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 128 | ptr = strchr(arg, '='); |
| 129 | if (ptr) { /* Event name */ |
| 130 | *ptr = '\0'; |
| 131 | tmp = ptr + 1; |
| 132 | ptr = strchr(arg, ':'); |
| 133 | if (ptr) /* Group name is not supported yet. */ |
| 134 | semantic_error("Group name is not supported yet."); |
Masami Hiramatsu | b7702a2 | 2009-12-16 17:24:15 -0500 | [diff] [blame] | 135 | if (!check_event_name(arg)) |
| 136 | semantic_error("%s is bad for event name -it must " |
| 137 | "follow C symbol-naming rule.", arg); |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 138 | pp->event = strdup(arg); |
| 139 | arg = tmp; |
| 140 | } |
| 141 | |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 142 | ptr = strpbrk(arg, ":+@%"); |
| 143 | if (ptr) { |
| 144 | nc = *ptr; |
| 145 | *ptr++ = '\0'; |
| 146 | } |
| 147 | |
| 148 | /* Check arg is function or file and copy it */ |
| 149 | if (strchr(arg, '.')) /* File */ |
| 150 | pp->file = strdup(arg); |
| 151 | else /* Function */ |
| 152 | pp->function = strdup(arg); |
| 153 | DIE_IF(pp->file == NULL && pp->function == NULL); |
| 154 | |
| 155 | /* Parse other options */ |
| 156 | while (ptr) { |
| 157 | arg = ptr; |
| 158 | c = nc; |
| 159 | ptr = strpbrk(arg, ":+@%"); |
| 160 | if (ptr) { |
| 161 | nc = *ptr; |
| 162 | *ptr++ = '\0'; |
| 163 | } |
| 164 | switch (c) { |
| 165 | case ':': /* Line number */ |
| 166 | pp->line = strtoul(arg, &tmp, 0); |
| 167 | if (*tmp != '\0') |
| 168 | semantic_error("There is non-digit charactor" |
| 169 | " in line number."); |
| 170 | break; |
| 171 | case '+': /* Byte offset from a symbol */ |
| 172 | pp->offset = strtoul(arg, &tmp, 0); |
| 173 | if (*tmp != '\0') |
| 174 | semantic_error("There is non-digit charactor" |
| 175 | " in offset."); |
| 176 | break; |
| 177 | case '@': /* File name */ |
| 178 | if (pp->file) |
| 179 | semantic_error("SRC@SRC is not allowed."); |
| 180 | pp->file = strdup(arg); |
| 181 | DIE_IF(pp->file == NULL); |
| 182 | if (ptr) |
| 183 | semantic_error("@SRC must be the last " |
| 184 | "option."); |
| 185 | break; |
| 186 | case '%': /* Probe places */ |
| 187 | if (strcmp(arg, "return") == 0) { |
| 188 | pp->retprobe = 1; |
| 189 | } else /* Others not supported yet */ |
| 190 | semantic_error("%%%s is not supported.", arg); |
| 191 | break; |
| 192 | default: |
| 193 | DIE_IF("Program has a bug."); |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* Exclusion check */ |
| 199 | if (pp->line && pp->offset) |
| 200 | semantic_error("Offset can't be used with line number."); |
| 201 | |
| 202 | if (!pp->line && pp->file && !pp->function) |
| 203 | semantic_error("File always requires line number."); |
| 204 | |
| 205 | if (pp->offset && !pp->function) |
| 206 | semantic_error("Offset requires an entry function."); |
| 207 | |
| 208 | if (pp->retprobe && !pp->function) |
| 209 | semantic_error("Return probe requires an entry function."); |
| 210 | |
| 211 | if ((pp->offset || pp->line) && pp->retprobe) |
| 212 | semantic_error("Offset/Line can't be used with return probe."); |
| 213 | |
| 214 | pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", |
| 215 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe); |
| 216 | } |
| 217 | |
| 218 | /* Parse perf-probe event definition */ |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 219 | void parse_perf_probe_event(const char *str, struct probe_point *pp, |
| 220 | bool *need_dwarf) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 221 | { |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 222 | char **argv; |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 223 | int argc, i; |
| 224 | |
| 225 | *need_dwarf = false; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 226 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 227 | argv = argv_split(str, &argc); |
| 228 | if (!argv) |
| 229 | die("argv_split failed."); |
| 230 | if (argc > MAX_PROBE_ARGS + 1) |
| 231 | semantic_error("Too many arguments"); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 232 | |
| 233 | /* Parse probe point */ |
| 234 | parse_perf_probe_probepoint(argv[0], pp); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 235 | if (pp->file || pp->line) |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 236 | *need_dwarf = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 237 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 238 | /* Copy arguments and ensure return probe has no C argument */ |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 239 | pp->nr_args = argc - 1; |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 240 | pp->args = zalloc(sizeof(char *) * pp->nr_args); |
| 241 | for (i = 0; i < pp->nr_args; i++) { |
| 242 | pp->args[i] = strdup(argv[i + 1]); |
| 243 | if (!pp->args[i]) |
| 244 | die("Failed to copy argument."); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 245 | if (is_c_varname(pp->args[i])) { |
| 246 | if (pp->retprobe) |
| 247 | semantic_error("You can't specify local" |
| 248 | " variable for kretprobe"); |
Masami Hiramatsu | fac13fd | 2009-12-15 10:31:14 -0500 | [diff] [blame] | 249 | *need_dwarf = true; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 250 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 251 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 252 | |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 253 | argv_free(argv); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 254 | } |
| 255 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 256 | /* Parse kprobe_events event into struct probe_point */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 257 | void parse_trace_kprobe_event(const char *str, struct probe_point *pp) |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 258 | { |
| 259 | char pr; |
| 260 | char *p; |
| 261 | int ret, i, argc; |
| 262 | char **argv; |
| 263 | |
| 264 | pr_debug("Parsing kprobe_events: %s\n", str); |
| 265 | argv = argv_split(str, &argc); |
| 266 | if (!argv) |
| 267 | die("argv_split failed."); |
| 268 | if (argc < 2) |
| 269 | semantic_error("Too less arguments."); |
| 270 | |
| 271 | /* Scan event and group name. */ |
Liming Wang | 93aaa45 | 2009-12-02 16:42:54 +0800 | [diff] [blame] | 272 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 273 | &pr, (float *)(void *)&pp->group, |
| 274 | (float *)(void *)&pp->event); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 275 | if (ret != 3) |
| 276 | semantic_error("Failed to parse event name: %s", argv[0]); |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 277 | pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 278 | |
| 279 | pp->retprobe = (pr == 'r'); |
| 280 | |
| 281 | /* Scan function name and offset */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 282 | ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, |
| 283 | &pp->offset); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 284 | if (ret == 1) |
| 285 | pp->offset = 0; |
| 286 | |
| 287 | /* kprobe_events doesn't have this information */ |
| 288 | pp->line = 0; |
| 289 | pp->file = NULL; |
| 290 | |
| 291 | pp->nr_args = argc - 2; |
| 292 | pp->args = zalloc(sizeof(char *) * pp->nr_args); |
| 293 | for (i = 0; i < pp->nr_args; i++) { |
| 294 | p = strchr(argv[i + 2], '='); |
| 295 | if (p) /* We don't need which register is assigned. */ |
| 296 | *p = '\0'; |
| 297 | pp->args[i] = strdup(argv[i + 2]); |
| 298 | if (!pp->args[i]) |
| 299 | die("Failed to copy argument."); |
| 300 | } |
| 301 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 302 | argv_free(argv); |
| 303 | } |
| 304 | |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 305 | /* Synthesize only probe point (not argument) */ |
| 306 | int synthesize_perf_probe_point(struct probe_point *pp) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 307 | { |
| 308 | char *buf; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 309 | char offs[64] = "", line[64] = ""; |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 310 | int ret; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 311 | |
| 312 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
| 313 | if (!buf) |
| 314 | die("Failed to allocate memory by zalloc."); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 315 | if (pp->offset) { |
| 316 | ret = e_snprintf(offs, 64, "+%d", pp->offset); |
| 317 | if (ret <= 0) |
| 318 | goto error; |
| 319 | } |
| 320 | if (pp->line) { |
| 321 | ret = e_snprintf(line, 64, ":%d", pp->line); |
| 322 | if (ret <= 0) |
| 323 | goto error; |
| 324 | } |
| 325 | |
| 326 | if (pp->function) |
| 327 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function, |
| 328 | offs, pp->retprobe ? "%return" : "", line); |
| 329 | else |
Masami Hiramatsu | 8498845 | 2009-12-07 12:00:53 -0500 | [diff] [blame] | 330 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line); |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 331 | if (ret <= 0) { |
| 332 | error: |
| 333 | free(pp->probes[0]); |
| 334 | pp->probes[0] = NULL; |
| 335 | } |
| 336 | return ret; |
| 337 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 338 | |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 339 | int synthesize_perf_probe_event(struct probe_point *pp) |
| 340 | { |
| 341 | char *buf; |
| 342 | int i, len, ret; |
| 343 | |
| 344 | len = synthesize_perf_probe_point(pp); |
| 345 | if (len < 0) |
| 346 | return 0; |
| 347 | |
| 348 | buf = pp->probes[0]; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 349 | for (i = 0; i < pp->nr_args; i++) { |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 350 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 351 | pp->args[i]); |
| 352 | if (ret <= 0) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 353 | goto error; |
| 354 | len += ret; |
| 355 | } |
| 356 | pp->found = 1; |
| 357 | |
| 358 | return pp->found; |
| 359 | error: |
| 360 | free(pp->probes[0]); |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 361 | pp->probes[0] = NULL; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 366 | int synthesize_trace_kprobe_event(struct probe_point *pp) |
| 367 | { |
| 368 | char *buf; |
| 369 | int i, len, ret; |
| 370 | |
| 371 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
| 372 | if (!buf) |
| 373 | die("Failed to allocate memory by zalloc."); |
| 374 | ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); |
| 375 | if (ret <= 0) |
| 376 | goto error; |
| 377 | len = ret; |
| 378 | |
| 379 | for (i = 0; i < pp->nr_args; i++) { |
| 380 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
| 381 | pp->args[i]); |
| 382 | if (ret <= 0) |
| 383 | goto error; |
| 384 | len += ret; |
| 385 | } |
| 386 | pp->found = 1; |
| 387 | |
| 388 | return pp->found; |
| 389 | error: |
| 390 | free(pp->probes[0]); |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 391 | pp->probes[0] = NULL; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | static int open_kprobe_events(int flags, int mode) |
| 397 | { |
| 398 | char buf[PATH_MAX]; |
| 399 | int ret; |
| 400 | |
| 401 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); |
| 402 | if (ret < 0) |
| 403 | die("Failed to make kprobe_events path."); |
| 404 | |
| 405 | ret = open(buf, flags, mode); |
| 406 | if (ret < 0) { |
| 407 | if (errno == ENOENT) |
| 408 | die("kprobe_events file does not exist -" |
Liming Wang | 63bbd5e | 2009-12-29 16:37:09 +0800 | [diff] [blame] | 409 | " please rebuild with CONFIG_KPROBE_EVENT."); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 410 | else |
| 411 | die("Could not open kprobe_events file: %s", |
| 412 | strerror(errno)); |
| 413 | } |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | /* Get raw string list of current kprobe_events */ |
| 418 | static struct strlist *get_trace_kprobe_event_rawlist(int fd) |
| 419 | { |
| 420 | int ret, idx; |
| 421 | FILE *fp; |
| 422 | char buf[MAX_CMDLEN]; |
| 423 | char *p; |
| 424 | struct strlist *sl; |
| 425 | |
| 426 | sl = strlist__new(true, NULL); |
| 427 | |
| 428 | fp = fdopen(dup(fd), "r"); |
| 429 | while (!feof(fp)) { |
| 430 | p = fgets(buf, MAX_CMDLEN, fp); |
| 431 | if (!p) |
| 432 | break; |
| 433 | |
| 434 | idx = strlen(p) - 1; |
| 435 | if (p[idx] == '\n') |
| 436 | p[idx] = '\0'; |
| 437 | ret = strlist__add(sl, buf); |
| 438 | if (ret < 0) |
| 439 | die("strlist__add failed: %s", strerror(-ret)); |
| 440 | } |
| 441 | fclose(fp); |
| 442 | |
| 443 | return sl; |
| 444 | } |
| 445 | |
| 446 | /* Free and zero clear probe_point */ |
| 447 | static void clear_probe_point(struct probe_point *pp) |
| 448 | { |
| 449 | int i; |
| 450 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 451 | if (pp->event) |
| 452 | free(pp->event); |
| 453 | if (pp->group) |
| 454 | free(pp->group); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 455 | if (pp->function) |
| 456 | free(pp->function); |
| 457 | if (pp->file) |
| 458 | free(pp->file); |
| 459 | for (i = 0; i < pp->nr_args; i++) |
| 460 | free(pp->args[i]); |
| 461 | if (pp->args) |
| 462 | free(pp->args); |
| 463 | for (i = 0; i < pp->found; i++) |
| 464 | free(pp->probes[i]); |
Julia Lawall | 5660ce3 | 2009-12-09 20:26:18 +0100 | [diff] [blame] | 465 | memset(pp, 0, sizeof(*pp)); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 466 | } |
| 467 | |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 468 | /* Show an event */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 469 | static void show_perf_probe_event(const char *event, const char *place, |
| 470 | struct probe_point *pp) |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 471 | { |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 472 | int i, ret; |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 473 | char buf[128]; |
| 474 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 475 | ret = e_snprintf(buf, 128, "%s:%s", pp->group, event); |
Masami Hiramatsu | 7e990a5 | 2009-12-15 10:31:21 -0500 | [diff] [blame] | 476 | if (ret < 0) |
| 477 | die("Failed to copy event: %s", strerror(-ret)); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 478 | printf(" %-40s (on %s", buf, place); |
| 479 | |
| 480 | if (pp->nr_args > 0) { |
| 481 | printf(" with"); |
| 482 | for (i = 0; i < pp->nr_args; i++) |
| 483 | printf(" %s", pp->args[i]); |
| 484 | } |
| 485 | printf(")\n"); |
| 486 | } |
| 487 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 488 | /* List up current perf-probe events */ |
| 489 | void show_perf_probe_events(void) |
| 490 | { |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 491 | int fd; |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 492 | struct probe_point pp; |
| 493 | struct strlist *rawlist; |
| 494 | struct str_node *ent; |
| 495 | |
Masami Hiramatsu | 7204133 | 2010-01-05 17:47:10 -0500 | [diff] [blame] | 496 | setup_pager(); |
| 497 | |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 498 | fd = open_kprobe_events(O_RDONLY, 0); |
| 499 | rawlist = get_trace_kprobe_event_rawlist(fd); |
| 500 | close(fd); |
| 501 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 502 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 503 | parse_trace_kprobe_event(ent->s, &pp); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 504 | /* Synthesize only event probe point */ |
Masami Hiramatsu | 7ef17aa | 2009-12-15 10:32:47 -0500 | [diff] [blame] | 505 | synthesize_perf_probe_point(&pp); |
Masami Hiramatsu | 278498d | 2009-12-08 17:02:40 -0500 | [diff] [blame] | 506 | /* Show an event */ |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 507 | show_perf_probe_event(pp.event, pp.probes[0], &pp); |
Masami Hiramatsu | 4de189f | 2009-11-30 19:20:17 -0500 | [diff] [blame] | 508 | clear_probe_point(&pp); |
| 509 | } |
| 510 | |
| 511 | strlist__delete(rawlist); |
| 512 | } |
| 513 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 514 | /* Get current perf-probe event names */ |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 515 | static struct strlist *get_perf_event_names(int fd, bool include_group) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 516 | { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 517 | char buf[128]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 518 | struct strlist *sl, *rawlist; |
| 519 | struct str_node *ent; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 520 | struct probe_point pp; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 521 | |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 522 | memset(&pp, 0, sizeof(pp)); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 523 | rawlist = get_trace_kprobe_event_rawlist(fd); |
| 524 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 525 | sl = strlist__new(true, NULL); |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 526 | strlist__for_each(ent, rawlist) { |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 527 | parse_trace_kprobe_event(ent->s, &pp); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 528 | if (include_group) { |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 529 | if (e_snprintf(buf, 128, "%s:%s", pp.group, |
| 530 | pp.event) < 0) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 531 | die("Failed to copy group:event name."); |
| 532 | strlist__add(sl, buf); |
| 533 | } else |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 534 | strlist__add(sl, pp.event); |
| 535 | clear_probe_point(&pp); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | strlist__delete(rawlist); |
| 539 | |
| 540 | return sl; |
| 541 | } |
| 542 | |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 543 | static void write_trace_kprobe_event(int fd, const char *buf) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 544 | { |
| 545 | int ret; |
| 546 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 547 | pr_debug("Writing event: %s\n", buf); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 548 | ret = write(fd, buf, strlen(buf)); |
| 549 | if (ret <= 0) |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 550 | die("Failed to write event: %s", strerror(errno)); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 553 | static void get_new_event_name(char *buf, size_t len, const char *base, |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 554 | struct strlist *namelist, bool allow_suffix) |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 555 | { |
| 556 | int i, ret; |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 557 | |
| 558 | /* Try no suffix */ |
| 559 | ret = e_snprintf(buf, len, "%s", base); |
| 560 | if (ret < 0) |
| 561 | die("snprintf() failed: %s", strerror(-ret)); |
| 562 | if (!strlist__has_entry(namelist, buf)) |
| 563 | return; |
| 564 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 565 | if (!allow_suffix) { |
| 566 | pr_warning("Error: event \"%s\" already exists. " |
| 567 | "(Use -f to force duplicates.)\n", base); |
| 568 | die("Can't add new event."); |
| 569 | } |
| 570 | |
Masami Hiramatsu | 17f88fc | 2009-12-08 17:03:02 -0500 | [diff] [blame] | 571 | /* Try to add suffix */ |
| 572 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 573 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
| 574 | if (ret < 0) |
| 575 | die("snprintf() failed: %s", strerror(-ret)); |
| 576 | if (!strlist__has_entry(namelist, buf)) |
| 577 | break; |
| 578 | } |
| 579 | if (i == MAX_EVENT_INDEX) |
| 580 | die("Too many events are on the same function."); |
| 581 | } |
| 582 | |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 583 | void add_trace_kprobe_events(struct probe_point *probes, int nr_probes, |
| 584 | bool force_add) |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 585 | { |
| 586 | int i, j, fd; |
| 587 | struct probe_point *pp; |
| 588 | char buf[MAX_CMDLEN]; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 589 | char event[64]; |
| 590 | struct strlist *namelist; |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 591 | bool allow_suffix; |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 592 | |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 593 | fd = open_kprobe_events(O_RDWR, O_APPEND); |
| 594 | /* Get current event names */ |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 595 | namelist = get_perf_event_names(fd, false); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 596 | |
| 597 | for (j = 0; j < nr_probes; j++) { |
| 598 | pp = probes + j; |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 599 | if (!pp->event) |
| 600 | pp->event = strdup(pp->function); |
| 601 | if (!pp->group) |
| 602 | pp->group = strdup(PERFPROBE_GROUP); |
| 603 | DIE_IF(!pp->event || !pp->group); |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 604 | /* If force_add is true, suffix search is allowed */ |
| 605 | allow_suffix = force_add; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 606 | for (i = 0; i < pp->found; i++) { |
| 607 | /* Get an unused new event name */ |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 608 | get_new_event_name(event, 64, pp->event, namelist, |
| 609 | allow_suffix); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 610 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n", |
| 611 | pp->retprobe ? 'r' : 'p', |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 612 | pp->group, event, |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 613 | pp->probes[i]); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 614 | write_trace_kprobe_event(fd, buf); |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 615 | printf("Added new event:\n"); |
| 616 | /* Get the first parameter (probe-point) */ |
| 617 | sscanf(pp->probes[i], "%s", buf); |
Masami Hiramatsu | af663d7 | 2009-12-15 10:32:18 -0500 | [diff] [blame] | 618 | show_perf_probe_event(event, buf, pp); |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 619 | /* Add added event name to namelist */ |
| 620 | strlist__add(namelist, event); |
Masami Hiramatsu | d761b08 | 2009-12-15 10:32:25 -0500 | [diff] [blame] | 621 | /* |
| 622 | * Probes after the first probe which comes from same |
| 623 | * user input are always allowed to add suffix, because |
| 624 | * there might be several addresses corresponding to |
| 625 | * one code line. |
| 626 | */ |
| 627 | allow_suffix = true; |
Masami Hiramatsu | b498ce1 | 2009-11-30 19:20:25 -0500 | [diff] [blame] | 628 | } |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 629 | } |
Masami Hiramatsu | a9b495b | 2009-12-08 17:02:47 -0500 | [diff] [blame] | 630 | /* Show how to use the event. */ |
| 631 | printf("\nYou can now use it on all perf tools, such as:\n\n"); |
| 632 | printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event); |
| 633 | |
Masami Hiramatsu | e1d2017 | 2009-12-07 12:00:46 -0500 | [diff] [blame] | 634 | strlist__delete(namelist); |
Masami Hiramatsu | 50656ee | 2009-11-30 19:19:58 -0500 | [diff] [blame] | 635 | close(fd); |
| 636 | } |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 637 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 638 | static void __del_trace_kprobe_event(int fd, struct str_node *ent) |
| 639 | { |
| 640 | char *p; |
| 641 | char buf[128]; |
| 642 | |
| 643 | /* Convert from perf-probe event to trace-kprobe event */ |
| 644 | if (e_snprintf(buf, 128, "-:%s", ent->s) < 0) |
| 645 | die("Failed to copy event."); |
| 646 | p = strchr(buf + 2, ':'); |
| 647 | if (!p) |
| 648 | die("Internal error: %s should have ':' but not.", ent->s); |
| 649 | *p = '/'; |
| 650 | |
| 651 | write_trace_kprobe_event(fd, buf); |
| 652 | printf("Remove event: %s\n", ent->s); |
| 653 | } |
| 654 | |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 655 | static void del_trace_kprobe_event(int fd, const char *group, |
| 656 | const char *event, struct strlist *namelist) |
| 657 | { |
| 658 | char buf[128]; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 659 | struct str_node *ent, *n; |
| 660 | int found = 0; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 661 | |
| 662 | if (e_snprintf(buf, 128, "%s:%s", group, event) < 0) |
| 663 | die("Failed to copy event."); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 664 | |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 665 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
| 666 | strlist__for_each_safe(ent, n, namelist) |
| 667 | if (strglobmatch(ent->s, buf)) { |
| 668 | found++; |
| 669 | __del_trace_kprobe_event(fd, ent); |
| 670 | strlist__remove(namelist, ent); |
| 671 | } |
| 672 | } else { |
| 673 | ent = strlist__find(namelist, buf); |
| 674 | if (ent) { |
| 675 | found++; |
| 676 | __del_trace_kprobe_event(fd, ent); |
| 677 | strlist__remove(namelist, ent); |
| 678 | } |
| 679 | } |
| 680 | if (found == 0) |
| 681 | pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void del_trace_kprobe_events(struct strlist *dellist) |
| 685 | { |
| 686 | int fd; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 687 | const char *group, *event; |
| 688 | char *p, *str; |
| 689 | struct str_node *ent; |
| 690 | struct strlist *namelist; |
| 691 | |
| 692 | fd = open_kprobe_events(O_RDWR, O_APPEND); |
| 693 | /* Get current event names */ |
| 694 | namelist = get_perf_event_names(fd, true); |
| 695 | |
Masami Hiramatsu | adf365f | 2009-12-15 10:32:03 -0500 | [diff] [blame] | 696 | strlist__for_each(ent, dellist) { |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 697 | str = strdup(ent->s); |
| 698 | if (!str) |
| 699 | die("Failed to copy event."); |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 700 | pr_debug("Parsing: %s\n", str); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 701 | p = strchr(str, ':'); |
| 702 | if (p) { |
| 703 | group = str; |
| 704 | *p = '\0'; |
| 705 | event = p + 1; |
| 706 | } else { |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 707 | group = "*"; |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 708 | event = str; |
| 709 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 710 | pr_debug("Group: %s, Event: %s\n", group, event); |
Masami Hiramatsu | fa28244 | 2009-12-08 17:03:23 -0500 | [diff] [blame] | 711 | del_trace_kprobe_event(fd, group, event, namelist); |
| 712 | free(str); |
| 713 | } |
| 714 | strlist__delete(namelist); |
| 715 | close(fd); |
| 716 | } |
| 717 | |
Masami Hiramatsu | 631c9de | 2010-01-06 09:45:34 -0500 | [diff] [blame] | 718 | #define LINEBUF_SIZE 256 |
| 719 | |
| 720 | static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num) |
| 721 | { |
| 722 | char buf[LINEBUF_SIZE]; |
| 723 | const char *color = PERF_COLOR_BLUE; |
| 724 | |
| 725 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 726 | goto error; |
| 727 | if (!skip) { |
| 728 | if (show_num) |
| 729 | fprintf(stdout, "%7u %s", l, buf); |
| 730 | else |
| 731 | color_fprintf(stdout, color, " %s", buf); |
| 732 | } |
| 733 | |
| 734 | while (strlen(buf) == LINEBUF_SIZE - 1 && |
| 735 | buf[LINEBUF_SIZE - 2] != '\n') { |
| 736 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 737 | goto error; |
| 738 | if (!skip) { |
| 739 | if (show_num) |
| 740 | fprintf(stdout, "%s", buf); |
| 741 | else |
| 742 | color_fprintf(stdout, color, "%s", buf); |
| 743 | } |
| 744 | } |
| 745 | return; |
| 746 | error: |
| 747 | if (feof(fp)) |
| 748 | die("Source file is shorter than expected."); |
| 749 | else |
| 750 | die("File read error: %s", strerror(errno)); |
| 751 | } |
| 752 | |
| 753 | void show_line_range(struct line_range *lr) |
| 754 | { |
| 755 | unsigned int l = 1; |
| 756 | struct line_node *ln; |
| 757 | FILE *fp; |
| 758 | |
| 759 | setup_pager(); |
| 760 | |
| 761 | if (lr->function) |
| 762 | fprintf(stdout, "<%s:%d>\n", lr->function, |
| 763 | lr->start - lr->offset); |
| 764 | else |
| 765 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); |
| 766 | |
| 767 | fp = fopen(lr->path, "r"); |
| 768 | if (fp == NULL) |
| 769 | die("Failed to open %s: %s", lr->path, strerror(errno)); |
| 770 | /* Skip to starting line number */ |
| 771 | while (l < lr->start) |
| 772 | show_one_line(fp, l++, true, false); |
| 773 | |
| 774 | list_for_each_entry(ln, &lr->line_list, list) { |
| 775 | while (ln->line > l) |
| 776 | show_one_line(fp, (l++) - lr->offset, false, false); |
| 777 | show_one_line(fp, (l++) - lr->offset, false, true); |
| 778 | } |
| 779 | fclose(fp); |
| 780 | } |