xref: /openbsd-src/gnu/usr.bin/binutils-2.17/gprof/utils.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /*
2*3d8817e4Smiod  * Copyright (c) 1983, 1993, 2001
3*3d8817e4Smiod  *      The Regents of the University of California.  All rights reserved.
4*3d8817e4Smiod  *
5*3d8817e4Smiod  * Redistribution and use in source and binary forms, with or without
6*3d8817e4Smiod  * modification, are permitted provided that the following conditions
7*3d8817e4Smiod  * are met:
8*3d8817e4Smiod  * 1. Redistributions of source code must retain the above copyright
9*3d8817e4Smiod  *    notice, this list of conditions and the following disclaimer.
10*3d8817e4Smiod  * 2. Redistributions in binary form must reproduce the above copyright
11*3d8817e4Smiod  *    notice, this list of conditions and the following disclaimer in the
12*3d8817e4Smiod  *    documentation and/or other materials provided with the distribution.
13*3d8817e4Smiod  * 3. Neither the name of the University nor the names of its contributors
14*3d8817e4Smiod  *    may be used to endorse or promote products derived from this software
15*3d8817e4Smiod  *    without specific prior written permission.
16*3d8817e4Smiod  *
17*3d8817e4Smiod  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*3d8817e4Smiod  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*3d8817e4Smiod  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*3d8817e4Smiod  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*3d8817e4Smiod  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*3d8817e4Smiod  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*3d8817e4Smiod  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*3d8817e4Smiod  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*3d8817e4Smiod  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*3d8817e4Smiod  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*3d8817e4Smiod  * SUCH DAMAGE.
28*3d8817e4Smiod  */
29*3d8817e4Smiod #include "demangle.h"
30*3d8817e4Smiod #include "gprof.h"
31*3d8817e4Smiod #include "search_list.h"
32*3d8817e4Smiod #include "source.h"
33*3d8817e4Smiod #include "symtab.h"
34*3d8817e4Smiod #include "cg_arcs.h"
35*3d8817e4Smiod #include "utils.h"
36*3d8817e4Smiod 
37*3d8817e4Smiod 
38*3d8817e4Smiod /*
39*3d8817e4Smiod  * Print name of symbol.  Return number of characters printed.
40*3d8817e4Smiod  */
41*3d8817e4Smiod int
print_name_only(Sym * self)42*3d8817e4Smiod print_name_only (Sym *self)
43*3d8817e4Smiod {
44*3d8817e4Smiod   const char *name = self->name;
45*3d8817e4Smiod   const char *filename;
46*3d8817e4Smiod   char *demangled = 0;
47*3d8817e4Smiod   char buf[PATH_MAX];
48*3d8817e4Smiod   int size = 0;
49*3d8817e4Smiod 
50*3d8817e4Smiod   if (name)
51*3d8817e4Smiod     {
52*3d8817e4Smiod       if (!bsd_style_output)
53*3d8817e4Smiod 	{
54*3d8817e4Smiod 	  if (name[0] == '_' && name[1] && discard_underscores)
55*3d8817e4Smiod 	    {
56*3d8817e4Smiod 	      name++;
57*3d8817e4Smiod 	    }
58*3d8817e4Smiod 	  if (demangle)
59*3d8817e4Smiod 	    {
60*3d8817e4Smiod 	      demangled = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
61*3d8817e4Smiod 	      if (demangled)
62*3d8817e4Smiod 		{
63*3d8817e4Smiod 		  name = demangled;
64*3d8817e4Smiod 		}
65*3d8817e4Smiod 	    }
66*3d8817e4Smiod 	}
67*3d8817e4Smiod       printf ("%s", name);
68*3d8817e4Smiod       size = strlen (name);
69*3d8817e4Smiod       if (line_granularity && self->file)
70*3d8817e4Smiod 	{
71*3d8817e4Smiod 	  filename = self->file->name;
72*3d8817e4Smiod 	  if (!print_path)
73*3d8817e4Smiod 	    {
74*3d8817e4Smiod 	      filename = strrchr (filename, '/');
75*3d8817e4Smiod 	      if (filename)
76*3d8817e4Smiod 		{
77*3d8817e4Smiod 		  ++filename;
78*3d8817e4Smiod 		}
79*3d8817e4Smiod 	      else
80*3d8817e4Smiod 		{
81*3d8817e4Smiod 		  filename = self->file->name;
82*3d8817e4Smiod 		}
83*3d8817e4Smiod 	    }
84*3d8817e4Smiod 	  sprintf (buf, " (%s:%d @ %lx)", filename, self->line_num,
85*3d8817e4Smiod 		   (unsigned long) self->addr);
86*3d8817e4Smiod 	  printf ("%s", buf);
87*3d8817e4Smiod 	  size += strlen (buf);
88*3d8817e4Smiod 	}
89*3d8817e4Smiod       if (demangled)
90*3d8817e4Smiod 	{
91*3d8817e4Smiod 	  free (demangled);
92*3d8817e4Smiod 	}
93*3d8817e4Smiod       DBG (DFNDEBUG, printf ("{%d} ", self->cg.top_order));
94*3d8817e4Smiod       DBG (PROPDEBUG, printf ("%4.0f%% ", 100.0 * self->cg.prop.fract));
95*3d8817e4Smiod     }
96*3d8817e4Smiod   return size;
97*3d8817e4Smiod }
98*3d8817e4Smiod 
99*3d8817e4Smiod 
100*3d8817e4Smiod void
print_name(Sym * self)101*3d8817e4Smiod print_name (Sym *self)
102*3d8817e4Smiod {
103*3d8817e4Smiod   print_name_only (self);
104*3d8817e4Smiod 
105*3d8817e4Smiod   if (self->cg.cyc.num != 0)
106*3d8817e4Smiod     {
107*3d8817e4Smiod       printf (_(" <cycle %d>"), self->cg.cyc.num);
108*3d8817e4Smiod     }
109*3d8817e4Smiod   if (self->cg.index != 0)
110*3d8817e4Smiod     {
111*3d8817e4Smiod       if (self->cg.print_flag)
112*3d8817e4Smiod 	{
113*3d8817e4Smiod 	  printf (" [%d]", self->cg.index);
114*3d8817e4Smiod 	}
115*3d8817e4Smiod       else
116*3d8817e4Smiod 	{
117*3d8817e4Smiod 	  printf (" (%d)", self->cg.index);
118*3d8817e4Smiod 	}
119*3d8817e4Smiod     }
120*3d8817e4Smiod }
121