Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / srccode.c
blobc29edaaca8633e5539511abb457e5592ee7fbbf1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Manage printing of source lines
4 * Copyright (c) 2017, Intel Corporation.
5 * Author: Andi Kleen
6 */
7 #include <linux/list.h>
8 #include <linux/zalloc.h>
9 #include <stdlib.h>
10 #include <sys/mman.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <assert.h>
15 #include <string.h>
16 #include "srccode.h"
17 #include "debug.h"
18 #include <internal/lib.h> // page_size
19 #include "fncache.h"
21 #define MAXSRCCACHE (32*1024*1024)
22 #define MAXSRCFILES 64
23 #define SRC_HTAB_SZ 64
25 struct srcfile {
26 struct hlist_node hash_nd;
27 struct list_head nd;
28 char *fn;
29 char **lines;
30 char *map;
31 unsigned numlines;
32 size_t maplen;
35 static struct hlist_head srcfile_htab[SRC_HTAB_SZ];
36 static LIST_HEAD(srcfile_list);
37 static long map_total_sz;
38 static int num_srcfiles;
40 static int countlines(char *map, int maplen)
42 int numl;
43 char *end = map + maplen;
44 char *p = map;
46 if (maplen == 0)
47 return 0;
48 numl = 0;
49 while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
50 numl++;
51 p++;
53 if (p < end)
54 numl++;
55 return numl;
58 static void fill_lines(char **lines, int maxline, char *map, int maplen)
60 int l;
61 char *end = map + maplen;
62 char *p = map;
64 if (maplen == 0 || maxline == 0)
65 return;
66 l = 0;
67 lines[l++] = map;
68 while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
69 if (l >= maxline)
70 return;
71 lines[l++] = ++p;
73 if (p < end)
74 lines[l] = p;
77 static void free_srcfile(struct srcfile *sf)
79 list_del_init(&sf->nd);
80 hlist_del(&sf->hash_nd);
81 map_total_sz -= sf->maplen;
82 munmap(sf->map, sf->maplen);
83 zfree(&sf->lines);
84 zfree(&sf->fn);
85 free(sf);
86 num_srcfiles--;
89 static struct srcfile *find_srcfile(char *fn)
91 struct stat st;
92 struct srcfile *h;
93 int fd;
94 unsigned long sz;
95 unsigned hval = shash((unsigned char *)fn) % SRC_HTAB_SZ;
97 hlist_for_each_entry (h, &srcfile_htab[hval], hash_nd) {
98 if (!strcmp(fn, h->fn)) {
99 /* Move to front */
100 list_del(&h->nd);
101 list_add(&h->nd, &srcfile_list);
102 return h;
106 /* Only prune if there is more than one entry */
107 while ((num_srcfiles > MAXSRCFILES || map_total_sz > MAXSRCCACHE) &&
108 srcfile_list.next != &srcfile_list) {
109 assert(!list_empty(&srcfile_list));
110 h = list_entry(srcfile_list.prev, struct srcfile, nd);
111 free_srcfile(h);
114 fd = open(fn, O_RDONLY);
115 if (fd < 0 || fstat(fd, &st) < 0) {
116 pr_debug("cannot open source file %s\n", fn);
117 return NULL;
120 h = malloc(sizeof(struct srcfile));
121 if (!h)
122 return NULL;
124 h->fn = strdup(fn);
125 if (!h->fn)
126 goto out_h;
128 h->maplen = st.st_size;
129 sz = (h->maplen + page_size - 1) & ~(page_size - 1);
130 h->map = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
131 close(fd);
132 if (h->map == (char *)-1) {
133 pr_debug("cannot mmap source file %s\n", fn);
134 goto out_fn;
136 h->numlines = countlines(h->map, h->maplen);
137 h->lines = calloc(h->numlines, sizeof(char *));
138 if (!h->lines)
139 goto out_map;
140 fill_lines(h->lines, h->numlines, h->map, h->maplen);
141 list_add(&h->nd, &srcfile_list);
142 hlist_add_head(&h->hash_nd, &srcfile_htab[hval]);
143 map_total_sz += h->maplen;
144 num_srcfiles++;
145 return h;
147 out_map:
148 munmap(h->map, sz);
149 out_fn:
150 zfree(&h->fn);
151 out_h:
152 free(h);
153 return NULL;
156 /* Result is not 0 terminated */
157 char *find_sourceline(char *fn, unsigned line, int *lenp)
159 char *l, *p;
160 struct srcfile *sf = find_srcfile(fn);
161 if (!sf)
162 return NULL;
163 line--;
164 if (line >= sf->numlines)
165 return NULL;
166 l = sf->lines[line];
167 if (!l)
168 return NULL;
169 p = memchr(l, '\n', sf->map + sf->maplen - l);
170 *lenp = p - l;
171 return l;