3 # Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 # PERFORMANCE OF THIS SOFTWARE.
17 # Id: mksymtbl.pl,v 1.4 2009/10/05 22:39:09 jinmei Exp
23 my $rev = 'Id: mksymtbl.pl,v 1.4 2009/10/05 22:39:09 jinmei Exp';
30 getopts
('i:o:', \
%options);
32 my ($binname, $need_uscorefix, $outputfile, $nsyms, $ostype, $nm_prog);
38 $outputfile = $options{'o'};
40 $outputfile = "symtbl.c";
43 # OS-depending configuration
47 if ($ostype eq "SunOS" || $ostype eq "HP-UX") {
48 $nm_prog = "/usr/ccs/bin/nm -x"
52 open(SYMBOLS
, $options{'i'}) || die "failed to open $options{'i'}";
54 open(SYMBOLS
, "$nm_prog $binname |") ||
55 die "failed to invoke utility to get symbols";
57 open(TBLFILE
, ">$outputfile") || die "failed to open output file: $outputfile";
61 my ($addr, $symbol) = (0, "");
62 if ($ostype eq "SunOS") {
63 if (/\[\d*\]\s*\|\s*0x([0-9a-f]*)\|\s*0x[0-9a-f]*\|FUNC\s*(.*)\|([^|]+)$/) {
64 next if ($2 =~ /UNDEF/); # skip undefined symbols
69 } elsif ($ostype eq "HP-UX") {
70 if (/(\S*)\s*\|0x([0-9a-f]*)\|([^|]*\|entry|extern\|code)/) {
73 # this filter catches a massive number of awkward
74 # symbols such as "$START$". we are not interested in
75 # those and ignore them.
76 next if ($symbol =~ /\$/);
80 if (/([0-9a-f]*)\s[tT]\s(.*)/) {
81 ($addr, $symbol) = ($1, $2);
82 # heuristics: some compilers add a "_" to all program
83 # defined symbols. Detect and fix it for a well known
85 $need_uscorefix = 1 if ($symbol eq "_main");
89 # XXX: HP-UX's nm can produce a duplicate entry for the same
90 # address. Ignore duplicate entries except the first one.
91 next if ($symmap{$addr});
93 $symmap{$addr} = $symbol;
98 print TBLFILE
"/*\n * Generated by $rev \n */\n";
99 print TBLFILE
"#include <isc/backtrace.h>\n";
100 print TBLFILE
"const int isc__backtrace_nsymbols = $nsyms;\n";
101 print TBLFILE
"const isc_backtrace_symmap_t isc__backtrace_symtable[] = {\n";
102 foreach (sort {hex($a) <=> hex($b)} keys(%symmap)) {
103 my ($addr, $symbol) = ($_, $symmap{$_});
104 if ($need_uscorefix && $symbol =~ /^_(.*)/) {
107 print TBLFILE
"\t{ (void *)0x$addr, \"$symbol\" },\n";
109 print TBLFILE
"\t{ (void *)0x0, \"\" },\n";
110 print TBLFILE
"};\n";