10b86b1bbSFabien Thomas /*- 2*1de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*1de7b4b8SPedro F. Giffuni * 40b86b1bbSFabien Thomas * Copyright (c) 2005-2007, Joseph Koshy 50b86b1bbSFabien Thomas * Copyright (c) 2007 The FreeBSD Foundation 60b86b1bbSFabien Thomas * All rights reserved. 70b86b1bbSFabien Thomas * 80b86b1bbSFabien Thomas * Portions of this software were developed by A. Joseph Koshy under 90b86b1bbSFabien Thomas * sponsorship from the FreeBSD Foundation and Google, Inc. 100b86b1bbSFabien Thomas * 110b86b1bbSFabien Thomas * Redistribution and use in source and binary forms, with or without 120b86b1bbSFabien Thomas * modification, are permitted provided that the following conditions 130b86b1bbSFabien Thomas * are met: 140b86b1bbSFabien Thomas * 1. Redistributions of source code must retain the above copyright 150b86b1bbSFabien Thomas * notice, this list of conditions and the following disclaimer. 160b86b1bbSFabien Thomas * 2. Redistributions in binary form must reproduce the above copyright 170b86b1bbSFabien Thomas * notice, this list of conditions and the following disclaimer in the 180b86b1bbSFabien Thomas * documentation and/or other materials provided with the distribution. 190b86b1bbSFabien Thomas * 200b86b1bbSFabien Thomas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 210b86b1bbSFabien Thomas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 220b86b1bbSFabien Thomas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 230b86b1bbSFabien Thomas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 240b86b1bbSFabien Thomas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 250b86b1bbSFabien Thomas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 260b86b1bbSFabien Thomas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 270b86b1bbSFabien Thomas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 280b86b1bbSFabien Thomas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 290b86b1bbSFabien Thomas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 300b86b1bbSFabien Thomas * SUCH DAMAGE. 310b86b1bbSFabien Thomas */ 320b86b1bbSFabien Thomas 330b86b1bbSFabien Thomas /* 340b86b1bbSFabien Thomas * Transform a hwpmc(4) log into human readable form, and into 350b86b1bbSFabien Thomas * gprof(1) compatible profiles. 360b86b1bbSFabien Thomas */ 370b86b1bbSFabien Thomas 380b86b1bbSFabien Thomas #include <sys/cdefs.h> 390b86b1bbSFabien Thomas __FBSDID("$FreeBSD$"); 400b86b1bbSFabien Thomas 410b86b1bbSFabien Thomas #include <sys/param.h> 420b86b1bbSFabien Thomas #include <sys/endian.h> 430b86b1bbSFabien Thomas #include <sys/gmon.h> 440b86b1bbSFabien Thomas #include <sys/imgact_aout.h> 450b86b1bbSFabien Thomas #include <sys/imgact_elf.h> 460b86b1bbSFabien Thomas #include <sys/mman.h> 470b86b1bbSFabien Thomas #include <sys/pmc.h> 480b86b1bbSFabien Thomas #include <sys/queue.h> 490b86b1bbSFabien Thomas #include <sys/socket.h> 500b86b1bbSFabien Thomas #include <sys/stat.h> 510b86b1bbSFabien Thomas #include <sys/wait.h> 520b86b1bbSFabien Thomas 530b86b1bbSFabien Thomas #include <netinet/in.h> 540b86b1bbSFabien Thomas 550b86b1bbSFabien Thomas #include <assert.h> 560b86b1bbSFabien Thomas #include <err.h> 570b86b1bbSFabien Thomas #include <errno.h> 580b86b1bbSFabien Thomas #include <fcntl.h> 590b86b1bbSFabien Thomas #include <gelf.h> 600b86b1bbSFabien Thomas #include <libgen.h> 610b86b1bbSFabien Thomas #include <limits.h> 620b86b1bbSFabien Thomas #include <netdb.h> 630b86b1bbSFabien Thomas #include <pmc.h> 640b86b1bbSFabien Thomas #include <pmclog.h> 650b86b1bbSFabien Thomas #include <sysexits.h> 660b86b1bbSFabien Thomas #include <stdint.h> 670b86b1bbSFabien Thomas #include <stdio.h> 680b86b1bbSFabien Thomas #include <stdlib.h> 690b86b1bbSFabien Thomas #include <string.h> 700b86b1bbSFabien Thomas #include <unistd.h> 710b86b1bbSFabien Thomas 720b86b1bbSFabien Thomas #include "pmcstat.h" 730b86b1bbSFabien Thomas #include "pmcstat_log.h" 740b86b1bbSFabien Thomas #include "pmcpl_annotate.h" 750b86b1bbSFabien Thomas 760b86b1bbSFabien Thomas /* 770b86b1bbSFabien Thomas * Record a callchain. 780b86b1bbSFabien Thomas */ 790b86b1bbSFabien Thomas 800b86b1bbSFabien Thomas void 810b86b1bbSFabien Thomas pmcpl_annotate_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr, 820b86b1bbSFabien Thomas uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu) 830b86b1bbSFabien Thomas { 840b86b1bbSFabien Thomas struct pmcstat_pcmap *map; 850b86b1bbSFabien Thomas struct pmcstat_symbol *sym; 860b86b1bbSFabien Thomas uintfptr_t newpc; 870b86b1bbSFabien Thomas struct pmcstat_image *image; 880b86b1bbSFabien Thomas 890b86b1bbSFabien Thomas (void) pmcr; (void) nsamples; (void) usermode; (void) cpu; 900b86b1bbSFabien Thomas 910b86b1bbSFabien Thomas map = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, cc[0]); 920b86b1bbSFabien Thomas if (map == NULL) { 930b86b1bbSFabien Thomas /* Unknown offset. */ 940b86b1bbSFabien Thomas pmcstat_stats.ps_samples_unknown_offset++; 950b86b1bbSFabien Thomas return; 960b86b1bbSFabien Thomas } 970b86b1bbSFabien Thomas 980b86b1bbSFabien Thomas assert(cc[0] >= map->ppm_lowpc && cc[0] < map->ppm_highpc); 990b86b1bbSFabien Thomas 1000b86b1bbSFabien Thomas image = map->ppm_image; 1010b86b1bbSFabien Thomas newpc = cc[0] - (map->ppm_lowpc + 1020b86b1bbSFabien Thomas (image->pi_vaddr - image->pi_start)); 1030b86b1bbSFabien Thomas sym = pmcstat_symbol_search(image, newpc); 1040b86b1bbSFabien Thomas if (sym == NULL) 1050b86b1bbSFabien Thomas return; 1060b86b1bbSFabien Thomas 1070b86b1bbSFabien Thomas fprintf(args.pa_graphfile, "%p %s 0x%jx 0x%jx\n", 1080b86b1bbSFabien Thomas (void *)cc[0], 1090b86b1bbSFabien Thomas pmcstat_string_unintern(sym->ps_name), 1100b86b1bbSFabien Thomas (uintmax_t)(sym->ps_start + 1110b86b1bbSFabien Thomas image->pi_vaddr), (uintmax_t)(sym->ps_end + 1120b86b1bbSFabien Thomas image->pi_vaddr)); 1130b86b1bbSFabien Thomas } 114