1b8e80941Smrg#!/bin/bash
2b8e80941Smrg# -*- mode: sh -*-
3b8e80941Smrg
4b8e80941Smrgfunction show_help() {
5b8e80941Smrg    cat <<EOF
6b8e80941SmrgUsage: intel_dump_gpu [OPTION]... [--] COMMAND ARGUMENTS
7b8e80941Smrg
8b8e80941SmrgRun COMMAND with ARGUMENTS and dump an AUB file that captures buffer
9b8e80941Smrgcontents and execution of the GEM application.
10b8e80941Smrg
11b8e80941Smrg  -g, --gdb           Launch GDB
12b8e80941Smrg
13b8e80941Smrg  -o, --output=FILE   Name of AUB file. Defaults to COMMAND.aub
14b8e80941Smrg
15b8e80941Smrg      --device=ID     Override PCI ID of the reported device
16b8e80941Smrg
17b8e80941Smrg  -p, --platform=NAME Override PCI ID using a platform name
18b8e80941Smrg
19b8e80941Smrg  -v                  Enable verbose output
20b8e80941Smrg
21b8e80941Smrg  -vv                 Enable extra verbosity - dumps gtt mappings
22b8e80941Smrg
23b8e80941Smrg      --help          Display this help message and exit
24b8e80941Smrg
25b8e80941SmrgEOF
26b8e80941Smrg
27b8e80941Smrg    exit 0
28b8e80941Smrg}
29b8e80941Smrg
30b8e80941Smrgld_preload="@install_libexecdir@/libintel_dump_gpu.so${LD_PPRELOAD:+:$LD_PRELOAD}"
31b8e80941Smrgargs=""
32b8e80941Smrgfile=""
33b8e80941Smrggdb=""
34b8e80941Smrg
35b8e80941Smrgfunction add_arg() {
36b8e80941Smrg    arg=$1
37b8e80941Smrg    args="$args$arg\n"
38b8e80941Smrg}
39b8e80941Smrg
40b8e80941Smrgwhile true; do
41b8e80941Smrg    case "$1" in
42b8e80941Smrg        -v)
43b8e80941Smrg            add_arg "verbose=1"
44b8e80941Smrg            shift 1
45b8e80941Smrg            ;;
46b8e80941Smrg        -vv)
47b8e80941Smrg            add_arg "verbose=2"
48b8e80941Smrg            shift 1
49b8e80941Smrg            ;;
50b8e80941Smrg        -o)
51b8e80941Smrg            file=$2
52b8e80941Smrg            add_arg "file=${file:-$(basename ${file}).aub}"
53b8e80941Smrg            shift 2
54b8e80941Smrg            ;;
55b8e80941Smrg        -o*)
56b8e80941Smrg            file=${1##-o}
57b8e80941Smrg            add_arg "file=${file:-$(basename ${file}).aub}"
58b8e80941Smrg            shift
59b8e80941Smrg            ;;
60b8e80941Smrg        --output=*)
61b8e80941Smrg            file=${1##--output=}
62b8e80941Smrg            add_arg "file=${file:-$(basename ${file}).aub}"
63b8e80941Smrg            shift
64b8e80941Smrg            ;;
65b8e80941Smrg        --device=*)
66b8e80941Smrg            add_arg "device=${1##--device=}"
67b8e80941Smrg            shift
68b8e80941Smrg            ;;
69b8e80941Smrg        -p)
70b8e80941Smrg            platform=$2
71b8e80941Smrg            add_arg "platform=${platform}"
72b8e80941Smrg            shift 2
73b8e80941Smrg            ;;
74b8e80941Smrg        -p*)
75b8e80941Smrg            platform=${1##-p}
76b8e80941Smrg            add_arg "platform=${platform}"
77b8e80941Smrg            shift
78b8e80941Smrg            ;;
79b8e80941Smrg        --platform=*)
80b8e80941Smrg            platform=${1##-p}
81b8e80941Smrg            add_arg "platform=${platform}"
82b8e80941Smrg            shift
83b8e80941Smrg            ;;
84b8e80941Smrg        --gdb)
85b8e80941Smrg            gdb=1
86b8e80941Smrg            shift
87b8e80941Smrg            ;;
88b8e80941Smrg        -g)
89b8e80941Smrg            gdb=1
90b8e80941Smrg            shift
91b8e80941Smrg            ;;
92b8e80941Smrg        --help)
93b8e80941Smrg            show_help
94b8e80941Smrg            ;;
95b8e80941Smrg        --)
96b8e80941Smrg            shift
97b8e80941Smrg            break
98b8e80941Smrg            ;;
99b8e80941Smrg        -*)
100b8e80941Smrg            echo "intel_aubdump: invalid option: $1"
101b8e80941Smrg            echo
102b8e80941Smrg            show_help
103b8e80941Smrg            ;;
104b8e80941Smrg        *)
105b8e80941Smrg            break
106b8e80941Smrg            ;;
107b8e80941Smrg    esac
108b8e80941Smrgdone
109b8e80941Smrg
110b8e80941Smrg[ -z $1 ] && show_help
111b8e80941Smrg
112b8e80941Smrg[ -z $file ] && add_arg "file=intel.aub"
113b8e80941Smrg
114b8e80941Smrgtmp_file=`mktemp`
115b8e80941Smrgecho -e $args > $tmp_file
116b8e80941Smrg
117b8e80941Smrgif [ -z $gdb ]; then
118b8e80941Smrg    LD_PRELOAD="$ld_preload" INTEL_DUMP_GPU_CONFIG=$tmp_file $@
119b8e80941Smrgelse
120b8e80941Smrg    gdb -iex "set exec-wrapper env LD_PRELOAD=$ld_preload INTEL_DUMP_GPU_CONFIG=$tmp_file" --args $@
121b8e80941Smrgfi
122b8e80941Smrg
123b8e80941Smrgret=$?
124b8e80941Smrgrm $tmp_file
125b8e80941Smrgexit $ret
126