blob: 7b3b131a3652bde840a33d693e0060fc5cd2f9b9 [file] [log] [blame]
Willy Tarreau7de211c2012-05-25 23:53:16 +02001#!/bin/sh
2#
3# trace.awk - Fast trace symbol resolver - w@1wt.eu - 2012/05/25
4#
5# Principle: this program launches reads pointers from a trace file and if not
6# found in its cache, it passes them over a pipe to addr2line which is forked
7# in a coprocess, then stores the result in the cache.
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License
11# as published by the Free Software Foundation; either version
12# 2 of the License, or (at your option) any later version.
13#
14# usage: $0 exec_file < trace.out
15#
16
17if [ $# -lt 1 ]; then
18 echo "Usage: ${0##*/} exec_file < trace.out"
19 echo "Example: ${0##*/} ./haproxy < trace.out"
20 echo "Example: HAPROXY_TRACE=/dev/stdout ./haproxy -f cfg | ${0##*/} ./haproxy"
21 exit 1
22fi
23
24if [ ! -s "$1" ]; then
25 echo "$1 is not a valid executable file"
26 exit 1
27fi
28
29exec awk -v prog="$1" \
30'
31BEGIN {
32 if (cmd == "")
33 cmd=ENVIRON["ADDR2LINE"];
34 if (cmd == "")
35 cmd="addr2line";
36
37 if (prog == "")
38 prog=ENVIRON["PROG"];
39
Willy Tarreauf5e0d1f2017-10-24 11:55:37 +020040 cmd=cmd " -f -s -e " prog;
Willy Tarreau7de211c2012-05-25 23:53:16 +020041
42 for (i = 1; i < 100; i++) {
43 indents[">",i] = indents[">",i-1] "->"
44 indents[">",i-1] = indents[">",i-1] " "
45 indents["<",i] = indents["<",i-1] " "
46 indents["<",i-1] = indents["<",i-1] " "
Willy Tarreau12963822017-10-24 10:54:08 +020047 indents[" ",i] = indents[" ",i-1] "##"
48 indents[" ",i-1] = indents[" ",i-1] " "
Willy Tarreau7de211c2012-05-25 23:53:16 +020049 }
50}
51
52function getptr(ptr)
53{
54 loc=locs[ptr];
55 name=names[ptr];
56 if (loc == "" || name == "") {
57 print ptr |& cmd;
58 cmd |& getline name;
59 cmd |& getline loc;
60 names[ptr]=name
61 locs[ptr]=loc
62 }
63}
64
65{
Willy Tarreau12963822017-10-24 10:54:08 +020066 # input format: <timestamp> <level> <caller> <dir> <callee> [<ret>|<args>...]
67 if ($3 == "#") { # this is a trace comment
68 printf "%s %s ", $1, indents[" ",$2]
69 $1=""; $2=""; $3=""
70 print substr($0,4)
71 next
72 }
Willy Tarreau7de211c2012-05-25 23:53:16 +020073 getptr($3); caller_loc=loc; caller_name=name
74 getptr($5); callee_loc=loc; callee_name=name
Willy Tarreaue8f0f122017-10-24 10:58:20 +020075 printf "%s %s %s %s %s(%s) [%s:%s] %s [%s:%s]\n",
76 $1, indents[$4,$2], caller_name, $4, callee_name, $6, caller_loc, $3, $4, callee_loc, $5
Willy Tarreau7de211c2012-05-25 23:53:16 +020077}
78'