Skip to content

Commit 6faff24

Browse files
committed
auto merge of #19144 : michaelwoerister/rust/lldb-scripts, r=alexcrichton
This PR adds the `rust-lldb` script (feel free to bikeshed about the name). The script will start LLDB and, before doing anything else, load [LLDB type summaries](http://lldb.llvm.org/varformats.html) that will make LLDB print values with Rust syntax. Just use the script like you would normally use LLDB: ``` rust-lldb executable-to-debug --and-any-other-commandline --args ``` The script will just add one additional commandline argument to the LLDB invocation and pass along the rest of the arguments to LLDB after that. Given the following program... ```rust fn main() { let x = Some(1u); let y = [0, 1, 2i]; let z = (x, y); println!("{} {} {}", x, y, z); } ``` ...*without* the 'LLDB type summaries', values will be printed something like this... ``` (lldb) p x (core::option::Option<uint>) $3 = { = (RUST$ENUM$DISR = Some) = (RUST$ENUM$DISR = Some, 1) } (lldb) p y (long [3]) $4 = ([0] = 0, [1] = 1, [2] = 2) (lldb) p z ((core::option::Option<uint>, [int, ..3])) $5 = { = { = (RUST$ENUM$DISR = Some) = (RUST$ENUM$DISR = Some, 1) } = ([0] = 0, [1] = 1, [2] = 2) } ``` ...*with* the 'LLDB type summaries', values will be printed like this: ``` (lldb) p x (core::option::Option<uint>) $0 = Some(1) (lldb) p y (long [3]) $1 = [0, 1, 2] (lldb) p z ((core::option::Option<uint>, [int, ..3])) $2 = (Some(1), [0, 1, 2]) ``` The 'LLDB type summaries' used by the script have been in use for a while in the LLDB autotests but I still consider them to be of alpha-version quality. If you see anything weird when you use them, feel free to file an issue. The script will use whatever Rust "installation" is in PATH, so whichever `rustc` will be called if you type `rustc` into the console, this is the one that the script will ask for the LLDB extension module location. The build system will take care of putting the script and LLDB python module in the right places, whether you want to use the stage1 or stage2 compiler or the one coming with `make install` / `rustup.sh`. Since I don't have much experience with the build system, Makefiles and shell scripts, please look these changes over carefully.
2 parents 1a44875 + 67ba096 commit 6faff24

File tree

9 files changed

+124
-12
lines changed

9 files changed

+124
-12
lines changed

Makefile.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ include $(CFG_SRC_DIR)mk/rustllvm.mk
198198
include $(CFG_SRC_DIR)mk/docs.mk
199199
# LLVM
200200
include $(CFG_SRC_DIR)mk/llvm.mk
201+
# Rules for installing debugger scripts
202+
include $(CFG_SRC_DIR)mk/debuggers.mk
201203

202204
######################################################################
203205
# Secondary makefiles, conditionalized for speed

mk/clean.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ clean-generic-$(2)-$(1):
6363
-name '*.lib' -o \
6464
-name '*.dll' -o \
6565
-name '*.def' -o \
66+
-name '*.py' -o \
6667
-name '*.bc' \
6768
\) \
6869
| xargs rm -f
@@ -78,7 +79,7 @@ define CLEAN_HOST_STAGE_N
7879

7980
clean$(1)_H_$(2): \
8081
$$(foreach crate,$$(CRATES),clean$(1)_H_$(2)-lib-$$(crate)) \
81-
$$(foreach tool,$$(TOOLS),clean$(1)_H_$(2)-tool-$$(tool))
82+
$$(foreach tool,$$(TOOLS) $$(DEBUGGER_BIN_SCRIPTS),clean$(1)_H_$(2)-tool-$$(tool))
8283
$$(Q)rm -fr $(2)/rt/libbacktrace
8384

8485
clean$(1)_H_$(2)-tool-%:
@@ -98,7 +99,7 @@ define CLEAN_TARGET_STAGE_N
9899

99100
clean$(1)_T_$(2)_H_$(3): \
100101
$$(foreach crate,$$(CRATES),clean$(1)_T_$(2)_H_$(3)-lib-$$(crate)) \
101-
$$(foreach tool,$$(TOOLS),clean$(1)_T_$(2)_H_$(3)-tool-$$(tool))
102+
$$(foreach tool,$$(TOOLS) $$(DEBUGGER_BIN_SCRIPTS),clean$(1)_T_$(2)_H_$(3)-tool-$$(tool))
102103
$$(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a
103104
$$(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
104105
$(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/librun_pass_stage* # For unix

mk/debuggers.mk

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
# file at the top-level directory of this distribution and at
3+
# http://rust-lang.org/COPYRIGHT.
4+
#
5+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
# option. This file may not be copied, modified, or distributed
9+
# except according to those terms.
10+
11+
######################################################################
12+
# Copy debugger related scripts
13+
######################################################################
14+
15+
DEBUGGER_RUSTLIB_ETC_SCRIPTS=lldb_rust_formatters.py
16+
DEBUGGER_BIN_SCRIPTS=rust-lldb
17+
18+
DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS=$(foreach script,$(DEBUGGER_RUSTLIB_ETC_SCRIPTS), \
19+
$(CFG_SRC_DIR)src/etc/$(script))
20+
DEBUGGER_BIN_SCRIPTS_ABS=$(foreach script,$(DEBUGGER_BIN_SCRIPTS), \
21+
$(CFG_SRC_DIR)src/etc/$(script))
22+
23+
DEBUGGER_SCRIPTS_ALL=$(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $(DEBUGGER_BIN_SCRIPTS_ABS)
24+
25+
# $(1) - the stage to copy to
26+
# $(2) - the host triple
27+
define DEF_INSTALL_DEBUGGER_SCRIPTS_HOST
28+
29+
tmp/install-debugger-scripts$(1)_H_$(2).done: $$(DEBUGGER_SCRIPTS_ALL)
30+
$(Q)mkdir -p $$(HBIN$(1)_H_$(2))
31+
$(Q)mkdir -p $$(HLIB$(1)_H_$(2))/rustlib/etc
32+
$(Q)install $(DEBUGGER_BIN_SCRIPTS_ABS) $$(HBIN$(1)_H_$(2))
33+
$(Q)install $(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $$(HLIB$(1)_H_$(2))/rustlib/etc
34+
$(Q)touch $$@
35+
endef
36+
37+
# Expand host make-targets for all stages
38+
$(foreach stage,$(STAGES), \
39+
$(foreach host,$(CFG_HOST), \
40+
$(eval $(call DEF_INSTALL_DEBUGGER_SCRIPTS_HOST,$(stage),$(host)))))
41+
42+
# $(1) is the stage number
43+
# $(2) is the target triple
44+
# $(3) is the host triple
45+
define DEF_INSTALL_DEBUGGER_SCRIPTS_TARGET
46+
47+
tmp/install-debugger-scripts$(1)_T_$(2)_H_$(3).done: $$(DEBUGGER_SCRIPTS_ALL)
48+
$(Q)mkdir -p $$(TBIN$(1)_T_$(2)_H_$(3))
49+
$(Q)mkdir -p $$(TLIB$(1)_T_$(2)_H_$(3))/rustlib/etc
50+
$(Q)install $(DEBUGGER_BIN_SCRIPTS_ABS) $$(TBIN$(1)_T_$(2)_H_$(3))
51+
$(Q)install $(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $$(TLIB$(1)_T_$(2)_H_$(3))/rustlib/etc
52+
$(Q)touch $$@
53+
endef
54+
55+
# Expand target make-targets for all stages
56+
$(foreach stage,$(STAGES), \
57+
$(foreach target,$(CFG_TARGET), \
58+
$(foreach host,$(CFG_HOST), \
59+
$(eval $(call DEF_INSTALL_DEBUGGER_SCRIPTS_TARGET,$(stage),$(target),$(host))))))

mk/main.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
356356
else
357357
HSREQ$(1)_H_$(3) = \
358358
$$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
359-
$$(MKFILE_DEPS)
359+
$$(MKFILE_DEPS) \
360+
tmp/install-debugger-scripts$(1)_H_$(3).done
360361
endif
361362

362363
# Prerequisites for using the stageN compiler to build target artifacts
@@ -370,7 +371,8 @@ TSREQ$(1)_T_$(2)_H_$(3) = \
370371
SREQ$(1)_T_$(2)_H_$(3) = \
371372
$$(TSREQ$(1)_T_$(2)_H_$(3)) \
372373
$$(foreach dep,$$(TARGET_CRATES), \
373-
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep))
374+
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
375+
tmp/install-debugger-scripts$(1)_T_$(2)_H_$(3).done
374376

375377
# Prerequisites for a working stageN compiler and complete set of target
376378
# libraries

mk/prepare.mk

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ prepare-base-$(1): PREPARE_DEST_LIB_DIR=$$(PREPARE_DEST_DIR)/$$(CFG_LIBDIR_RELAT
155155
prepare-base-$(1): PREPARE_DEST_MAN_DIR=$$(PREPARE_DEST_DIR)/share/man/man1
156156
prepare-base-$(1): prepare-everything-$(1)
157157

158-
prepare-everything-$(1): prepare-host-$(1) prepare-targets-$(1)
158+
prepare-everything-$(1): prepare-host-$(1) prepare-targets-$(1) prepare-debugger-scripts-$(1)
159159

160160
prepare-host-$(1): prepare-host-tools-$(1)
161161

@@ -167,8 +167,13 @@ prepare-host-tools-$(1): \
167167
prepare-host-dirs-$(1): prepare-maybe-clean-$(1)
168168
$$(call PREPARE_DIR,$$(PREPARE_DEST_BIN_DIR))
169169
$$(call PREPARE_DIR,$$(PREPARE_DEST_LIB_DIR))
170+
$$(call PREPARE_DIR,$$(PREPARE_DEST_LIB_DIR)/rustlib/etc)
170171
$$(call PREPARE_DIR,$$(PREPARE_DEST_MAN_DIR))
171172

173+
prepare-debugger-scripts-$(1): prepare-host-dirs-$(1) $(DEBUGGER_SCRIPTS_ALL)
174+
$$(Q)$$(PREPARE_BIN_CMD) $(DEBUGGER_BIN_SCRIPTS_ABS) $$(PREPARE_DEST_BIN_DIR)
175+
$$(Q)$$(PREPARE_LIB_CMD) $(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $$(PREPARE_DEST_LIB_DIR)/rustlib/etc
176+
172177
$$(foreach tool,$$(PREPARE_TOOLS), \
173178
$$(foreach host,$$(CFG_HOST), \
174179
$$(eval $$(call DEF_PREPARE_HOST_TOOL,$$(tool),$$(PREPARE_STAGE),$$(host),$(1)))))

src/etc/lldb_rust_formatters.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ def print_struct_val_starting_from(field_start_index, val, internal_dict):
6969
assert val.GetType().GetTypeClass() == lldb.eTypeClassStruct
7070

7171
t = val.GetType()
72-
has_field_names = type_has_field_names(t)
7372
type_name = extract_type_name(t.GetName())
73+
num_children = val.num_children
74+
75+
if (num_children - field_start_index) == 0:
76+
# The only field of this struct is the enum discriminant
77+
return type_name
78+
79+
has_field_names = type_has_field_names(t)
7480

7581
if has_field_names:
7682
template = "%(type_name)s {\n%(body)s\n}"
@@ -83,8 +89,6 @@ def print_struct_val_starting_from(field_start_index, val, internal_dict):
8389
# this is a tuple, so don't print the type name
8490
type_name = ""
8591

86-
num_children = val.num_children
87-
8892
def render_child(child_index):
8993
this = ""
9094
if has_field_names:
@@ -105,7 +109,6 @@ def print_enum_val(val, internal_dict):
105109

106110
assert val.GetType().GetTypeClass() == lldb.eTypeClassUnion
107111

108-
109112
if val.num_children == 1:
110113
# This is either an enum with just one variant, or it is an Option-like enum
111114
# where the discriminant is encoded in a non-nullable pointer field. We find

src/etc/rust-lldb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
# Copyright 2014 The Rust Project Developers. See the COPYRIGHT
3+
# file at the top-level directory of this distribution and at
4+
# http://rust-lang.org/COPYRIGHT.
5+
#
6+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
# option. This file may not be copied, modified, or distributed
10+
# except according to those terms.
11+
12+
# Exit if anything fails
13+
set -e
14+
15+
# Create a tempfile containing the LLDB script we want to execute on startup
16+
TMPFILE=`mktemp /tmp/rust-lldb-commands.XXXXXX`
17+
18+
# Make sure to delete the tempfile no matter what
19+
trap "rm -f $TMPFILE; exit" INT TERM EXIT
20+
21+
# Find out where to look for the pretty printer Python module
22+
RUSTC_SYSROOT=`rustc -Zprint-sysroot`
23+
24+
# Write the LLDB script to the tempfile
25+
echo "command script import \"$RUSTC_SYSROOT/lib/rustlib/etc/lldb_rust_formatters.py\"" >> $TMPFILE
26+
echo "type summary add --no-value --python-function lldb_rust_formatters.print_val -x \".*\" --category Rust" >> $TMPFILE
27+
echo "type category enable Rust" >> $TMPFILE
28+
29+
# Call LLDB with the script added to the argument list
30+
lldb --source-before-file="$TMPFILE" "$@"

src/librustc/session/config.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ debugging_opts!(
212212
FLOWGRAPH_PRINT_LOANS,
213213
FLOWGRAPH_PRINT_MOVES,
214214
FLOWGRAPH_PRINT_ASSIGNS,
215-
FLOWGRAPH_PRINT_ALL
215+
FLOWGRAPH_PRINT_ALL,
216+
PRINT_SYSROOT
216217
]
217218
0
218219
)
219220

220221
pub fn debugging_opts_map() -> Vec<(&'static str, &'static str, u64)> {
221-
vec!(("verbose", "in general, enable more debug printouts", VERBOSE),
222+
vec![("verbose", "in general, enable more debug printouts", VERBOSE),
222223
("time-passes", "measure time of each rustc pass", TIME_PASSES),
223224
("count-llvm-insns", "count where LLVM \
224225
instrs originate", COUNT_LLVM_INSNS),
@@ -256,7 +257,9 @@ pub fn debugging_opts_map() -> Vec<(&'static str, &'static str, u64)> {
256257
("flowgraph-print-assigns", "Include assignment analysis data in \
257258
--pretty flowgraph output", FLOWGRAPH_PRINT_ASSIGNS),
258259
("flowgraph-print-all", "Include all dataflow analysis data in \
259-
--pretty flowgraph output", FLOWGRAPH_PRINT_ALL))
260+
--pretty flowgraph output", FLOWGRAPH_PRINT_ALL),
261+
("print-sysroot", "Print the sysroot as used by this rustc invocation",
262+
PRINT_SYSROOT)]
260263
}
261264

262265
#[deriving(Clone)]

src/librustc_trans/driver/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ fn run_compiler(args: &[String]) {
7575
describe_lints(&ls, false);
7676
return;
7777
}
78+
79+
let sess = build_session(sopts, None, descriptions);
80+
if sess.debugging_opt(config::PRINT_SYSROOT) {
81+
println!("{}", sess.sysroot().display());
82+
return;
83+
}
84+
7885
early_error("no input filename given");
7986
}
8087
1u => {

0 commit comments

Comments
 (0)