Skip to content

Commit

Permalink
[GDB] expose arguments (#7)
Browse files Browse the repository at this point in the history
* [BAZEL] updated gdb rules

* [EXAMPLES] updated for gdb args

* [BAZEL] buildifier

* [BAZEL] rules refactoring

* [GITHUB] pull request template
  • Loading branch information
hexdae authored Jun 27, 2021
1 parent fdff8dd commit 772fe34
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 56 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ rust_embedded_repositories()

load("@rust_embedded//:deps.bzl", "rust_embedded_deps")

rust_embedded_deps()
rust_embedded_deps()
4 changes: 2 additions & 2 deletions deps.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
''' Transitive dependencies for Rust Embedded Tools '''
""" Transitive dependencies for Rust Embedded Tools """

load("@rust_embedded//crates:crates.bzl", "raze_fetch_remote_crates")
load("@rules_rust//rust:repositories.bzl", "rust_repositories")
Expand All @@ -9,4 +9,4 @@ def rust_embedded_deps():

bazel_skylib_workspace()
rust_repositories()
raze_fetch_remote_crates()
raze_fetch_remote_crates()
4 changes: 2 additions & 2 deletions examples/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Toolchain: arm-none-eabi-gcc v1.2.0.
http_archive(
name = "arm_none_eabi",
url = "https://github.com/d-asnaghi/bazel-arm-none-eabi/archive/v1.2.0.tar.gz",
sha256 = "34487973fd09f655a0b4531fb48cec5795bec303de30223aef43606b01fcb161",
strip_prefix = "bazel-arm-none-eabi-1.2.0"
strip_prefix = "bazel-arm-none-eabi-1.2.0",
url = "https://github.com/d-asnaghi/bazel-arm-none-eabi/archive/v1.2.0.tar.gz",
)

load("@arm_none_eabi//:deps.bzl", "arm_none_eabi_deps")
Expand Down
4 changes: 2 additions & 2 deletions examples/flash/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ cc_binary(
],
linkopts = [
"-nostdlib",
"-T $(execpath :linker)"
"-T $(execpath :linker)",
],
)

cargo_flash(
name = "flash",
file = ":firmware",
chip = "STM32F103C8",
file = ":firmware",
)
15 changes: 9 additions & 6 deletions examples/gdb-console/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ cc_binary(
],
linkopts = [
"-nostdlib",
"-T $(execpath :linker)"
"-T $(execpath :linker)",
],
)

gdb_console(
name = "gdb-console",
file = ":firmware",
chip = "STM32F103C8",
file = ":firmware",
gdb = "@arm_none_eabi//:gdb",
gdb_commands = [
"b main",
]
)
gdb_args = [
"-q",
"-ex 'file $(execpath :firmware)'",
"-ex 'target extended-remote 0.0.0.0:3333'",
# "-ex 'b main'",
],
)
4 changes: 2 additions & 2 deletions examples/gdb-server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ cc_binary(
],
linkopts = [
"-nostdlib",
"-T $(execpath :linker)"
"-T $(execpath :linker)",
],
)

gdb_server(
name = "gdb-server",
file = ":firmware",
chip = "STM32F103C8",
file = ":firmware",
)
4 changes: 2 additions & 2 deletions repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
''' Repositories for Rust Embedded Tools '''
""" Repositories for Rust Embedded Tools """

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
Expand Down Expand Up @@ -28,4 +28,4 @@ def rust_embedded_repositories():
],
)

raze_fetch_remote_crates()
raze_fetch_remote_crates()
95 changes: 56 additions & 39 deletions rules.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
""" Rules """

load("@bazel_skylib//rules:native_binary.bzl", "native_binary")

def cargo_flash(name, file, chip, bin=False):
def cargo_flash(name, file, chip, bin = False):
"""Use cargo-flash to load an elf file onto a chip
To find the supported chip targets use the following command
Expand All @@ -16,30 +17,35 @@ def cargo_flash(name, file, chip, bin=False):

file_type = "bin" if bin else "elf"
tool = "@rust_embedded//:cargo-flash"
cmd = """
echo "$(execpath {}) --chip {} --{} $(execpath {})" > $@
""".format(tool, chip, file_type, file)
args = [
"--chip {}".format(chip),
"--{} $(execpath {})".format(file_type, file),
]

script = """
#! /usr/bin/env bash
set -euo pipefail
bash -c \\"$(execpath {}) {}\\"
""".format(tool, " ".join(args))

native.genrule(
name = name,
srcs = [file],
outs = [name + ".sh"],
executable = True,
tools = [tool],
cmd = cmd,
cmd = """echo "{}" > $@""".format(script),
)


def cargo_embed_config(
name,
protocol="Swd",
flash="true",
reset="true",
halt="true",
log_level ="ERROR",
protocol = "Swd",
flash = "true",
reset = "true",
halt = "true",
log_level = "ERROR",
gdb_enabled = "false",
gdb_connection_string="0.0.0.0:3333",
):
gdb_connection_string = "0.0.0.0:3333"):
""" generate a custom cargo-embed config
Args:
Expand Down Expand Up @@ -77,8 +83,7 @@ def cargo_embed_config(
cmd = """echo "{}" > $@""".format(config),
)


def cargo_embed(name, file, chip, custom_config=None):
def cargo_embed(name, file, chip, config = None):
"""Use cargo-embed with a custom config
You can flash the probe, start a gdb server and
Expand All @@ -88,27 +93,35 @@ def cargo_embed(name, file, chip, custom_config=None):
name: the target name as a string
file: the label to the elf file
chip: string, the target chip
custom_config: the label of a custom config file
config: the label of a custom config file
"""
srcs = [file]
tool = "@rust_embedded//:cargo-embed"
cmd = "$(execpath {}) --chip {} --artifact-path $(execpath {})".format(tool, chip, file)
args = [
"--chip {}".format(chip),
"--artifact-path $(execpath {})".format(file),
]

if config:
srcs.append(config)
args.append("--custom-config $(execpath {})".format(config))

if custom_config:
srcs.append(custom_config)
cmd += " --custom-config $(execpath {})".format(custom_config)
script = """
#! /usr/bin/env bash
set -euo pipefail
bash -c \\"$(execpath {}) {}\\"
""".format(tool, " ".join(args))

native.genrule(
name = name,
srcs = srcs,
outs = [name + ".sh"],
executable = True,
tools = [tool],
cmd = """echo "{}" > $@""".format(cmd),
cmd = """echo "{}" > $@""".format(script),
)


def gdb_server(name, file, chip, log_level="ERROR", address="0.0.0.0", port="3333"):
def gdb_server(name, file, chip, log_level = "ERROR", address = "0.0.0.0", port = "3333"):
""" Launch a gdb server using cargo embed
Args:
Expand All @@ -132,43 +145,47 @@ def gdb_server(name, file, chip, log_level="ERROR", address="0.0.0.0", port="333
name = name,
file = file,
chip = chip,
custom_config = config,
config = config,
)


def gdb_console(name, file, chip, gdb, gdb_commands = [], log_level="ERROR", address="0.0.0.0", port="3333"):
def gdb_console(name, file, chip, gdb, gdb_args = [], log_level = "ERROR", address = "0.0.0.0", port = "3333"):
""" Launch a gdb console using a custom gdb executable
Args:
name: the name of the console target
file: binary to debug
chip: chip setting of cargo embed
gdb: label to the gdb file binary
gdb_commands: additional commands for gdb
gdb_args: list of arguments to gdb
log_level: log level of cargo embed
address: IP address of the GDB server
port: Port of the GDB serverV
"""
config = name + "_config"
server_binary = name + "_gdb_server"
gdb_binary = name + "_gdb_binary"
gdb_args = "-q"
gdb_cmds = [
"file $(execpath {})".format(file),
"set mem inaccessible-by-default off",
"target extended-remote {}:{}".format(address, port),
] + gdb_commands

for command in gdb_cmds:
gdb_args += " -ex=\'{}\'".format(command)
gdb_default_args = [
"-ex 'file $(execpath {})'".format(file),
"-ex 'target extended-remote {}:{}'".format(address, port),
]

for arg in gdb_default_args:
if arg not in gdb_args:
gdb_args.append(arg)

gdb_console_script = """
escape = {"\'": "\\'"}
for arg in gdb_args:
for key in escape:
arg.replace(key, escape.get(key))

script = """
#! /usr/bin/env bash
set -euo pipefail
trap 'killall cargo_bin_cargo_embed' EXIT SIGINT SIGTERM SIGHUP
bash -c \\"$(execpath {})\\" 2> /dev/null &
bash -c \\"$(execpath {}) {}\\"
""".format(server_binary, gdb_binary, gdb_args)
""".format(server_binary, gdb_binary, " ".join(gdb_args))

gdb_server(
name = server_binary,
Expand All @@ -190,6 +207,6 @@ def gdb_console(name, file, chip, gdb, gdb_commands = [], log_level="ERROR", add
srcs = [server_binary, file],
outs = [name + "_gdb_console.sh"],
tools = [gdb_binary],
cmd = """echo "{}" > $@""".format(gdb_console_script),
cmd = """echo "{}" > $@""".format(script),
executable = True,
)
)

0 comments on commit 772fe34

Please sign in to comment.