Skip to content

Commit

Permalink
auto merge of rust-lang#19144 : michaelwoerister/rust/lldb-scripts, r…
Browse files Browse the repository at this point in the history
…=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.
  • Loading branch information
bors committed Nov 26, 2014
2 parents 1a44875 + 67ba096 commit 6faff24
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ include $(CFG_SRC_DIR)mk/rustllvm.mk
include $(CFG_SRC_DIR)mk/docs.mk
# LLVM
include $(CFG_SRC_DIR)mk/llvm.mk
# Rules for installing debugger scripts
include $(CFG_SRC_DIR)mk/debuggers.mk

######################################################################
# Secondary makefiles, conditionalized for speed
Expand Down
5 changes: 3 additions & 2 deletions mk/clean.mk
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ clean-generic-$(2)-$(1):
-name '*.lib' -o \
-name '*.dll' -o \
-name '*.def' -o \
-name '*.py' -o \
-name '*.bc' \
\) \
| xargs rm -f
Expand All @@ -78,7 +79,7 @@ define CLEAN_HOST_STAGE_N

clean$(1)_H_$(2): \
$$(foreach crate,$$(CRATES),clean$(1)_H_$(2)-lib-$$(crate)) \
$$(foreach tool,$$(TOOLS),clean$(1)_H_$(2)-tool-$$(tool))
$$(foreach tool,$$(TOOLS) $$(DEBUGGER_BIN_SCRIPTS),clean$(1)_H_$(2)-tool-$$(tool))
$$(Q)rm -fr $(2)/rt/libbacktrace

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

clean$(1)_T_$(2)_H_$(3): \
$$(foreach crate,$$(CRATES),clean$(1)_T_$(2)_H_$(3)-lib-$$(crate)) \
$$(foreach tool,$$(TOOLS),clean$(1)_T_$(2)_H_$(3)-tool-$$(tool))
$$(foreach tool,$$(TOOLS) $$(DEBUGGER_BIN_SCRIPTS),clean$(1)_T_$(2)_H_$(3)-tool-$$(tool))
$$(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a
$$(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
$(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/librun_pass_stage* # For unix
Expand Down
59 changes: 59 additions & 0 deletions mk/debuggers.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2014 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

######################################################################
# Copy debugger related scripts
######################################################################

DEBUGGER_RUSTLIB_ETC_SCRIPTS=lldb_rust_formatters.py
DEBUGGER_BIN_SCRIPTS=rust-lldb

DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS=$(foreach script,$(DEBUGGER_RUSTLIB_ETC_SCRIPTS), \
$(CFG_SRC_DIR)src/etc/$(script))
DEBUGGER_BIN_SCRIPTS_ABS=$(foreach script,$(DEBUGGER_BIN_SCRIPTS), \
$(CFG_SRC_DIR)src/etc/$(script))

DEBUGGER_SCRIPTS_ALL=$(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $(DEBUGGER_BIN_SCRIPTS_ABS)

# $(1) - the stage to copy to
# $(2) - the host triple
define DEF_INSTALL_DEBUGGER_SCRIPTS_HOST

tmp/install-debugger-scripts$(1)_H_$(2).done: $$(DEBUGGER_SCRIPTS_ALL)
$(Q)mkdir -p $$(HBIN$(1)_H_$(2))
$(Q)mkdir -p $$(HLIB$(1)_H_$(2))/rustlib/etc
$(Q)install $(DEBUGGER_BIN_SCRIPTS_ABS) $$(HBIN$(1)_H_$(2))
$(Q)install $(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $$(HLIB$(1)_H_$(2))/rustlib/etc
$(Q)touch $$@
endef

# Expand host make-targets for all stages
$(foreach stage,$(STAGES), \
$(foreach host,$(CFG_HOST), \
$(eval $(call DEF_INSTALL_DEBUGGER_SCRIPTS_HOST,$(stage),$(host)))))

# $(1) is the stage number
# $(2) is the target triple
# $(3) is the host triple
define DEF_INSTALL_DEBUGGER_SCRIPTS_TARGET

tmp/install-debugger-scripts$(1)_T_$(2)_H_$(3).done: $$(DEBUGGER_SCRIPTS_ALL)
$(Q)mkdir -p $$(TBIN$(1)_T_$(2)_H_$(3))
$(Q)mkdir -p $$(TLIB$(1)_T_$(2)_H_$(3))/rustlib/etc
$(Q)install $(DEBUGGER_BIN_SCRIPTS_ABS) $$(TBIN$(1)_T_$(2)_H_$(3))
$(Q)install $(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $$(TLIB$(1)_T_$(2)_H_$(3))/rustlib/etc
$(Q)touch $$@
endef

# Expand target make-targets for all stages
$(foreach stage,$(STAGES), \
$(foreach target,$(CFG_TARGET), \
$(foreach host,$(CFG_HOST), \
$(eval $(call DEF_INSTALL_DEBUGGER_SCRIPTS_TARGET,$(stage),$(target),$(host))))))
6 changes: 4 additions & 2 deletions mk/main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
else
HSREQ$(1)_H_$(3) = \
$$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
$$(MKFILE_DEPS)
$$(MKFILE_DEPS) \
tmp/install-debugger-scripts$(1)_H_$(3).done
endif

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

# Prerequisites for a working stageN compiler and complete set of target
# libraries
Expand Down
7 changes: 6 additions & 1 deletion mk/prepare.mk
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ prepare-base-$(1): PREPARE_DEST_LIB_DIR=$$(PREPARE_DEST_DIR)/$$(CFG_LIBDIR_RELAT
prepare-base-$(1): PREPARE_DEST_MAN_DIR=$$(PREPARE_DEST_DIR)/share/man/man1
prepare-base-$(1): prepare-everything-$(1)

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

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

Expand All @@ -167,8 +167,13 @@ prepare-host-tools-$(1): \
prepare-host-dirs-$(1): prepare-maybe-clean-$(1)
$$(call PREPARE_DIR,$$(PREPARE_DEST_BIN_DIR))
$$(call PREPARE_DIR,$$(PREPARE_DEST_LIB_DIR))
$$(call PREPARE_DIR,$$(PREPARE_DEST_LIB_DIR)/rustlib/etc)
$$(call PREPARE_DIR,$$(PREPARE_DEST_MAN_DIR))

prepare-debugger-scripts-$(1): prepare-host-dirs-$(1) $(DEBUGGER_SCRIPTS_ALL)
$$(Q)$$(PREPARE_BIN_CMD) $(DEBUGGER_BIN_SCRIPTS_ABS) $$(PREPARE_DEST_BIN_DIR)
$$(Q)$$(PREPARE_LIB_CMD) $(DEBUGGER_RUSTLIB_ETC_SCRIPTS_ABS) $$(PREPARE_DEST_LIB_DIR)/rustlib/etc

$$(foreach tool,$$(PREPARE_TOOLS), \
$$(foreach host,$$(CFG_HOST), \
$$(eval $$(call DEF_PREPARE_HOST_TOOL,$$(tool),$$(PREPARE_STAGE),$$(host),$(1)))))
Expand Down
11 changes: 7 additions & 4 deletions src/etc/lldb_rust_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ def print_struct_val_starting_from(field_start_index, val, internal_dict):
assert val.GetType().GetTypeClass() == lldb.eTypeClassStruct

t = val.GetType()
has_field_names = type_has_field_names(t)
type_name = extract_type_name(t.GetName())
num_children = val.num_children

if (num_children - field_start_index) == 0:
# The only field of this struct is the enum discriminant
return type_name

has_field_names = type_has_field_names(t)

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

num_children = val.num_children

def render_child(child_index):
this = ""
if has_field_names:
Expand All @@ -105,7 +109,6 @@ def print_enum_val(val, internal_dict):

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


if val.num_children == 1:
# This is either an enum with just one variant, or it is an Option-like enum
# where the discriminant is encoded in a non-nullable pointer field. We find
Expand Down
30 changes: 30 additions & 0 deletions src/etc/rust-lldb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh
# Copyright 2014 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

# Exit if anything fails
set -e

# Create a tempfile containing the LLDB script we want to execute on startup
TMPFILE=`mktemp /tmp/rust-lldb-commands.XXXXXX`

# Make sure to delete the tempfile no matter what
trap "rm -f $TMPFILE; exit" INT TERM EXIT

# Find out where to look for the pretty printer Python module
RUSTC_SYSROOT=`rustc -Zprint-sysroot`

# Write the LLDB script to the tempfile
echo "command script import \"$RUSTC_SYSROOT/lib/rustlib/etc/lldb_rust_formatters.py\"" >> $TMPFILE
echo "type summary add --no-value --python-function lldb_rust_formatters.print_val -x \".*\" --category Rust" >> $TMPFILE
echo "type category enable Rust" >> $TMPFILE

# Call LLDB with the script added to the argument list
lldb --source-before-file="$TMPFILE" "$@"
9 changes: 6 additions & 3 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ debugging_opts!(
FLOWGRAPH_PRINT_LOANS,
FLOWGRAPH_PRINT_MOVES,
FLOWGRAPH_PRINT_ASSIGNS,
FLOWGRAPH_PRINT_ALL
FLOWGRAPH_PRINT_ALL,
PRINT_SYSROOT
]
0
)

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

#[deriving(Clone)]
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_trans/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ fn run_compiler(args: &[String]) {
describe_lints(&ls, false);
return;
}

let sess = build_session(sopts, None, descriptions);
if sess.debugging_opt(config::PRINT_SYSROOT) {
println!("{}", sess.sysroot().display());
return;
}

early_error("no input filename given");
}
1u => {
Expand Down

0 comments on commit 6faff24

Please sign in to comment.