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