2 * Copyright (c) 2008 Nokia Corporation
5 * This software was developed by Attilio Rao for the IPSO project under
6 * contract to Nokia Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/queue.h>
47 #define TMPPATH "/tmp/pmcannotate.XXXXXX"
49 #define FATAL(ptr, x ...) do { \
51 general_deleteall(); \
54 fprintf(stderr, ##x); \
60 #define PERCSAMP(x) ((x) * 100 / totalsamples)
63 TAILQ_ENTRY(entry) en_iter;
72 TAILQ_ENTRY(aggent) ag_fiter;
80 static struct aggent *agg_create(const char *name, u_int nsamples,
81 uintptr_t start, uintptr_t end);
82 static void agg_destroy(struct aggent *agg) __unused;
83 static void asmparse(FILE *fp);
84 static int cparse(FILE *fp);
85 static void entry_acqref(struct entry *entry);
86 static struct entry *entry_create(const char *name, uintptr_t pc,
87 uintptr_t start, uintptr_t end);
88 static void entry_destroy(struct entry *entry) __unused;
89 static void fqueue_compact(float th);
90 static void fqueue_deleteall(void);
91 static struct aggent *fqueue_findent_by_name(const char *name);
92 static int fqueue_getall(const char *bin, char *temp, int asmf);
93 static int fqueue_insertent(struct entry *entry);
94 static int fqueue_insertgen(void);
95 static void general_deleteall(void);
96 static struct entry *general_findent(uintptr_t pc);
97 static void general_insertent(struct entry *entry);
98 static void general_printasm(FILE *fp, struct aggent *agg);
99 static int general_printc(FILE *fp, struct aggent *agg);
100 static int printblock(FILE *fp, struct aggent *agg);
101 static void usage(const char *progname) __dead2;
103 static TAILQ_HEAD(, entry) mainlst = TAILQ_HEAD_INITIALIZER(mainlst);
104 static TAILQ_HEAD(, aggent) fqueue = TAILQ_HEAD_INITIALIZER(fqueue);
107 * Use a float value in order to automatically promote operations
108 * to return a float value rather than use casts.
110 static float totalsamples;
113 * Identifies a string cointaining objdump's assembly printout.
116 isasminline(const char *str)
121 if (isxdigit(str[1]) == 0)
123 if (sscanf(str, " %p%n", &ptr, &nbytes) != 1)
125 if (str[nbytes] != ':' || isspace(str[nbytes + 1]) == 0)
131 * Identifies a string containing objdump's assembly printout
132 * for a new function.
135 newfunction(const char *str)
143 if (sscanf(str, "%p <%[^>:]>:%n", &ptr, fname, &nbytes) != 2)
149 * Create a new first-level aggregation object for a specified
152 static struct aggent *
153 agg_create(const char *name, u_int nsamples, uintptr_t start, uintptr_t end)
157 agg = calloc(1, sizeof(struct aggent));
160 agg->ag_name = strdup(name);
161 if (agg->ag_name == NULL) {
165 agg->ag_nsamples = nsamples;
166 agg->ag_ostart = start;
172 * Destroy a first-level aggregation object for a specified
176 agg_destroy(struct aggent *agg)
184 * Analyze the "objdump -d" output, locate functions and start
185 * printing out the assembly functions content.
186 * We do not use newfunction() because we actually need the
187 * function name in available form, but the heurstic used is
193 char buffer[LNBUFF], fname[FNBUFF];
197 while (fgets(buffer, LNBUFF, fp) != NULL) {
198 if (isspace(buffer[0]))
200 if (sscanf(buffer, "%p <%[^>:]>:", &ptr, fname) != 2)
202 agg = fqueue_findent_by_name(fname);
205 agg->ag_offset = ftell(fp);
208 TAILQ_FOREACH(agg, &fqueue, ag_fiter) {
209 if (fseek(fp, agg->ag_offset, SEEK_SET) == -1)
211 printf("Profile trace for function: %s() [%.2f%%]\n",
212 agg->ag_name, PERCSAMP(agg->ag_nsamples));
213 general_printasm(fp, agg);
219 * Analyze the "objdump -S" output, locate functions and start
220 * printing out the C functions content.
221 * We do not use newfunction() because we actually need the
222 * function name in available form, but the heurstic used is
224 * In order to maintain the printout sorted, on the first pass it
225 * simply stores the file offsets in order to fastly moved later
226 * (when the file is hot-cached also) when the real printout will
232 char buffer[LNBUFF], fname[FNBUFF];
236 while (fgets(buffer, LNBUFF, fp) != NULL) {
237 if (isspace(buffer[0]))
239 if (sscanf(buffer, "%p <%[^>:]>:", &ptr, fname) != 2)
241 agg = fqueue_findent_by_name(fname);
244 agg->ag_offset = ftell(fp);
247 TAILQ_FOREACH(agg, &fqueue, ag_fiter) {
248 if (fseek(fp, agg->ag_offset, SEEK_SET) == -1)
250 printf("Profile trace for function: %s() [%.2f%%]\n",
251 agg->ag_name, PERCSAMP(agg->ag_nsamples));
252 if (general_printc(fp, agg) == -1)
260 * Bump the number of samples for any raw entry.
263 entry_acqref(struct entry *entry)
266 entry->en_nsamples++;
270 * Create a new raw entry object for a specified function.
272 static struct entry *
273 entry_create(const char *name, uintptr_t pc, uintptr_t start, uintptr_t end)
277 obj = calloc(1, sizeof(struct entry));
280 obj->en_name = strdup(name);
281 if (obj->en_name == NULL) {
286 obj->en_ostart = start;
288 obj->en_nsamples = 1;
293 * Destroy a raw entry object for a specified function.
296 entry_destroy(struct entry *entry)
299 free(entry->en_name);
304 * Specify a lower bound in percentage and drop from the
305 * first-level aggregation queue all the objects with a
309 fqueue_compact(float th)
312 struct aggent *agg, *tmpagg;
314 if (totalsamples == 0)
317 /* Revert the percentage calculation. */
318 thi = th * totalsamples / 100;
319 TAILQ_FOREACH_SAFE(agg, &fqueue, ag_fiter, tmpagg)
320 if (agg->ag_nsamples < thi)
321 TAILQ_REMOVE(&fqueue, agg, ag_fiter);
325 * Flush the first-level aggregates queue.
328 fqueue_deleteall(void)
332 while (TAILQ_EMPTY(&fqueue) == 0) {
333 agg = TAILQ_FIRST(&fqueue);
334 TAILQ_REMOVE(&fqueue, agg, ag_fiter);
339 * Insert a raw entry into the aggregations queue.
340 * If the respective first-level aggregation object
341 * does not exist create it and maintain it sorted
342 * in respect of the number of samples.
345 fqueue_insertent(struct entry *entry)
347 struct aggent *obj, *tmp;
351 TAILQ_FOREACH(obj, &fqueue, ag_fiter)
352 if (!strcmp(obj->ag_name, entry->en_name)) {
354 obj->ag_nsamples += entry->en_nsamples;
359 * If the first-level aggregation object already exists,
360 * just aggregate the samples and, if needed, resort
364 TAILQ_REMOVE(&fqueue, obj, ag_fiter);
366 TAILQ_FOREACH(tmp, &fqueue, ag_fiter)
367 if (obj->ag_nsamples > tmp->ag_nsamples) {
372 TAILQ_INSERT_BEFORE(tmp, obj, ag_fiter);
374 TAILQ_INSERT_TAIL(&fqueue, obj, ag_fiter);
379 * If the first-level aggregation object does not
380 * exist, create it and put in the sorted queue.
381 * If this is the first object, we need to set the
384 obj = agg_create(entry->en_name, entry->en_nsamples, entry->en_ostart,
388 if (TAILQ_EMPTY(&fqueue) != 0) {
389 TAILQ_INSERT_HEAD(&fqueue, obj, ag_fiter);
392 TAILQ_FOREACH(tmp, &fqueue, ag_fiter)
393 if (obj->ag_nsamples > tmp->ag_nsamples) {
398 TAILQ_INSERT_BEFORE(tmp, obj, ag_fiter);
400 TAILQ_INSERT_TAIL(&fqueue, obj, ag_fiter);
405 * Lookup a first-level aggregation object by name.
407 static struct aggent *
408 fqueue_findent_by_name(const char *name)
412 TAILQ_FOREACH(obj, &fqueue, ag_fiter)
413 if (!strcmp(obj->ag_name, name))
419 * Return the number of object in the first-level aggregations queue.
422 fqueue_getall(const char *bin, char *temp, int asmf)
424 char tmpf[MAXPATHLEN * 2 + 50];
426 uintptr_t start, end;
428 if (mkstemp(temp) == -1)
430 TAILQ_FOREACH(agg, &fqueue, ag_fiter) {
431 bzero(tmpf, sizeof(tmpf));
432 start = agg->ag_ostart;
436 * Fix-up the end address in order to show it in the objdump's
441 snprintf(tmpf, sizeof(tmpf),
442 "objdump --start-address=%p "
443 "--stop-address=%p -d %s >> %s", (void *)start,
444 (void *)end, bin, temp);
446 snprintf(tmpf, sizeof(tmpf),
447 "objdump --start-address=%p "
448 "--stop-address=%p -S %s >> %s", (void *)start,
449 (void *)end, bin, temp);
450 if (system(tmpf) != 0)
457 * Insert all the raw entries present in the general queue
458 * into the first-level aggregations queue.
461 fqueue_insertgen(void)
465 TAILQ_FOREACH(obj, &mainlst, en_iter)
466 if (fqueue_insertent(obj) == -1)
472 * Flush the raw entries general queue.
475 general_deleteall(void)
479 while (TAILQ_EMPTY(&mainlst) == 0) {
480 obj = TAILQ_FIRST(&mainlst);
481 TAILQ_REMOVE(&mainlst, obj, en_iter);
486 * Lookup a raw entry by the PC.
488 static struct entry *
489 general_findent(uintptr_t pc)
493 TAILQ_FOREACH(obj, &mainlst, en_iter)
494 if (obj->en_pc == pc)
500 * Insert a new raw entry in the general queue.
503 general_insertent(struct entry *entry)
506 TAILQ_INSERT_TAIL(&mainlst, entry, en_iter);
510 * Printout the body of an "objdump -d" assembly function.
511 * It does simply stops when a new function is encountered,
512 * bringing back the file position in order to not mess up
513 * subsequent analysis.
514 * C lines and others not recognized are simply skipped.
517 general_printasm(FILE *fp, struct aggent *agg)
524 while (fgets(buffer, LNBUFF, fp) != NULL) {
525 if ((nbytes = newfunction(buffer)) != 0) {
526 fseek(fp, nbytes * -1, SEEK_CUR);
529 if (!isasminline(buffer))
531 if (sscanf(buffer, " %p:", &ptr) != 1)
533 obj = general_findent((uintptr_t)ptr);
535 printf("\t| %s", buffer);
537 printf("%.2f%%\t| %s",
538 (float)obj->en_nsamples * 100 / agg->ag_nsamples,
544 * Printout the body of an "objdump -S" function.
545 * It does simply stops when a new function is encountered,
546 * bringing back the file position in order to not mess up
547 * subsequent analysis.
548 * It expect from the starting to the end to find, always, valid blocks
549 * (see below for an explanation of the "block" concept).
552 general_printc(FILE *fp, struct aggent *agg)
556 while (fgets(buffer, LNBUFF, fp) != NULL) {
557 fseek(fp, strlen(buffer) * -1, SEEK_CUR);
558 if (newfunction(buffer) != 0)
560 if (printblock(fp, agg) == -1)
567 * Printout a single block inside an "objdump -S" function.
568 * The block is composed of a first part in C and subsequent translation
570 * This code also operates a second-level aggregation packing together
571 * samples relative to PCs into a (lower bottom) block with their
572 * C (higher half) counterpart.
575 printblock(FILE *fp, struct aggent *agg)
581 int done, nbytes, sentinel;
585 * We expect the first thing of the block is C code, so simply give
586 * up if asm line is found.
591 if (fgets(buffer, LNBUFF, fp) == NULL)
593 if (isasminline(buffer) != 0)
596 nbytes = newfunction(buffer);
598 if (fseek(fp, nbytes * -1, SEEK_CUR) == -1)
605 * If the sentinel is not set, it means it did not match any
606 * "high half" for this code so simply give up.
607 * Operates the second-level aggregation.
613 if (sscanf(buffer, " %p:", &ptr) != 1)
615 obj = general_findent((uintptr_t)ptr);
617 tnsamples += obj->en_nsamples;
618 } while (fgets(buffer, LNBUFF, fp) != NULL && isasminline(buffer) != 0);
620 /* Rewind to the start of the block in order to start the printout. */
621 if (fseek(fp, lstart, SEEK_SET) == -1)
624 /* Again the high half of the block rappresenting the C part. */
626 while (fgets(buffer, LNBUFF, fp) != NULL && isasminline(buffer) == 0) {
627 if (tnsamples == 0 || done != 0)
628 printf("\t| %s", buffer);
631 printf("%.2f%%\t| %s",
632 (float)tnsamples * 100 / agg->ag_nsamples, buffer);
637 * Again the low half of the block rappresenting the asm
641 if (fgets(buffer, LNBUFF, fp) == NULL)
643 if (isasminline(buffer) == 0)
645 nbytes = newfunction(buffer);
647 if (fseek(fp, nbytes * -1, SEEK_CUR) == -1)
652 if (fseek(fp, strlen(buffer) * -1, SEEK_CUR) == -1)
658 * Helper printout functions.
661 usage(const char *progname)
665 "usage: %s [-a] [-h] [-k kfile] [-l lb] pmcraw.out binary\n",
671 main(int argc, char *argv[])
673 char buffer[LNBUFF], fname[FNBUFF], tbfl[] = TMPPATH, tofl[] = TMPPATH;
674 char tmpf[MAXPATHLEN * 2 + 50];
676 char *bin, *exec, *kfile, *ofile;
679 void *ptr, *hstart, *hend;
680 uintptr_t tmppc, ostart, oend;
689 while ((cget = getopt(argc, argv, "ahl:k:")) != -1)
698 limit = (float)atof(optarg);
712 if (access(bin, R_OK | F_OK) == -1)
713 FATAL(exec, "%s: Impossible to locate the binary file\n",
715 if (access(ofile, R_OK | F_OK) == -1)
716 FATAL(exec, "%s: Impossible to locate the pmcstat file\n",
718 if (kfile != NULL && access(kfile, R_OK | F_OK) == -1)
719 FATAL(exec, "%s: Impossible to locate the kernel file\n",
722 bzero(tmpf, sizeof(tmpf));
723 if (mkstemp(tofl) == -1)
724 FATAL(exec, "%s: Impossible to create the tmp file\n",
727 snprintf(tmpf, sizeof(tmpf), "pmcstat -k %s -R %s -m %s",
730 snprintf(tmpf, sizeof(tmpf), "pmcstat -R %s -m %s", ofile,
732 if (system(tmpf) != 0)
733 FATAL(exec, "%s: Impossible to create the tmp file\n",
736 gfp = fopen(tofl, "r");
738 FATAL(exec, "%s: Impossible to open the map file\n",
742 * Make the collection of raw entries from a pmcstat mapped file.
743 * The heuristic here wants strings in the form:
744 * "addr funcname startfaddr endfaddr".
746 while (fgets(buffer, LNBUFF, gfp) != NULL) {
747 if (isspace(buffer[0]))
749 if (sscanf(buffer, "%p %s %p %p\n", &ptr, fname,
750 &hstart, &hend) != 4)
752 "%s: Invalid scan of function in the map file\n",
754 ostart = (uintptr_t)hstart;
755 oend = (uintptr_t)hend;
756 tmppc = (uintptr_t)ptr;
758 obj = general_findent(tmppc);
763 obj = entry_create(fname, tmppc, ostart, oend);
766 "%s: Impossible to create a new object\n", exec);
767 general_insertent(obj);
769 if (fclose(gfp) == EOF)
770 FATAL(exec, "%s: Impossible to close the filedesc\n",
772 if (remove(tofl) == -1)
773 FATAL(exec, "%s: Impossible to remove the tmpfile\n",
777 * Remove the loose end objects and feed the first-level aggregation
780 if (fqueue_insertgen() == -1)
781 FATAL(exec, "%s: Impossible to generate an analysis\n",
783 fqueue_compact(limit);
784 if (fqueue_getall(bin, tbfl, asmsrc) == -1)
785 FATAL(exec, "%s: Impossible to create the tmp file\n",
788 bfp = fopen(tbfl, "r");
790 FATAL(exec, "%s: Impossible to open the binary file\n",
795 else if (cparse(bfp) == -1)
796 FATAL(NULL, "%s: Invalid format for the C file\n", exec);
797 if (fclose(bfp) == EOF)
798 FATAL(exec, "%s: Impossible to close the filedesc\n",
800 if (remove(tbfl) == -1)
801 FATAL(exec, "%s: Impossible to remove the tmpfile\n",