简体   繁体   English

如何在 GDB 中漂亮地打印 Rust HashMap?

[英]How do I pretty-print a Rust HashMap in GDB?

Here is a simple Rust example with a Vec and HashMap that I am running in GDB:这是我在 GDB 中运行的带有VecHashMap的简单 Rust 示例:

use std::collections::HashMap;

fn main() {
    let v = vec!["Mon", "Tue", "Wed"];
    println!("{:?}", v);

    let mut h = HashMap::new();
    h.insert(1,"Jan");
    h.insert(2,"Feb");
    h.insert(3,"Mar");
    h.insert(4,"Apr");
    println!("{:?}", h);
}

When I view the Vec it is pretty-printed, while the HashMap isn't:当我查看Vec它打印得很漂亮,而HashMap则不是:

bash$ rust-gdb target/debug/test_hashmap
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from target/debug/test_hashmap...done.
(gdb) b 12
Breakpoint 1 at 0xa056: file src/main.rs, line 12.
(gdb) r
Starting program: /home/john/proj/test_hashmap/target/debug/test_hashmap 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
["Mon", "Tue", "Wed"]

Breakpoint 1, test_hashmap::main () at src/main.rs:12
12          println!("{:?}", h);
(gdb) p v
$1 = Vec<&str>(len: 3, cap: 3) = {"Mon", "Tue", "Wed"}
(gdb) p h
$2 = HashMap<i32, &str, std::collections::hash::map::RandomState> = {hash_builder = RandomState = {k0 = 12919411587537124140, 
    k1 = 836420561220520453}, table = RawTable<i32, &str> = {capacity_mask = 31, size = 4, hashes = TaggedHashUintPtr = {
      Unique<usize> = {pointer = NonZero<*const usize> = {0x7ffff6c1c400}, _marker = PhantomData<usize>}}, 
    marker = PhantomData<(i32, &str)>}, resize_policy = DefaultResizePolicy}
(gdb) info pretty-printer
global pretty-printers:
  builtin
    mpx_bound128
  objfile /home/john/proj/test_hashmap/target/debug/test_hashmap pretty-printers:
  rust_pretty_printer_lookup_function
(gdb) n
{1: "Jan", 4: "Apr", 3: "Mar", 2: "Feb"}
13      }
(gdb) 

The program sees the HashMap (and prints its values ok), but I can't see those inside GDB.该程序可以看到HashMap (并正常打印其值),但我看不到 GDB 中的那些。 The Rust pretty printer seems to be enabled because Vec is printed out just fine. Rust 漂亮的打印机似乎已启用,因为Vec打印得很好。

How can I get the HashMap keys and values in regular GDB?如何在常规 GDB 中获取HashMap键和值?

At the moment it is not possible to pretty-print key-values items for an HashMap .目前无法为HashMap漂亮地打印键值项。

There is an open issue requesting this feature .一个请求此功能的未决问题

Pretty printing a BTreeMap will be enabled if this PR is merged .如果合并此 PR,将启用BTreeMap漂亮打印。

Both BTreeMap and HashMap APIs are similar. BTreeMap 和 HashMap API 是相似的。 So, you may be able to replace HashMap and BTreeMap for debugging.因此,您可以替换HashMapBTreeMap进行调试。

您可以使用 rust-gdb/rust-lldb,它可以漂亮地打印 HashMap 的键值项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM