xref: /freebsd/lib/libpmcstat/libpmcstat_symbol.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * Copyright (c) 2003-2008 Joseph Koshy
3  * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
4  * All rights reserved.
5  *
6  * This software was developed by BAE Systems, the University of Cambridge
7  * Computer Laboratory, and Memorial University under DARPA/AFRL contract
8  * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
9  * (TC) research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/cpuset.h>
35 #include <sys/event.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/module.h>
40 #include <sys/pmc.h>
41 
42 #include <assert.h>
43 #include <err.h>
44 #include <pmc.h>
45 #include <pmclog.h>
46 #include <stdbool.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "libpmcstat.h"
52 
53 struct pmcstat_symbol *
54 pmcstat_symbol_search_by_name(struct pmcstat_process *pp,
55     const char *pi_name, const char *name, uintptr_t *addr_start,
56     uintptr_t *addr_end)
57 {
58 	struct pmcstat_symbol *sym;
59 	struct pmcstat_image *image;
60 	struct pmcstat_pcmap *pcm;
61 	const char *name1;
62 	const char *name2;
63 	bool found;
64 	size_t i;
65 
66 	found = 0;
67 
68 	if (pp == NULL)
69 		return (NULL);
70 
71 	TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) {
72 		image = pcm->ppm_image;
73 		if (image->pi_name == NULL)
74 			continue;
75 		name1 = pmcstat_string_unintern(image->pi_name);
76 		if (strcmp(name1, pi_name) == 0) {
77 			found = 1;
78 			break;
79 		}
80 	}
81 
82 	if (!found || image->pi_symbols == NULL)
83 		return (NULL);
84 
85 	found = 0;
86 
87 	for (i = 0; i < image->pi_symcount; i++) {
88 		sym = &image->pi_symbols[i];
89 		name2 = pmcstat_string_unintern(sym->ps_name);
90 		if (strcmp(name2, name) == 0) {
91 			found = 1;
92 			break;
93 		}
94 	}
95 
96 	if (!found)
97 		return (NULL);
98 
99 	*addr_start = (image->pi_vaddr - image->pi_start +
100 	    pcm->ppm_lowpc + sym->ps_start);
101 	*addr_end = (image->pi_vaddr - image->pi_start +
102 	    pcm->ppm_lowpc + sym->ps_end);
103 
104 	return (sym);
105 }
106 
107 /*
108  * Helper function.
109  */
110 
111 int
112 pmcstat_symbol_compare(const void *a, const void *b)
113 {
114 	const struct pmcstat_symbol *sym1, *sym2;
115 
116 	sym1 = (const struct pmcstat_symbol *) a;
117 	sym2 = (const struct pmcstat_symbol *) b;
118 
119 	if (sym1->ps_end <= sym2->ps_start)
120 		return (-1);
121 	if (sym1->ps_start >= sym2->ps_end)
122 		return (1);
123 	return (0);
124 }
125 
126 /*
127  * Map an address to a symbol in an image.
128  */
129 
130 struct pmcstat_symbol *
131 pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr)
132 {
133 	struct pmcstat_symbol sym;
134 
135 	if (image->pi_symbols == NULL)
136 		return (NULL);
137 
138 	sym.ps_name  = NULL;
139 	sym.ps_start = addr;
140 	sym.ps_end   = addr + 1;
141 
142 	return (bsearch((void *) &sym, image->pi_symbols,
143 	    image->pi_symcount, sizeof(struct pmcstat_symbol),
144 	    pmcstat_symbol_compare));
145 }
146