xref: /freebsd/usr.sbin/pmcstat/pmcpl_annotate_cg.c (revision b740c88bfb6453416926271c089262e7164dace3)
1 /*-
2  * Copyright (c) 2005-2007, Joseph Koshy
3  * Copyright (c) 2007 The FreeBSD Foundation
4  * Copyright (c) 2014, Adrian Chadd, Netflix Inc.
5  * All rights reserved.
6  *
7  * Portions of this software were developed by A. Joseph Koshy under
8  * sponsorship from the FreeBSD Foundation and Google, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Transform a hwpmc(4) log into human readable form, and into
34  * gprof(1) compatible profiles.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/endian.h>
42 #include <sys/gmon.h>
43 #include <sys/imgact_aout.h>
44 #include <sys/imgact_elf.h>
45 #include <sys/mman.h>
46 #include <sys/pmc.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/stat.h>
50 #include <sys/wait.h>
51 
52 #include <netinet/in.h>
53 
54 #include <assert.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <gelf.h>
59 #include <libgen.h>
60 #include <limits.h>
61 #include <netdb.h>
62 #include <pmc.h>
63 #include <pmclog.h>
64 #include <sysexits.h>
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 
71 #include "pmcstat.h"
72 #include "pmcstat_log.h"
73 #include "pmcpl_annotate_cg.h"
74 
75 /*
76  * Record a callchain.
77  */
78 
79 void
80 pmcpl_annotate_cg_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
81     uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
82 {
83 	struct pmcstat_pcmap *map;
84 	struct pmcstat_symbol *sym;
85 	uintfptr_t newpc;
86 	struct pmcstat_image *image;
87 	int i;
88 	char filename[PATH_MAX], funcname[PATH_MAX];
89 	unsigned sline;
90 
91 	(void) pmcr; (void) nsamples; (void) usermode; (void) cpu;
92 
93 	for (i = 0; i < (int) nsamples; i++) {
94 		map = NULL;
95 		sym = NULL;
96 		image = NULL;
97 		filename[0] = '\0';
98 		funcname[0] = '\0';
99 		sline = 0;
100 
101 		map = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, cc[i]);
102 		if (map != NULL) {
103 			assert(cc[i] >= map->ppm_lowpc && cc[i] < map->ppm_highpc);
104 			image = map->ppm_image;
105 			newpc = cc[i] - (map->ppm_lowpc +
106 				(image->pi_vaddr - image->pi_start));
107 			sym = pmcstat_symbol_search(image, newpc);
108 		}
109 
110 		if (map != NULL && image != NULL && sym != NULL) {
111 			(void) pmcstat_image_addr2line(image, cc[i],
112 			    filename, sizeof(filename), &sline, funcname, sizeof(funcname));
113 		}
114 
115 		if (map != NULL && sym != NULL) {
116 			fprintf(args.pa_graphfile, "%p %s %s:%d\n",
117 			    (void *)cc[i],
118 			    funcname,
119 			    filename,
120 			    sline);
121 		} else {
122 			fprintf(args.pa_graphfile, "%p <unknown> ??:0\n",
123 			    (void *) cc[i]);
124 		}
125 	}
126 	fprintf(args.pa_graphfile, "--\n");
127 }
128