Loading...
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
20#define MAXSRCCACHE (32*1024*1024)
21#define MAXSRCFILES 64
22#define SRC_HTAB_SZ 64
23
24struct srcfile {
25 struct hlist_node hash_nd;
26 struct list_head nd;
27 char *fn;
28 char **lines;
29 char *map;
30 unsigned numlines;
31 size_t maplen;
32};
33
34static struct hlist_head srcfile_htab[SRC_HTAB_SZ];
35static LIST_HEAD(srcfile_list);
36static long map_total_sz;
37static int num_srcfiles;
38
39static unsigned shash(unsigned char *s)
40{
41 unsigned h = 0;
42 while (*s)
43 h = 65599 * h + *s++;
44 return h ^ (h >> 16);
45}
46
47static int countlines(char *map, int maplen)
48{
49 int numl;
50 char *end = map + maplen;
51 char *p = map;
52
53 if (maplen == 0)
54 return 0;
55 numl = 0;
56 while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
57 numl++;
58 p++;
59 }
60 if (p < end)
61 numl++;
62 return numl;
63}
64
65static void fill_lines(char **lines, int maxline, char *map, int maplen)
66{
67 int l;
68 char *end = map + maplen;
69 char *p = map;
70
71 if (maplen == 0 || maxline == 0)
72 return;
73 l = 0;
74 lines[l++] = map;
75 while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
76 if (l >= maxline)
77 return;
78 lines[l++] = ++p;
79 }
80 if (p < end)
81 lines[l] = p;
82}
83
84static void free_srcfile(struct srcfile *sf)
85{
86 list_del_init(&sf->nd);
87 hlist_del(&sf->hash_nd);
88 map_total_sz -= sf->maplen;
89 munmap(sf->map, sf->maplen);
90 zfree(&sf->lines);
91 zfree(&sf->fn);
92 free(sf);
93 num_srcfiles--;
94}
95
96static struct srcfile *find_srcfile(char *fn)
97{
98 struct stat st;
99 struct srcfile *h;
100 int fd;
101 unsigned long sz;
102 unsigned hval = shash((unsigned char *)fn) % SRC_HTAB_SZ;
103
104 hlist_for_each_entry (h, &srcfile_htab[hval], hash_nd) {
105 if (!strcmp(fn, h->fn)) {
106 /* Move to front */
107 list_del(&h->nd);
108 list_add(&h->nd, &srcfile_list);
109 return h;
110 }
111 }
112
113 /* Only prune if there is more than one entry */
114 while ((num_srcfiles > MAXSRCFILES || map_total_sz > MAXSRCCACHE) &&
115 srcfile_list.next != &srcfile_list) {
116 assert(!list_empty(&srcfile_list));
117 h = list_entry(srcfile_list.prev, struct srcfile, nd);
118 free_srcfile(h);
119 }
120
121 fd = open(fn, O_RDONLY);
122 if (fd < 0 || fstat(fd, &st) < 0) {
123 pr_debug("cannot open source file %s\n", fn);
124 return NULL;
125 }
126
127 h = malloc(sizeof(struct srcfile));
128 if (!h)
129 return NULL;
130
131 h->fn = strdup(fn);
132 if (!h->fn)
133 goto out_h;
134
135 h->maplen = st.st_size;
136 sz = (h->maplen + page_size - 1) & ~(page_size - 1);
137 h->map = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
138 close(fd);
139 if (h->map == (char *)-1) {
140 pr_debug("cannot mmap source file %s\n", fn);
141 goto out_fn;
142 }
143 h->numlines = countlines(h->map, h->maplen);
144 h->lines = calloc(h->numlines, sizeof(char *));
145 if (!h->lines)
146 goto out_map;
147 fill_lines(h->lines, h->numlines, h->map, h->maplen);
148 list_add(&h->nd, &srcfile_list);
149 hlist_add_head(&h->hash_nd, &srcfile_htab[hval]);
150 map_total_sz += h->maplen;
151 num_srcfiles++;
152 return h;
153
154out_map:
155 munmap(h->map, sz);
156out_fn:
157 zfree(&h->fn);
158out_h:
159 free(h);
160 return NULL;
161}
162
163/* Result is not 0 terminated */
164char *find_sourceline(char *fn, unsigned line, int *lenp)
165{
166 char *l, *p;
167 struct srcfile *sf = find_srcfile(fn);
168 if (!sf)
169 return NULL;
170 line--;
171 if (line >= sf->numlines)
172 return NULL;
173 l = sf->lines[line];
174 if (!l)
175 return NULL;
176 p = memchr(l, '\n', sf->map + sf->maplen - l);
177 *lenp = p - l;
178 return l;
179}
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"
20
21#define MAXSRCCACHE (32*1024*1024)
22#define MAXSRCFILES 64
23#define SRC_HTAB_SZ 64
24
25struct 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;
33};
34
35static struct hlist_head srcfile_htab[SRC_HTAB_SZ];
36static LIST_HEAD(srcfile_list);
37static long map_total_sz;
38static int num_srcfiles;
39
40static int countlines(char *map, int maplen)
41{
42 int numl;
43 char *end = map + maplen;
44 char *p = map;
45
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++;
52 }
53 if (p < end)
54 numl++;
55 return numl;
56}
57
58static void fill_lines(char **lines, int maxline, char *map, int maplen)
59{
60 int l;
61 char *end = map + maplen;
62 char *p = map;
63
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;
72 }
73 if (p < end)
74 lines[l] = p;
75}
76
77static void free_srcfile(struct srcfile *sf)
78{
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--;
87}
88
89static struct srcfile *find_srcfile(char *fn)
90{
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;
96
97 hlist_for_each_entry (h, &srcfile_htab[hval], hash_nd) {
98 if (!strcmp(fn, h->fn)) {
99 /* Move to front */
100 list_move(&h->nd, &srcfile_list);
101 return h;
102 }
103 }
104
105 /* Only prune if there is more than one entry */
106 while ((num_srcfiles > MAXSRCFILES || map_total_sz > MAXSRCCACHE) &&
107 srcfile_list.next != &srcfile_list) {
108 assert(!list_empty(&srcfile_list));
109 h = list_entry(srcfile_list.prev, struct srcfile, nd);
110 free_srcfile(h);
111 }
112
113 fd = open(fn, O_RDONLY);
114 if (fd < 0 || fstat(fd, &st) < 0) {
115 pr_debug("cannot open source file %s\n", fn);
116 return NULL;
117 }
118
119 h = malloc(sizeof(struct srcfile));
120 if (!h)
121 return NULL;
122
123 h->fn = strdup(fn);
124 if (!h->fn)
125 goto out_h;
126
127 h->maplen = st.st_size;
128 sz = (h->maplen + page_size - 1) & ~(page_size - 1);
129 h->map = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
130 close(fd);
131 if (h->map == (char *)-1) {
132 pr_debug("cannot mmap source file %s\n", fn);
133 goto out_fn;
134 }
135 h->numlines = countlines(h->map, h->maplen);
136 h->lines = calloc(h->numlines, sizeof(char *));
137 if (!h->lines)
138 goto out_map;
139 fill_lines(h->lines, h->numlines, h->map, h->maplen);
140 list_add(&h->nd, &srcfile_list);
141 hlist_add_head(&h->hash_nd, &srcfile_htab[hval]);
142 map_total_sz += h->maplen;
143 num_srcfiles++;
144 return h;
145
146out_map:
147 munmap(h->map, sz);
148out_fn:
149 zfree(&h->fn);
150out_h:
151 free(h);
152 return NULL;
153}
154
155/* Result is not 0 terminated */
156char *find_sourceline(char *fn, unsigned line, int *lenp)
157{
158 char *l, *p;
159 struct srcfile *sf = find_srcfile(fn);
160 if (!sf)
161 return NULL;
162 line--;
163 if (line >= sf->numlines)
164 return NULL;
165 l = sf->lines[line];
166 if (!l)
167 return NULL;
168 p = memchr(l, '\n', sf->map + sf->maplen - l);
169 *lenp = p - l;
170 return l;
171}