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: cmake run directory.
15if [ -z "$WIRESHARK_BIN_DIR" ]; then
16    WIRESHARK_BIN_DIR=run
17fi
18
19# Use tshark by default
20COMMAND=tshark
21COMMAND_ARGS="-nr"
22COMMAND_ARGS2=
23VALID=0
24PCAP=""
25TOOL="memcheck"
26
27while 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        d) COMMAND=dumpcap
58           COMMAND_ARGS="-i eth1 -c 3000"
59           VALID=1 ;;
60        *) printf "Unknown option: %s\\n" "$OPTARG"
61           exit ;;
62    esac
63done
64shift $(( OPTIND - 1 ))
65
66# Sanitize parameters
67if [ "$COMMAND" != "tshark" ] && [[ $COMMAND_ARGS =~ Vx ]]
68then
69    printf "\\nYou can't use -T if you're not using tshark\\n\\n" >&2
70    exit 1
71fi
72
73if [ $# -ge 1 ]
74then
75    PCAP=$1
76    VALID=1
77fi
78
79if [ $VALID -eq 0 ]
80then
81    printf "\\nUsage: %s [-2] [-a file] [-b bin_dir] [-c] [-e] [-C config_profile] " "$(basename "$0")"
82    printf "[-l] [-m] [-n] [-p] [-r] [-s] [-t] [-T] [-w] [-v] /path/to/file.pcap\\n"
83    printf "\\n"
84    printf "[-2]: run tshark with 2-pass analysis\\n"
85    printf "[-a]: additional valgrind suppression file\\n"
86    printf "[-b]: tshark binary dir\\n"
87    printf "[-e]: use 'editcap -E 0.02' instead of tshark\\n"
88    printf "[-c]: use capinfos instead of tshark\\n"
89    printf "[-C]: binary profile file\\n"
90    printf "[-l]: add valgrind option --leak-check=full\\n"
91    printf "[-m]: use valgrind massif tool\\n"
92    printf "[-n]: print binary version\\n"
93    printf "[-p]: use callgrind massif tool\\n"
94    printf "[-r]: add valgrind option --show-reachable=yes\\n"
95    printf "[-s]: add valgrind option --gen-suppressions=yes\\n"
96    printf "[-t]: add valgrind option --track-origins=yes\\n"
97    printf "[-T]: build the tshark tree (-Vx)\\n"
98    printf "[-w]: use wireshark instead of tshark\\n"
99    printf "[-v]: run in verbose mode (--num-callers=256)\\n"
100    exit 1
101fi
102
103if [ "$WIRESHARK_BIN_DIR" = "." ]; then
104    export WIRESHARK_RUN_FROM_BUILD_DIRECTORY=
105fi
106
107if [ "$TOOL" != "callgrind" ]; then
108    export WIRESHARK_DEBUG_WMEM_OVERRIDE=simple
109    export G_SLICE=always-malloc # or debug-blocks
110fi
111
112COMMAND="$WIRESHARK_BIN_DIR/$COMMAND"
113
114cmdline="valgrind --suppressions=$( dirname "$0" )/vg-suppressions $ADDITIONAL_SUPPRESSION_FILE \
115--tool=$TOOL $CALLGRIND_OUT_FILE $VERBOSE $LEAK_CHECK $REACHABLE $GEN_SUPPRESSIONS $TRACK_ORIGINS \
116$COMMAND $COMMAND_ARGS $PCAP $COMMAND_ARGS2"
117
118if [ "$VERBOSE" != "" ];then
119  echo -e "\\n$cmdline\\n"
120fi
121
122# shellcheck disable=SC2086
123exec $cmdline > /dev/null
124