1a28e17c0e7a882d73e4ca91bd984cef4bf3f1f5
[metze/wireshark/wip.git] / tools / valgrind-wireshark.sh
1 #!/bin/bash
2
3 # A small script to export some variables and run tshark or wireshark in
4 # valgrind on a given capture file.
5 #
6 # Copyright 2012 Jeff Morriss <jeff.morriss.ws [AT] gmail.com>
7 #
8 # Wireshark - Network traffic analyzer
9 # By Gerald Combs <gerald@wireshark.org>
10 # Copyright 1998 Gerald Combs
11 #
12 # SPDX-License-Identifier: GPL-2.0-or-later
13
14 # Directory containing tshark or wireshark.  Default current directory.
15 if [ -z "$WIRESHARK_BIN_DIR" ]; then
16     WIRESHARK_BIN_DIR=.
17 fi
18
19 # Use tshark by default
20 COMMAND=tshark
21 COMMAND_ARGS="-nr"
22 COMMAND_ARGS2=
23 VALID=0
24 PCAP=""
25 TOOL="memcheck"
26
27 while getopts ":2a:b:C:lmnpP:rstTYwcevWdG" OPTCHAR ; do
28     case $OPTCHAR in
29         2) COMMAND_ARGS="-2 $COMMAND_ARGS" ;;
30         a) ADDITIONAL_SUPPRESSION_FILE="$ADDITIONAL_SUPPRESSION_FILE --suppressions=$OPTARG" ;;
31         b) WIRESHARK_BIN_DIR=$OPTARG ;;
32         C) COMMAND_ARGS="-C $OPTARG $COMMAND_ARGS" ;;
33         l) LEAK_CHECK="--leak-check=full" ;;
34         m) TOOL="massif" ;;
35         n) COMMAND_ARGS="-v"
36            VALID=1 ;;
37         p) TOOL="callgrind" ;;
38         P) TOOL="callgrind"
39            CALLGRIND_OUT_FILE="--callgrind-out-file=$OPTARG" ;;
40         r) REACHABLE="--show-reachable=yes" ;;
41         s) GEN_SUPPRESSIONS="--gen-suppressions=yes" ;;
42         t) TRACK_ORIGINS="--track-origins=yes" ;;
43         T) COMMAND_ARGS="-Vx $COMMAND_ARGS" ;; # "build the Tree"
44         Y) COMMAND_ARGS="-Y frame $COMMAND_ARGS" ;; # Run with a read filter (but no tree)
45         w) COMMAND=wireshark
46            COMMAND_ARGS="-nr" ;;
47         c) COMMAND=capinfos
48            COMMAND_ARGS="" ;;
49         e) COMMAND=editcap
50            COMMAND_ARGS="-E 0.02"
51            # We don't care about the output of editcap
52            COMMAND_ARGS2="/dev/null" ;;
53         v) VERBOSE="--num-callers=256 -v" ;;
54         W) COMMAND=wireshark
55            COMMAND_ARGS=""
56            VALID=1 ;;
57         G) COMMAND=wireshark-gtk
58            COMMAND_ARGS=""
59            VALID=1 ;;
60         d) COMMAND=dumpcap
61            COMMAND_ARGS="-i eth1 -c 3000"
62            VALID=1 ;;
63         *) printf "Unknown option -$OPTARG!\n"
64            exit ;;
65     esac
66 done
67 shift $(($OPTIND - 1))
68
69 # Sanitize parameters
70 if [ "$COMMAND" != "tshark" ] && [[ $COMMAND_ARGS =~ Vx ]]
71 then
72     printf "\nYou can't use -T if you're not using tshark\n\n" >&2
73     exit 1
74 fi
75
76 if [ $# -ge 1 ]
77 then
78     PCAP=$1
79     VALID=1
80 fi
81
82 if [ $VALID -eq 0 ]
83 then
84     printf "\nUsage: $(basename $0) [-2] [-a file] [-b bin_dir] [-c] [-e] [-C config_profile] "
85     printf "[-l] [-m] [-n] [-p] [-r] [-s] [-t] [-T] [-w] [-v] /path/to/file.pcap\n"
86     printf "\n"
87     printf "[-2]: run tshark with 2-pass analysis\n"
88     printf "[-a]: additional valgrind suppression file\n"
89     printf "[-b]: tshark binary dir\n"
90     printf "[-e]: use 'editcap -E 0.02' instead of tshark\n"
91     printf "[-c]: use capinfos instead of tshark\n"
92     printf "[-C]: binary profile file\n"
93     printf "[-l]: add valgrind option --leak-check=full\n"
94     printf "[-m]: use valgrind massif tool\n"
95     printf "[-n]: print binary version\n"
96     printf "[-p]: use callgrind massif tool\n"
97     printf "[-r]: add valgrind option --show-reachable=yes\n"
98     printf "[-s]: add valgrind option --gen-suppressions=yes\n"
99     printf "[-t]: add valgrind option --track-origins=yes\n"
100     printf "[-T]: build the tshark tree (-Vx)\n"
101     printf "[-w]: use wireshark instead of tshark\n"
102     printf "[-v]: run in verbose mode (--num-callers=256)\n"
103     exit 1
104 fi
105
106 if [ "$WIRESHARK_BIN_DIR" = "." ]; then
107     export WIRESHARK_RUN_FROM_BUILD_DIRECTORY=
108 fi
109
110 if [ "$TOOL" != "callgrind" ]; then
111     export WIRESHARK_DEBUG_WMEM_OVERRIDE=simple
112     export G_SLICE=always-malloc # or debug-blocks
113 fi
114
115 COMMAND="$WIRESHARK_BIN_DIR/$COMMAND"
116
117 if file $COMMAND | grep -q "ASCII text"; then
118     if [ -x "`dirname $0`/../libtool" ]; then
119         LIBTOOL="`dirname $0`/../libtool"
120     else
121         LIBTOOL="libtool"
122     fi
123     LIBTOOL="$LIBTOOL --mode=execute"
124 else
125     LIBTOOL=""
126 fi
127
128 cmdline="$LIBTOOL valgrind --suppressions=`dirname $0`/vg-suppressions $ADDITIONAL_SUPPRESSION_FILE \
129 --suppressions=`dirname $0`/gtk.suppression \
130 --tool=$TOOL $CALLGRIND_OUT_FILE $VERBOSE $LEAK_CHECK $REACHABLE $GEN_SUPPRESSIONS $TRACK_ORIGINS \
131 $COMMAND $COMMAND_ARGS $PCAP $COMMAND_ARGS2"
132
133 if [ "$VERBOSE" != "" ];then
134   echo -e "\n$cmdline\n"
135 fi
136
137 $cmdline > /dev/null