-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_renderer
executable file
·141 lines (122 loc) · 3.88 KB
/
debug_renderer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Runs Chrome / content_shell and attaches to the renderer process for the
# target URL (or the first navigation if none is specified).
usage() {
echo "usage: $(basename $0) [-d <debug-url>] <path-to-content-shell> <url> [more-args...]"
exit 1
}
DEFAULT_TARGET_FLAGS=(--no-sandbox --disable-hang-monitor --wait-for-debugger-on-navigation)
while getopts "h?d:" OPTION
do
case $OPTION in
h|\?)
usage
;;
d)
DEBUG_URL=$OPTARG
;;
esac
done
shift $[$OPTIND-1]
TARGET=$1
shift
if [ -z "$TARGET" ]; then
usage
fi
if [[ "$TARGET" =~ run_web_tests.py$ ]]; then
# Adjust flags to be passed to driver
WEB_TESTS_FLAGS=(--jobs=1 --timeout-ms=100000000 --driver-logging)
for DEFAULT_FLAG in "${DEFAULT_TARGET_FLAGS[@]}"; do
WEB_TESTS_FLAGS=(${WEB_TESTS_FLAGS[@]} --additional-driver-flag="$DEFAULT_FLAG")
done
TARGET_FLAGS=(${WEB_TESTS_FLAGS[@]} "$@")
elif [[ "$TARGET" =~ run_wpt_tests.py$ ]]; then
WPT_TESTS_FLAGS=(--jobs=1 --timeout-multiplier=10000 --verbose)
for DEFAULT_FLAG in "${DEFAULT_TARGET_FLAGS[@]}"; do
WPT_TESTS_FLAGS=(${WPT_TESTS_FLAGS[@]} --additional-driver-flag="$DEFAULT_FLAG")
done
TARGET_FLAGS=(${WPT_TESTS_FLAGS[@]} "$@")
else
TARGET_FLAGS=(${DEFAULT_TARGET_FLAGS[@]} "$@")
fi
if [ -z "$DEBUG_URL" ]; then
echo "Debugging first renderer"
fi
# TODO: If you pass a URL containing characters that require URL encoding,
# the URL will be encoded by chrome and won't be equivalent to the $DEBUG_URL.
# We should url encode DEBUG_URL so that it matches the url chrome navigates to.
RENDERER_PID_RE='Renderer url="([^"]+)" \(([0-9]+)\) paused waiting for debugger'
if [ -z "$DEBUGGER" ]; then
if which lldb > /dev/null; then
DEBUGGER="lldb"
CONTINUE="continue"
elif which gdb > /dev/null; then
DEBUGGER="gdb -q"
CONTINUE="signal SIGUSR1"
else
echo "no debugger found"
exit 1
fi
fi
OUTPUT=$(mktemp "${TMPDIR:-/tmp}"/"$(basename $0)".XXXXX)
UNBUFFER=""
# Use unbuffer to ensure we get the output while the subprocess is paused
# waiting for a renderer.
if [ -z `which unbuffer` ]; then
echo "Warning: Couldn't find unbuffer. Buffered output can result in" \
"failure to read the PID of the paused renderer from the pipe. If" \
"you encounter a hang install unbuffer tool from your package manager."
else
UNBUFFER="unbuffer "
fi
maybe_kill() {
[ -n "$1" ] && ps -p $1 > /dev/null && kill $1
}
cleanup() {
rm $OUTPUT
maybe_kill "$BROWSER_PID"
maybe_kill "$SIGNAL_PID"
}
trap cleanup EXIT
echo "Running ${UNBUFFER}${TARGET} ${TARGET_FLAGS[@]}" >&2
${UNBUFFER}"${TARGET}" "${TARGET_FLAGS[@]}" > >(tee $OUTPUT) 2>&1 &
BROWSER_PID=$!
echo "Process $BROWSER_PID logging to $OUTPUT"
wait_renderer_pid() {
NEXT_LINE=1
tail +1f $OUTPUT | while read LINE; do
NEXT_LINE=$[$NEXT_LINE + 1]
if [[ "$LINE" =~ $RENDERER_PID_RE ]]; then
RENDERER_URL=${BASH_REMATCH[1]}
RENDERER_PID=${BASH_REMATCH[2]}
if [ -z "$DEBUG_URL" ] || [[ "$RENDERER_URL" == "$DEBUG_URL" ]]; then
echo "$NEXT_LINE $RENDERER_PID"
return
fi
# Unblock unrelated renderers.
kill -s SIGUSR1 "$RENDERER_PID"
fi
done
}
signal_renderers() {
STARTING_OUTPUT_LINE=$1
tail +${STARTING_OUTPUT_LINE}f $OUTPUT | while read LINE; do
if [[ "$LINE" =~ $RENDERER_PID_RE ]]; then
kill -s SIGUSR1 ${BASH_REMATCH[2]}
fi
done
}
RESULT=$(wait_renderer_pid)
NEXT_LINE=$(echo $RESULT|cut -d' ' -f 1)
RENDERER_PID=$(echo $RESULT|cut -d' ' -f 2)
if [ -n "$RENDERER_PID" ]; then
echo "Target renderer found. Sending SIGUSR1 to unblock any subsequent renderers."
signal_renderers ${NEXT_LINE} &
SIGNAL_PID=$!
# print yellow message
echo -e "\n\033[1;33mDebugging renderer, use '$CONTINUE' to run.\033[0m\n"
$DEBUGGER -p $RENDERER_PID
fi