Willy Tarreau | 7de211c | 2012-05-25 23:53:16 +0200 | [diff] [blame] | 1 | #!/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 | |
| 17 | if [ $# -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 |
| 22 | fi |
| 23 | |
| 24 | if [ ! -s "$1" ]; then |
| 25 | echo "$1 is not a valid executable file" |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
| 29 | exec awk -v prog="$1" \ |
| 30 | ' |
| 31 | BEGIN { |
| 32 | if (cmd == "") |
| 33 | cmd=ENVIRON["ADDR2LINE"]; |
| 34 | if (cmd == "") |
| 35 | cmd="addr2line"; |
| 36 | |
| 37 | if (prog == "") |
| 38 | prog=ENVIRON["PROG"]; |
| 39 | |
Willy Tarreau | f5e0d1f | 2017-10-24 11:55:37 +0200 | [diff] [blame] | 40 | cmd=cmd " -f -s -e " prog; |
Willy Tarreau | 7de211c | 2012-05-25 23:53:16 +0200 | [diff] [blame] | 41 | |
| 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 Tarreau | 1296382 | 2017-10-24 10:54:08 +0200 | [diff] [blame] | 47 | indents[" ",i] = indents[" ",i-1] "##" |
| 48 | indents[" ",i-1] = indents[" ",i-1] " " |
Willy Tarreau | 7de211c | 2012-05-25 23:53:16 +0200 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | function 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 Tarreau | 1296382 | 2017-10-24 10:54:08 +0200 | [diff] [blame] | 66 | # 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 Tarreau | 7de211c | 2012-05-25 23:53:16 +0200 | [diff] [blame] | 73 | getptr($3); caller_loc=loc; caller_name=name |
| 74 | getptr($5); callee_loc=loc; callee_name=name |
Willy Tarreau | e8f0f12 | 2017-10-24 10:58:20 +0200 | [diff] [blame] | 75 | 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 Tarreau | 7de211c | 2012-05-25 23:53:16 +0200 | [diff] [blame] | 77 | } |
| 78 | ' |