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/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/types.h> 37 #include <sys/cpuset.h> 38 #include <sys/event.h> 39 #include <sys/param.h> 40 #include <sys/socket.h> 41 #include <sys/stat.h> 42 #include <sys/module.h> 43 #include <sys/pmc.h> 44 45 #include <assert.h> 46 #include <err.h> 47 #include <pmc.h> 48 #include <pmclog.h> 49 #include <stdbool.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 #include "libpmcstat.h" 55 56 struct pmcstat_symbol * 57 pmcstat_symbol_search_by_name(struct pmcstat_process *pp, 58 const char *pi_name, const char *name, uintptr_t *addr_start, 59 uintptr_t *addr_end) 60 { 61 struct pmcstat_symbol *sym; 62 struct pmcstat_image *image; 63 struct pmcstat_pcmap *pcm; 64 const char *name1; 65 const char *name2; 66 bool found; 67 size_t i; 68 69 found = 0; 70 71 if (pp == NULL) 72 return (NULL); 73 74 TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) { 75 image = pcm->ppm_image; 76 if (image->pi_name == NULL) 77 continue; 78 name1 = pmcstat_string_unintern(image->pi_name); 79 if (strcmp(name1, pi_name) == 0) { 80 found = 1; 81 break; 82 } 83 } 84 85 if (!found || image->pi_symbols == NULL) 86 return (NULL); 87 88 found = 0; 89 90 for (i = 0; i < image->pi_symcount; i++) { 91 sym = &image->pi_symbols[i]; 92 name2 = pmcstat_string_unintern(sym->ps_name); 93 if (strcmp(name2, name) == 0) { 94 found = 1; 95 break; 96 } 97 } 98 99 if (!found) 100 return (NULL); 101 102 *addr_start = (image->pi_vaddr - image->pi_start + 103 pcm->ppm_lowpc + sym->ps_start); 104 *addr_end = (image->pi_vaddr - image->pi_start + 105 pcm->ppm_lowpc + sym->ps_end); 106 107 return (sym); 108 } 109 110 /* 111 * Helper function. 112 */ 113 114 int 115 pmcstat_symbol_compare(const void *a, const void *b) 116 { 117 const struct pmcstat_symbol *sym1, *sym2; 118 119 sym1 = (const struct pmcstat_symbol *) a; 120 sym2 = (const struct pmcstat_symbol *) b; 121 122 if (sym1->ps_end <= sym2->ps_start) 123 return (-1); 124 if (sym1->ps_start >= sym2->ps_end) 125 return (1); 126 return (0); 127 } 128 129 /* 130 * Map an address to a symbol in an image. 131 */ 132 133 struct pmcstat_symbol * 134 pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr) 135 { 136 struct pmcstat_symbol sym; 137 138 if (image->pi_symbols == NULL) 139 return (NULL); 140 141 sym.ps_name = NULL; 142 sym.ps_start = addr; 143 sym.ps_end = addr + 1; 144 145 return (bsearch((void *) &sym, image->pi_symbols, 146 image->pi_symcount, sizeof(struct pmcstat_symbol), 147 pmcstat_symbol_compare)); 148 } 149