2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Nokia Corporation
7 * This software was developed by Attilio Rao for the IPSO project under
8 * contract to Nokia Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
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.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/queue.h>
46 /* NB: Make sure FNBUFF is as large as LNBUFF, otherwise it could overflow */
50 #define TMPPATH "/tmp/pmcannotate.XXXXXX"
52 #define FATAL(ptr, x ...) do { \
54 general_deleteall(); \
57 fprintf(stderr, ##x); \
63 #define PERCSAMP(x) ((x) * 100 / totalsamples)
66 TAILQ_ENTRY(entry) en_iter;
75 TAILQ_ENTRY(aggent) ag_fiter;
83 static struct aggent *agg_create(const char *name, u_int nsamples,
84 uintptr_t start, uintptr_t end);
85 static void agg_destroy(struct aggent *agg) __unused;
86 static void asmparse(FILE *fp);
87 static int cparse(FILE *fp);
88 static void entry_acqref(struct entry *entry);
89 static struct entry *entry_create(const char *name, uintptr_t pc,
90 uintptr_t start, uintptr_t end);
91 static void entry_destroy(struct entry *entry) __unused;
92 static void fqueue_compact(float th);
93 static void fqueue_deleteall(void);
94 static struct aggent *fqueue_findent_by_name(const char *name);
95 static int fqueue_getall(const char *bin, char *temp, int asmf);
96 static int fqueue_insertent(struct entry *entry);
97 static int fqueue_insertgen(void);
98 static void general_deleteall(void);
99 static struct entry *general_findent(uintptr_t pc);
100 static void general_insertent(struct entry *entry);
101 static void general_printasm(FILE *fp, struct aggent *agg);
102 static int general_printc(FILE *fp, struct aggent *agg);
103 static int printblock(FILE *fp, struct aggent *agg);
104 static void usage(const char *progname) __dead2;
106 static TAILQ_HEAD(, entry) mainlst = TAILQ_HEAD_INITIALIZER(mainlst);
107 static TAILQ_HEAD(, aggent) fqueue = TAILQ_HEAD_INITIALIZER(fqueue);
110 * Use a float value in order to automatically promote operations
111 * to return a float value rather than use casts.
113 static float totalsamples;
116 * Identifies a string cointaining objdump's assembly printout.
119 isasminline(const char *str)
124 if (sscanf(str, " %p%n", &ptr, &nbytes) != 1)
126 if (str[nbytes] != ':' || isspace(str[nbytes + 1]) == 0)
132 * Identifies a string containing objdump's assembly printout
133 * for a new function.
136 newfunction(const char *str)
144 if (sscanf(str, "%p <%[^>:]>:%n", &ptr, fname, &nbytes) != 2)
150 * Create a new first-level aggregation object for a specified
153 static struct aggent *
154 agg_create(const char *name, u_int nsamples, uintptr_t start, uintptr_t end)
158 agg = calloc(1, sizeof(struct aggent));
161 agg->ag_name = strdup(name);
162 if (agg->ag_name == NULL) {
166 agg->ag_nsamples = nsamples;
167 agg->ag_ostart = start;
173 * Destroy a first-level aggregation object for a specified
177 agg_destroy(struct aggent *agg)
185 * Analyze the "objdump -d" output, locate functions and start
186 * printing out the assembly functions content.
187 * We do not use newfunction() because we actually need the
188 * function name in available form, but the heurstic used is
194 char buffer[LNBUFF], fname[FNBUFF];
198 while (fgets(buffer, LNBUFF, fp) != NULL) {
199 if (isspace(buffer[0]))
201 if (sscanf(buffer, "%p <%[^>:]>:", &ptr, fname) != 2)
203 agg = fqueue_findent_by_name(fname);
206 agg->ag_offset = ftell(fp);
209 TAILQ_FOREACH(agg, &fqueue, ag_fiter) {
210 if (fseek(fp, agg->ag_offset, SEEK_SET) == -1)
212 printf("Profile trace for function: %s() [%.2f%%]\n",
213 agg->ag_name, PERCSAMP(agg->ag_nsamples));
214 general_printasm(fp, agg);
220 * Analyze the "objdump -S" output, locate functions and start
221 * printing out the C functions content.
222 * We do not use newfunction() because we actually need the
223 * function name in available form, but the heurstic used is
225 * In order to maintain the printout sorted, on the first pass it
226 * simply stores the file offsets in order to fastly moved later
227 * (when the file is hot-cached also) when the real printout will
233 char buffer[LNBUFF], fname[FNBUFF];
237 while (fgets(buffer, LNBUFF, fp) != NULL) {
238 if (isspace(buffer[0]))
240 if (sscanf(buffer, "%p <%[^>:]>:", &ptr, fname) != 2)
242 agg = fqueue_findent_by_name(fname);
245 agg->ag_offset = ftell(fp);
248 TAILQ_FOREACH(agg, &fqueue, ag_fiter) {
249 if (fseek(fp, agg->ag_offset, SEEK_SET) == -1)
251 printf("Profile trace for function: %s() [%.2f%%]\n",
252 agg->ag_name, PERCSAMP(agg->ag_nsamples));
253 if (general_printc(fp, agg) == -1)
261 * Bump the number of samples for any raw entry.
264 entry_acqref(struct entry *entry)
267 entry->en_nsamples++;
271 * Create a new raw entry object for a specified function.
273 static struct entry *
274 entry_create(const char *name, uintptr_t pc, uintptr_t start, uintptr_t end)
278 obj = calloc(1, sizeof(struct entry));
281 obj->en_name = strdup(name);
282 if (obj->en_name == NULL) {
287 obj->en_ostart = start;
289 obj->en_nsamples = 1;
294 * Destroy a raw entry object for a specified function.
297 entry_destroy(struct entry *entry)
300 free(entry->en_name);
305 * Specify a lower bound in percentage and drop from the
306 * first-level aggregation queue all the objects with a
310 fqueue_compact(float th)
313 struct aggent *agg, *tmpagg;
315 if (totalsamples == 0)
318 /* Revert the percentage calculation. */
319 thi = th * totalsamples / 100;
320 TAILQ_FOREACH_SAFE(agg, &fqueue, ag_fiter, tmpagg)
321 if (agg->ag_nsamples < thi)
322 TAILQ_REMOVE(&fqueue, agg, ag_fiter);
326 * Flush the first-level aggregates queue.
329 fqueue_deleteall(void)
333 while (TAILQ_EMPTY(&fqueue) == 0) {
334 agg = TAILQ_FIRST(&fqueue);
335 TAILQ_REMOVE(&fqueue, agg, ag_fiter);
340 * Insert a raw entry into the aggregations queue.
341 * If the respective first-level aggregation object
342 * does not exist create it and maintain it sorted
343 * in respect of the number of samples.
346 fqueue_insertent(struct entry *entry)
348 struct aggent *obj, *tmp;
352 TAILQ_FOREACH(obj, &fqueue, ag_fiter)
353 if (!strcmp(obj->ag_name, entry->en_name)) {
355 obj->ag_nsamples += entry->en_nsamples;
360 * If the first-level aggregation object already exists,
361 * just aggregate the samples and, if needed, resort
365 TAILQ_REMOVE(&fqueue, obj, ag_fiter);
367 TAILQ_FOREACH(tmp, &fqueue, ag_fiter)
368 if (obj->ag_nsamples > tmp->ag_nsamples) {
373 TAILQ_INSERT_BEFORE(tmp, obj, ag_fiter);
375 TAILQ_INSERT_TAIL(&fqueue, obj, ag_fiter);
380 * If the first-level aggregation object does not
381 * exist, create it and put in the sorted queue.
382 * If this is the first object, we need to set the
385 obj = agg_create(entry->en_name, entry->en_nsamples, entry->en_ostart,
389 if (TAILQ_EMPTY(&fqueue) != 0) {
390 TAILQ_INSERT_HEAD(&fqueue, obj, ag_fiter);
393 TAILQ_FOREACH(tmp, &fqueue, ag_fiter)
394 if (obj->ag_nsamples > tmp->ag_nsamples) {
399 TAILQ_INSERT_BEFORE(tmp, obj, ag_fiter);
401 TAILQ_INSERT_TAIL(&fqueue, obj, ag_fiter);
406 * Lookup a first-level aggregation object by name.
408 static struct aggent *
409 fqueue_findent_by_name(const char *name)
413 TAILQ_FOREACH(obj, &fqueue, ag_fiter)
414 if (!strcmp(obj->ag_name, name))
420 * Return the number of object in the first-level aggregations queue.
423 fqueue_getall(const char *bin, char *temp, int asmf)
425 char tmpf[MAXPATHLEN * 2 + 50];
427 uintptr_t start, end;
429 if (mkstemp(temp) == -1)
431 TAILQ_FOREACH(agg, &fqueue, ag_fiter) {
432 bzero(tmpf, sizeof(tmpf));
433 start = agg->ag_ostart;
437 * Fix-up the end address in order to show it in the objdump's
442 snprintf(tmpf, sizeof(tmpf),
443 "objdump --start-address=%p "
444 "--stop-address=%p -d %s >> %s", (void *)start,
445 (void *)end, bin, temp);
447 snprintf(tmpf, sizeof(tmpf),
448 "objdump --start-address=%p "
449 "--stop-address=%p -S %s >> %s", (void *)start,
450 (void *)end, bin, temp);
451 if (system(tmpf) != 0)
458 * Insert all the raw entries present in the general queue
459 * into the first-level aggregations queue.
462 fqueue_insertgen(void)
466 TAILQ_FOREACH(obj, &mainlst, en_iter)
467 if (fqueue_insertent(obj) == -1)
473 * Flush the raw entries general queue.
476 general_deleteall(void)
480 while (TAILQ_EMPTY(&mainlst) == 0) {
481 obj = TAILQ_FIRST(&mainlst);
482 TAILQ_REMOVE(&mainlst, obj, en_iter);
487 * Lookup a raw entry by the PC.
489 static struct entry *
490 general_findent(uintptr_t pc)
494 TAILQ_FOREACH(obj, &mainlst, en_iter)
495 if (obj->en_pc == pc)
501 * Insert a new raw entry in the general queue.
504 general_insertent(struct entry *entry)
507 TAILQ_INSERT_TAIL(&mainlst, entry, en_iter);
511 * Printout the body of an "objdump -d" assembly function.
512 * It does simply stops when a new function is encountered,
513 * bringing back the file position in order to not mess up
514 * subsequent analysis.
515 * C lines and others not recognized are simply skipped.
518 general_printasm(FILE *fp, struct aggent *agg)
525 while (fgets(buffer, LNBUFF, fp) != NULL) {
526 if ((nbytes = newfunction(buffer)) != 0) {
527 fseek(fp, nbytes * -1, SEEK_CUR);
530 if (!isasminline(buffer))
532 if (sscanf(buffer, " %p:", &ptr) != 1)
534 obj = general_findent((uintptr_t)ptr);
536 printf("\t| %s", buffer);
538 printf("%.2f%%\t| %s",
539 (float)obj->en_nsamples * 100 / agg->ag_nsamples,
545 * Printout the body of an "objdump -S" function.
546 * It does simply stops when a new function is encountered,
547 * bringing back the file position in order to not mess up
548 * subsequent analysis.
549 * It expect from the starting to the end to find, always, valid blocks
550 * (see below for an explanation of the "block" concept).
553 general_printc(FILE *fp, struct aggent *agg)
557 while (fgets(buffer, LNBUFF, fp) != NULL) {
558 fseek(fp, strlen(buffer) * -1, SEEK_CUR);
559 if (newfunction(buffer) != 0)
561 if (printblock(fp, agg) == -1)
568 * Printout a single block inside an "objdump -S" function.
569 * The block is composed of a first part in C and subsequent translation
571 * This code also operates a second-level aggregation packing together
572 * samples relative to PCs into a (lower bottom) block with their
573 * C (higher half) counterpart.
576 printblock(FILE *fp, struct aggent *agg)
582 int done, nbytes, sentinel;
586 * We expect the first thing of the block is C code, so simply give
587 * up if asm line is found.
592 if (fgets(buffer, LNBUFF, fp) == NULL)
594 if (isasminline(buffer) != 0)
597 nbytes = newfunction(buffer);
599 if (fseek(fp, nbytes * -1, SEEK_CUR) == -1)
606 * If the sentinel is not set, it means it did not match any
607 * "high half" for this code so simply give up.
608 * Operates the second-level aggregation.
614 if (sscanf(buffer, " %p:", &ptr) != 1)
616 obj = general_findent((uintptr_t)ptr);
618 tnsamples += obj->en_nsamples;
619 } while (fgets(buffer, LNBUFF, fp) != NULL && isasminline(buffer) != 0);
621 /* Rewind to the start of the block in order to start the printout. */
622 if (fseek(fp, lstart, SEEK_SET) == -1)
625 /* Again the high half of the block rappresenting the C part. */
627 while (fgets(buffer, LNBUFF, fp) != NULL && isasminline(buffer) == 0) {
628 if (tnsamples == 0 || done != 0)
629 printf("\t| %s", buffer);
632 printf("%.2f%%\t| %s",
633 (float)tnsamples * 100 / agg->ag_nsamples, buffer);
638 * Again the low half of the block rappresenting the asm
642 if (fgets(buffer, LNBUFF, fp) == NULL)
644 if (isasminline(buffer) == 0)
646 nbytes = newfunction(buffer);
648 if (fseek(fp, nbytes * -1, SEEK_CUR) == -1)
653 if (fseek(fp, strlen(buffer) * -1, SEEK_CUR) == -1)
659 * Helper printout functions.
662 usage(const char *progname)
666 "usage: %s [-a] [-h] [-k kfile] [-l lb] pmcraw.out binary\n",
672 main(int argc, char *argv[])
674 char buffer[LNBUFF], fname[FNBUFF], tbfl[] = TMPPATH, tofl[] = TMPPATH;
675 char tmpf[MAXPATHLEN * 2 + 50];
677 char *bin, *exec, *kfile, *ofile;
680 void *ptr, *hstart, *hend;
681 uintptr_t tmppc, ostart, oend;
690 while ((cget = getopt(argc, argv, "ahl:k:")) != -1)
699 limit = (float)atof(optarg);
713 if (access(bin, R_OK | F_OK) == -1)
714 FATAL(exec, "%s: Impossible to locate the binary file\n",
716 if (access(ofile, R_OK | F_OK) == -1)
717 FATAL(exec, "%s: Impossible to locate the pmcstat file\n",
719 if (kfile != NULL && access(kfile, R_OK | F_OK) == -1)
720 FATAL(exec, "%s: Impossible to locate the kernel file\n",
723 bzero(tmpf, sizeof(tmpf));
724 if (mkstemp(tofl) == -1)
725 FATAL(exec, "%s: Impossible to create the tmp file\n",
728 snprintf(tmpf, sizeof(tmpf), "pmcstat -k %s -R %s -m %s",
731 snprintf(tmpf, sizeof(tmpf), "pmcstat -R %s -m %s", ofile,
733 if (system(tmpf) != 0)
734 FATAL(exec, "%s: Impossible to create the tmp file\n",
737 gfp = fopen(tofl, "r");
739 FATAL(exec, "%s: Impossible to open the map file\n",
743 * Make the collection of raw entries from a pmcstat mapped file.
744 * The heuristic here wants strings in the form:
745 * "addr funcname startfaddr endfaddr".
747 while (fgets(buffer, LNBUFF, gfp) != NULL) {
748 if (isspace(buffer[0]))
750 if (sscanf(buffer, "%p %s %p %p\n", &ptr, fname,
751 &hstart, &hend) != 4)
753 "%s: Invalid scan of function in the map file\n",
755 ostart = (uintptr_t)hstart;
756 oend = (uintptr_t)hend;
757 tmppc = (uintptr_t)ptr;
759 obj = general_findent(tmppc);
764 obj = entry_create(fname, tmppc, ostart, oend);
767 "%s: Impossible to create a new object\n", exec);
768 general_insertent(obj);
770 if (fclose(gfp) == EOF)
771 FATAL(exec, "%s: Impossible to close the filedesc\n",
773 if (remove(tofl) == -1)
774 FATAL(exec, "%s: Impossible to remove the tmpfile\n",
778 * Remove the loose end objects and feed the first-level aggregation
781 if (fqueue_insertgen() == -1)
782 FATAL(exec, "%s: Impossible to generate an analysis\n",
784 fqueue_compact(limit);
785 if (fqueue_getall(bin, tbfl, asmsrc) == -1)
786 FATAL(exec, "%s: Impossible to create the tmp file\n",
789 bfp = fopen(tbfl, "r");
791 FATAL(exec, "%s: Impossible to open the binary file\n",
796 else if (cparse(bfp) == -1)
797 FATAL(NULL, "%s: Invalid format for the C file\n", exec);
798 if (fclose(bfp) == EOF)
799 FATAL(exec, "%s: Impossible to close the filedesc\n",
801 if (remove(tbfl) == -1)
802 FATAL(exec, "%s: Impossible to remove the tmpfile\n",