-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add starlark debugger python script interface
This patch will add a python script interface to the debugger together with the following tools: * Interactive debugger * Trace tool See README.md for more information
- Loading branch information
1 parent
a4bb254
commit 704d1ff
Showing
5 changed files
with
1,325 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2018 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
py_library( | ||
name = "debugger", | ||
srcs = ["debugger.py"], | ||
deps = [ | ||
"//src/main/java/com/google/devtools/build/lib/starlarkdebug/proto:starlark_debugging_py_proto_library", | ||
], | ||
imports = ["."], | ||
) | ||
|
||
py_binary( | ||
name = "trace", | ||
srcs = ["trace.py"], | ||
deps = [":debugger"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Starlark debug | ||
|
||
This is the debugger and trace tool presented at Build Meetup 2021 (tutorial in video): | ||
https://www.youtube.com/watch?v=D7-BbD6QjeU | ||
|
||
## Preconditions | ||
|
||
Since you cannot start python in interactive mode using `bazel run` you need | ||
to build the starlark_debugger protobuf interface and then either copy it | ||
alongside the debugger tool or pass it's location in the PYTHONPATH | ||
environment variable. | ||
|
||
``` | ||
STARLARK_DEBUGGER_PROTO=src/main/java/com/google/devtools/build/lib/starlarkdebug/proto | ||
bazel build //${STARLARK_DEBUGGER_PROTO}:starlark_debugging_py_proto | ||
export PYTHONPATH="$(realpath bazel-bin/${STARLARK_DEBUGGER_PROTO})" | ||
python3 tools/starlarkdebug/debugger.py --help | ||
``` | ||
|
||
Note that above will use bazel to build the python interface but uses a local | ||
python installation to run the debugger. This installation needs the protobuf | ||
package installed which may fail due to mismatching protobuf versions in which | ||
you can: | ||
* Create a virtual python environment and install the protobuf version bazel uses there | ||
* Use protoc tool to generate the debugging interface with your version | ||
|
||
## Setup bazel for debugging | ||
|
||
Bazel needs to be running with the debugger active for the debugger tool to run | ||
otherwise you will get a `connection refused` message or similar. | ||
|
||
Execute the command to debug as normal but add a few extra build flags for | ||
it to wait for the debugger tool to connect. Here is a sample bazelrc file: | ||
``` | ||
build:starlark_debug # Enable the skylark debugger | ||
build:starlark_debug --experimental_skylark_debug | ||
build:starlark_debug --experimental_skylark_debug_server_port=7200 | ||
build:starlark_debug --keep_state_after_build=false | ||
``` | ||
|
||
You can optionally add `--build=false` to the command | ||
line as the build phase is not relevant for the starlark debug. | ||
|
||
Note if bazel exits immediately without triggering a breakpoint you may | ||
need to run `bazel shutdown` to clear the analysis cache. Disabling the | ||
`keep_state_after_build` flag (as in the example above) will prevent bazel | ||
to cache the execution in memory. | ||
|
||
## Interactive debugger | ||
|
||
The easiest usage as shown in the tutorial is to first execute the command | ||
to debug as normal with additional debug flags and then execute the debugger | ||
in interactive mode in another terminal while bazel waits for a debugger to | ||
attach. | ||
|
||
``` | ||
python3 -i tools/starlarkdebug/debugger.py | ||
``` |
Oops, something went wrong.