Debian Codegraph @

runtime/transport/symbols.c

raw
1 /* -*- linux-c -*-
2 * symbols.c - stp symbol and module functions
3 *
4 * Copyright (C) Red Hat Inc, 2006-2020
5 *
6 * This file is part of systemtap, and is free software. You can
7 * redistribute it and/or modify it under the terms of the GNU General
8 * Public License (GPL); either version 2, or (at your option) any
9 * later version.
10 */
11
12 #ifndef _STP_SYMBOLS_C_
13 #define _STP_SYMBOLS_C_
14 #include "../sym.h"
15 #include "relay_compat.h"
16
17 #ifdef STAPCONF_KERNEL_READ_FILE_FROM_PATH_OFFSET
18 // XXX kernel commit b89999d004931ab2e51236 for v5.10-rc1 split
19 // kernel_read_file_* functions into a separate header.
20 #include <linux/kernel_read_file.h>
21 #endif
22
23 #ifndef KERN_CONT
24 #define KERN_CONT ""
25 #endif
26
27 static int _stp_kmodule_check (const char*);
28
29 /* PR12612: pre-commit-3abb860f values */
30
31 #define STP13_MODULE_NAME_LEN 64
32 #define STP13_SYMBOL_NAME_LEN 64
33 struct _stp13_msg_relocation {
34 char module[STP13_MODULE_NAME_LEN];
35 char reloc[STP13_SYMBOL_NAME_LEN];
36 uint64_t address;
37 };
38
39 static void _stp_set_stext(uint64_t address)
40 {
41 if (address == 0)
42 _stp_warn("No load address found _stext. Kernel probes and addresses may not be available.");
43 else
44 dbug_sym(1, "found kernel _stext load address: 0x%lx\n",
45 (unsigned long) address);
46 if (_stp_kretprobe_trampoline != (unsigned long) -1)
47 _stp_kretprobe_trampoline += (unsigned long) address;
48 }
49
50 static void _stp_do_relocation(const char __user *buf, size_t count)
51 {
52 static struct _stp_msg_relocation msg; /* by protocol, never concurrently used */
53 static struct _stp13_msg_relocation msg13; /* ditto */
54 int rc = 0;
55
56 dbug_sym(2, "do_relocation size=%ld\n", (long) count);
57
58 /* PR12612: Let's try to be compatible with systemtap modules being
59 compiled by new systemtap, but loaded (staprun'd) by an older
60 systemtap runtime. The only known incompatilibility is that we
61 get an older, smaller, relocation message. So here we accept both
62 sizes. */
63 if (sizeof(msg) == count) { /* systemtap 1.4+ runtime */
64 rc = copy_from_user (& msg, buf, count);
65
66 } else if (sizeof(msg13) == count) { /* systemtap 1.3- runtime */
67
68 rc = copy_from_user (& msg13, buf, count);
69 if (rc == 0) {
70 #if STP_MODULE_NAME_LEN <= STP13_MODULE_NAME_LEN
71 #error "STP_MODULE_NAME_LEN should not be smaller than STP13_MODULE_NAME_LEN"
72 #endif
73 strlcpy (msg.module, msg13.module, STP13_MODULE_NAME_LEN);
74 strlcpy (msg.reloc, msg13.reloc, STP13_MODULE_NAME_LEN);
75 msg.address = msg13.address;
76 }
77 } else {
78 errk ("STP_RELOCATE message size mismatch (%lu or %lu vs %lu)\n",
79 (long unsigned) sizeof(msg), (long unsigned) sizeof (msg13), (long unsigned) count);
80 return;
81 }
82
83 dbug_sym(2, "relocate (%s %s 0x%lx) rc=%d\n", msg.module, msg.reloc, (unsigned long) msg.address, rc);
84 if (rc != 0)
85 return;
86
87 /* Detect actual kernel load address. */
88 if (!strcmp ("kernel", msg.module)
89 && !strcmp ("_stext", msg.reloc)) {
90 #ifdef CONFIG_KALLSYMS
91 // PR14555, PR26074: kptr_restrict=2 may hide _stext from
92 // staprun. We fall back by calling kallsyms_lookup_name,
93 // but this may need to be done later once
94 // kallsyms_lookup_name has been passed via relocation:
95 #if !defined(STAPCONF_KALLSYMS_LOOKUP_NAME_EXPORTED)
96 if (msg.address == 0)
97 _stp_need_kallsyms_stext = 1;
98 else
99 _stp_set_stext(msg.address);
100 #else
101 if (msg.address == 0)
102 msg.address = kallsyms_lookup_name("_stext");
103 _stp_set_stext(msg.address);
104 #endif
105 #else
106 _stp_set_stext(msg.address);
107 #endif
108 }
109
110 #if !defined(STAPCONF_KALLSYMS_LOOKUP_NAME_EXPORTED)
111 if (!strcmp ("kernel", msg.module)
112 && !strcmp ("kallsyms_lookup_name", msg.reloc)) {
113 _stp_kallsyms_lookup_name = (void *) msg.address;
114 }
115 #endif
116 #if defined(STAPCONF_KALLSYMS_ON_EACH_SYMBOL) && !defined(STAPCONF_KALLSYMS_ON_EACH_SYMBOL_EXPORTED)
117 if (!strcmp ("kernel", msg.module)
118 && !strcmp ("kallsyms_on_each_symbol", msg.reloc)) {
119 _stp_kallsyms_on_each_symbol = (void *) msg.address;
120 }
121 #endif
122 if (!strcmp ("kernel", msg.module)
123 && !strcmp ("module_kallsyms_on_each_symbol", msg.reloc)) {
124 _stp_module_kallsyms_on_each_symbol = (void *) msg.address;
125 }
126
127 _stp_kmodule_update_address(msg.module, msg.reloc, msg.address);
128 }
129
130
131
132 /* Module section attributes tell us where module sections are/were
133 loaded intoi kernel memory. In the kernel APIs, these have gone
134 through enough change over the years, that we roll our own now.
135 */
136 struct stap_module_sect_attr
137 {
138 const char* name;
139 unsigned long address;
140 };
141
142 struct stap_module_sect_attrs
143 {
144 unsigned nsections;
145 struct stap_module_sect_attr *sections;
146 };
147
148
149 // Allocates & fills in as->nsections and as->attrs[]. Addresses may be 0L
150 // if addresses unknown or invalid. Free with put_() function below. Do not
151 // keep alive any longer than target module's notifier callback, as section
152 // names (in attrs[]) are considered static. In case of error, mod->nsections
153 // may be zero and/or mod->sections[i].address may be zero.
154 void get_module_sect_attrs(struct module* mod,
155 struct stap_module_sect_attrs* as);
156
157 void put_module_sect_attrs(struct stap_module_sect_attrs* as);
158
159 void put_module_sect_attrs(struct stap_module_sect_attrs* as)
160 {
161 _stp_kfree(as->sections);
162 }
163
164 static ssize_t _stp_read_file_from_path(const char *path, char *buf, size_t len)
165 {
166 struct file *file;
167 loff_t pos = 0;
168 ssize_t ret;
169
170 file = filp_open(path, O_RDONLY, 0);
171 if (IS_ERR(file))
172 return PTR_ERR(file);
173
174 #ifdef STAPCONF_KERNEL_READ_NEW_ARGS
175 ret = kernel_read(file, buf, len, &pos);
176 #else
177 ret = kernel_read(file, pos, buf, len);
178 #endif
179 fput(file);
180 return ret;
181 }
182
183 static unsigned long
184 read_sect_sysfs(const char* module, const char *section)
185 {
186 char *pathname = __getname(); // PATH_MAX sized
187 int rc;
188 unsigned long addr = 0;
189 char buffer[32] = {}; // Sections files are 19 bytes in size at most
190
191 if (pathname == 0)
192 goto out;
193 rc = snprintf(pathname, PATH_MAX, "/sys/module/%s/sections/%s", module, section);
194 if (rc >= PATH_MAX)
195 goto out1;
196 rc = _stp_read_file_from_path(pathname, buffer, sizeof(buffer));
197 if (rc <= 0)
198 goto out1;
199 rc = kstrtoul(buffer, 0, &addr);
200 if (rc != 0) // parse error?
201 addr = 0L;
202 out1:
203 __putname(pathname);
204 out:
205 dbug_sym(2, "module %s section %s address 0x%lu\n",
206 module, section, addr);
207
208 return addr;
209 }
210
211
212 void get_module_sect_attrs(struct module* mod,
213 struct stap_module_sect_attrs* as)
214 {
215 unsigned i;
216
217 // Guess at the maximal set of sections we're likely to encounter
218 // as relevant for probing, $context var setting, unwinding; more welcome.
219 // It's not easy to say "all" because we can't enumerate them
220 const char* key_sections[] = { ".init", ".text", ".eh_frame",
221 ".text.unlikely", ".data", ".rodata",
222 ".symtab" };
223 as->nsections = sizeof(key_sections)/sizeof(key_sections[0]);
224 as->sections = _stp_kzalloc(as->nsections * sizeof(struct stap_module_sect_attr));
225 if (as->sections == 0) {
226 goto out1;
227 }
228
229 for (i=0; i<as->nsections; i++) {
230 as->sections[i].name = key_sections[i];
231 as->sections[i].address = read_sect_sysfs(mod->name, key_sections[i]);
232 }
233
234 goto out;
235
236 out1:
237 as->nsections = 0; // prevent later kfrees run amok
238 out:
239 return;
240 }
241
242 static int _stp_module_notifier (struct notifier_block * nb,
243 unsigned long val, void *data)
244 {
245 struct module *mod = data;
246 if (!mod) { // so as to avoid null pointer checks later
247 WARN_ON (!mod);
248 return NOTIFY_DONE;
249 }
250
251 dbug_sym(1, "module notify %lu %s\n",
252 val, mod->name);
253
254 /* Prior to 2.6.11, struct module contained a module_sections
255 attribute vector rather than module_sect_attrs. Prior to
256 2.6.19, module_sect_attrs lacked a number-of-sections
257 field. Past 3.8, MODULE_STATE_COMING is sent too late to
258 let us probe module init functions.
259
260 Without CONFIG_KALLSYMS, we don't get any of the
261 related fields at all in struct module. XXX: autoconf for
262 that directly? */
263
264 #if defined(CONFIG_KALLSYMS)
265 // After kernel commit 4982223e51, module notifiers are being
266 // called too early to get module section info. So, we have to
267 // switch to using symbol+offset probing for modules.
268 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
269 // The module refresh code (in systemtap_module_refresh)
270 // assumes the 1st call is on module load and the 2nd is on
271 // module unload. So, we can't call systemtap_module_refresh()
272 // twice for module load (once for MODULE_STATE_COMING and
273 // once for MODULE_STATE_LIVE). In the MODULE_STATE_COMING
274 // state, the module's init function hasn't fired yet and we
275 // can register symbol+offset probes. In the MODULE_STATE_LIVE
276 // state, the module's init function has already been run (and
277 // the init section has been discarded). So, we'll ignore
278 // MODULE_STATE_LIVE.
279 if (val == MODULE_STATE_COMING) {
280 /* Verify build-id. */
281 _stp_kmodule_check (mod->name);
282 }
283 else if (val == MODULE_STATE_GOING) {
284 /* Unregister all sections. */
285 dbug_sym(2, "unregister sections\n");
286 _stp_kmodule_update_address(mod->name, NULL, 0);
287 }
288 else if (val != MODULE_STATE_GOING) {
289 return NOTIFY_DONE;
290 }
291 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
292 if (val == MODULE_STATE_COMING ||
293 val == MODULE_STATE_LIVE) {
294 /* A module is arriving or has arrived. Register all
295 of its section addresses, as though staprun sent us
296 a bunch of STP_RELOCATE messages. Now ... where
297 did the fishie go? */
298
299 struct stap_module_sect_attrs attrs;
300 unsigned i, nsections;
301 get_module_sect_attrs (mod, &attrs);
302
303 for (i=0; i<attrs.nsections; i++) {
304 int init_p = (strstr(attrs.sections[i].name, "init.") != NULL);
305 int init_gone_p = (val == MODULE_STATE_LIVE); // likely already unloaded
306
307 _stp_kmodule_update_address(mod->name,
308 attrs.sections[i].name,
309 ((init_p && init_gone_p) ? 0 : attrs.sections[i].address));
310 }
311
312 put_module_sect_attrs (&attrs);
313
314 /* Verify build-id. */
315 if (_stp_kmodule_check (mod->name))
316 _stp_kmodule_update_address(mod->name, NULL, 0); /* Pretend it was never here. */
317 }
318 else if (val == MODULE_STATE_GOING) {
319 /* Unregister all sections. */
320 _stp_kmodule_update_address(mod->name, NULL, 0);
321 }
322 else
323 return NOTIFY_DONE;
324 #endif
325
326 /* Give the probes a chance to update themselves. */
327 /* Proper kprobes support for this appears to be relatively
328 recent. Example prerequisite commits: 0deddf436a f24659d9 */
329 systemtap_module_refresh(mod->name);
330
331 #endif /* skipped for ancient or kallsyms-free kernels */
332
333 return NOTIFY_DONE;
334 }
335
336 static int _stp_module_update_self (void)
337 {
338 /* Only bother if we need unwinding and have module_sect_attrs. */
339 /* Or if we need to figure out the addr->file:line mapping */
340 #if (defined(STP_USE_DWARF_UNWINDER) && defined(STP_NEED_UNWIND_DATA)) \
341 || defined(STP_NEED_LINE_DATA)
342 #if defined(CONFIG_KALLSYMS)
343
344 bool found_eh_frame = false;
345 struct module *mod = THIS_MODULE;
346 struct stap_module_sect_attrs attrs;
347 int rc;
348 unsigned i;
349
350 /* We've already been inserted at this point, so the path variable will
351 * still be unique. */
352 _stp_module_self.name = mod->name;
353 _stp_module_self.path = mod->name;
354
355 get_module_sect_attrs (mod, &attrs);
356
357 for (i=0; i<attrs.nsections; i++) {
358 const char* aname = attrs.sections[i].name;
359 unsigned long address = attrs.sections[i].address;
360
361 if (! aname)
362 continue;
363 if(!strcmp(".note.gnu.build-id",aname)) {
364 _stp_module_self.notes_sect = address;
365 }
366 else if (!strcmp(".eh_frame", aname)) {
367 _stp_module_self.eh_frame = (void*)address;
368 _stp_module_self.eh_frame_len = 0;
369 found_eh_frame = true;
370 }
371 else if (!strcmp(".symtab", aname)) {
372 #ifdef STAPCONF_MOD_KALLSYMS
373 struct mod_kallsyms *kallsyms;
374
375 rcu_read_lock_sched();
376 kallsyms = rcu_dereference_sched(mod->kallsyms);
377 rcu_read_unlock_sched();
378
379 if (address == (unsigned long) kallsyms->symtab)
380 _stp_module_self.sections[0].size =
381 kallsyms->num_symtab * sizeof(kallsyms->symtab[0]);
382 #else
383 if (address == (unsigned long) mod->symtab)
384 _stp_module_self.sections[0].size =
385 mod->num_symtab * sizeof(mod->symtab[0]);
386 #endif
387 _stp_module_self.sections[0].static_addr = address;
388 }
389 else if (!strcmp(".text", aname)) {
390 _stp_module_self.sections[1].static_addr = address;
391 #ifdef STAPCONF_MODULE_LAYOUT
392 _stp_module_self.sections[1].size = mod->core_layout.text_size;
393 #elif defined(STAPCONF_MODULE_MEMORY)
394 _stp_module_self.sections[1].size = mod->mem[MOD_TEXT].size;
395 #elif defined(STAPCONF_GRSECURITY)
396 _stp_module_self.sections[1].size = mod->core_size_rx;
397 #else
398 _stp_module_self.sections[1].size = mod->core_text_size;
399 #endif
400 }
401 }
402
403 if (found_eh_frame) {
404 /* Scan again for an upper bound on eh_frame_len, deduced from
405 * the position of the next closest section. (if any!) */
406 const unsigned long base = (unsigned long) _stp_module_self.eh_frame;
407 unsigned long maxlen = 0, len = 0;
408 for (i=0; i<attrs.nsections; i++) {
409 unsigned long address = attrs.sections[i].address;
410 if (base < address && (maxlen == 0 || address < base + maxlen))
411 maxlen = address - base;
412 }
413
414 /* The length could be smaller, especially if the next section
415 * has alignment padding. Walking the fde determines the real
416 * eh_frame length. There should be a 0x00000000 terminator
417 * word added by translate.cxx's T_800 auxiliary file, but
418 * check our maxlen bound just in case. */
419 while (len + sizeof(u32) <= maxlen) {
420 unsigned long offset = get_unaligned((u32*)(base + len));
421 if (offset == 0 || offset > maxlen - len - sizeof(u32))
422 break; /* 0-terminator, or out of bounds */
423 len += sizeof(u32) + offset;
424 }
425 _stp_module_self.eh_frame_len = len;
426 }
427
428 put_module_sect_attrs (&attrs);
429
430 #endif /* defined(CONFIG_KALLSYMS) */
431 #endif /* (defined(STP_USE_DWARF_UNWINDER) && defined(STP_NEED_UNWIND_DATA))
432 || defined(STP_NEED_LINE_DATA) */
433
434 return 0;
435 }
436
437 /* Notification function to call on a kernel panic */
438 static int _stp_module_panic_notifier (struct notifier_block *nb, unsigned long val, void *data)
439 {
440 int i;
441
442 if (unlikely(_stp_relay_data.rchan == NULL))
443 {
444 printk(KERN_ERR "No _stp_relay_data.rchan\n");
445 return NOTIFY_DONE;
446 }
447
448 /* Loop over each cpu buffer */
449 for_each_possible_cpu(i)
450 {
451 int j=0;
452 struct rchan_buf * sub_buf;
453 char *subbuf_start;
454 char *previous;
455 char *next;
456 size_t bytes_passed;
457 int printed;
458 int first_iteration;
459
460 sub_buf = _stp_get_rchan_subbuf(_stp_relay_data.rchan->buf, i);
461 if (unlikely(sub_buf == NULL))
462 break;
463
464 /* Set our pointer to the beginning of the channel buffer */
465 subbuf_start = (char *)sub_buf->start;
466
467 /* Loop over each sub buffer */
468 for (j=0; j< sub_buf->chan->n_subbufs; j++)
469 {
470 /* Ensure our start is not NULL */
471 if(subbuf_start == NULL)
472 {
473 printk(KERN_EMERG "Current buffer is NULL\n");
474 return NOTIFY_DONE;
475 }
476
477 bytes_passed = 0; /* Keep track of the number of bytes already passed */
478 first_iteration = 1; /* Flag for keeping track of the 1st itteration*/
479 printed = 0; /* Flag for keeping track of when we've already printed the
480 * message about what info might be new */
481
482 previous = subbuf_start;
483 next = strchr(previous, '\n');
484 bytes_passed+= (next - previous);
485
486 /* Loop over the whole buffer, printing line by line */
487 while (next != NULL && bytes_passed < sub_buf->chan->subbuf_size)
488 {
489
490 if(first_iteration)
491 {
492 printk(KERN_CONT "%s trace buffer for processor %d sub-buffer %d:\n",
493 THIS_MODULE->name, i, j);
494 }
495
496 /* Once we reach the number of bytes consumed on the last
497 * sub-buffer filled, print a message saying that everything
498 * from then on might not have made it to the display before
499 * the kernel panic */
500 if(subbuf_start == sub_buf->data
501 && bytes_passed >= sub_buf->bytes_consumed
502 && !printed)
503 {
504 printk(KERN_CONT
505 "The following may not have been sent to the display:\n");
506 printed = 1;
507 }
508
509 /* Print the line. Other than the first itteration, we need to print everything
510 * except the first '\n' character.*/
511 if(first_iteration)
512 {
513 printk(KERN_CONT "%.*s\n", (int)(next - previous), previous);
514 first_iteration = 0;
515 }
516 else
517 {
518 printk(KERN_CONT "%.*s\n", (int)(next - previous)-1, previous+1);
519 }
520
521 /* Get the next token */
522 previous = next;
523 next = strchr(next + 1, '\n');
524 if(next != NULL)
525 {
526 bytes_passed+= (next - previous);
527 }
528 }
529
530 /* Move on to the next sub-buffer */
531 subbuf_start = subbuf_start + sub_buf->chan->subbuf_size;
532 }
533 }
534 return NOTIFY_DONE;
535 }
536
537 #endif /* _STP_SYMBOLS_C_ */