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