about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-11-26 20:12:09 +0000
committerbors <bors@rust-lang.org>2014-11-26 20:12:09 +0000
commit6faff24ec85744de092a7d7c2378370f65d623bb (patch)
treeb6e67e9a410599ef74e608b2cb015a8ad5a9979e /src
parent1a44875af985de43d514192d43ef260a24e83d26 (diff)
parent67ba096cc30ec0f5fac8deec23d5557012e33d27 (diff)
downloadrust-6faff24ec85744de092a7d7c2378370f65d623bb.tar.gz
rust-6faff24ec85744de092a7d7c2378370f65d623bb.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/etc/lldb_rust_formatters.py11
-rwxr-xr-xsrc/etc/rust-lldb30
-rw-r--r--src/librustc/session/config.rs9
-rw-r--r--src/librustc_trans/driver/mod.rs7
4 files changed, 50 insertions, 7 deletions
diff --git a/src/etc/lldb_rust_formatters.py b/src/etc/lldb_rust_formatters.py
index 642235ed4e3..7924d63c8e0 100644
--- a/src/etc/lldb_rust_formatters.py
+++ b/src/etc/lldb_rust_formatters.py
@@ -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}"
@@ -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:
@@ -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
diff --git a/src/etc/rust-lldb b/src/etc/rust-lldb
new file mode 100755
index 00000000000..19f36df7dba
--- /dev/null
+++ b/src/etc/rust-lldb
@@ -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" "$@"
diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs
index 82cf8f28e3d..8b4918b6db0 100644
--- a/src/librustc/session/config.rs
+++ b/src/librustc/session/config.rs
@@ -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),
@@ -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)]
diff --git a/src/librustc_trans/driver/mod.rs b/src/librustc_trans/driver/mod.rs
index af5983244df..658be9169af 100644
--- a/src/librustc_trans/driver/mod.rs
+++ b/src/librustc_trans/driver/mod.rs
@@ -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 => {