Skip to contentSkip to conversation

Optimizing the print format of the QEMU monitor 'info mtree'

PATCH In review Chao Liu 4 participants 7 messages Jun 13, 2025 – Jun 16, 2025

lore
Share & tools
lore

Chao Liu

author
#0 lore raw

v7 cover Optimizing the print format of the QEMU monitor 'info mtree'

From: Chao Liu <[email protected]>

Hi, all:

After several rounds of discussion, I think that adding a -t option to the info mtree command, which enables the display of tree-like node characters (e.g., +--, |--), is a better approach.

As BALATON Zoltan pointed out, retaining space-based indentation for displaying memory region (mr) nodes helps ensure that the output remains easily parseable by other programs. This also provides better compatibility with existing tools and scripts.

Users can choose between:

- Without '-t': The default format using spaces for indentation, suitable for scripting and parsing;
- With '-t': The tree-style format using visual characters, ideal for human-readable output.

This way, both usability and compatibility are preserved.

---

v7:
- Rebased on the top of current upstream:
d9ce74873a6a ("Merge tag 'pull-vfio-20250611' of https://github.com/legoater/qemu
into staging")
- Add -t option to 'info mtree' for tree-style node display

v6:
- Replaced the bool type with enum mtree_node_type to improve code readability;
- Modified the output to use only one horizontal dash instead of two, and aligned character printing for a cleaner look.

v5:
- Fix one comment, level 0 no longer prints line characters.

v4:
- When printing the child nodes of a single node, the line characters from the parent node's level are no longer printed, making the output more concise and clear overall;

- Use more commonly used ASCII characters, such as '|--' instead of '├──',
  and '`--' instead of '└──';

- Control the number of prefix characters to reduce unnecessary output.

v3:
- Use more maintainable c functions instead of macros, as per review comments.

v2:
- Enrich the commit message, add 'info mtree' print example;
- Optimize the code implementation according to the review comments.

--
Regards,
Chao

Chao Liu (1):
system: improve visual representation of node hierarchy in 'info
mtree' output for qemu monitor

 hmp-commands-info.hx    |   5 +-
 include/system/memory.h |   3 +-
 monitor/hmp-cmds.c      |   3 +-
 system/memory.c         | 150 ++++++++++++++++++++++++++++++++++++----
 4 files changed, 143 insertions(+), 18 deletions(-)
Signature · 1 line
2.48.1
author
#1 lore raw

PATCH v7 1/1 system: improve visual representation of node hierarchy in 'info mtree' output for qemu monitor

From: Chao Liu <[email protected]>

Make the hierarchical relationship between nodes clearer by adding characters.

e.g.

```
$ qemu-system-riscv64 -M virt -monitor stdio -display none
(qemu) info mtree -v

...
address-space: memory
`- 0000000000000000-ffffffffffffffff (prio 0, i/o): system
   |- 0000000000001000-000000000000ffff (prio 0, rom): riscv_virt_board.mrom
...
   |- 0000000003000000-000000000300ffff (prio 0, i/o): gpex_ioport_window
   |  `- 0000000003000000-000000000300ffff (prio 0, i/o): gpex_ioport
   |- 0000000004000000-0000000005ffffff (prio 0, i/o): platform bus
...
   |- 0000000080000000-0000000087ffffff (prio 0, ram): riscv_virt_board.ram
   `- 0000000400000000-00000007ffffffff (prio 0, i/o): alias pcie-mmio-high @gpex_mmio_window
```
---
 hmp-commands-info.hx    |   5 +-
 include/system/memory.h |   3 +-
 monitor/hmp-cmds.c      |   3 +-
 system/memory.c         | 150 ++++++++++++++++++++++++++++++++++++----
 4 files changed, 143 insertions(+), 18 deletions(-)
hmp-commands-info.hx +3 -2
index 639a450ee5..a1b098d4ac 100644
@@ -228,10 +228,11 @@ ERST
 
     {
         .name       = "mtree",
-        .args_type  = "flatview:-f,dispatch_tree:-d,owner:-o,disabled:-D",
-        .params     = "[-f][-d][-o][-D]",
+        .args_type  = "flatview:-f,dispatch_tree:-d,treeview:-t,owner:-o,disabled:-D",
+        .params     = "[-f][-d][-t][-o][-D]",
         .help       = "show memory tree (-f: dump flat view for address spaces;"
                       "-d: dump dispatch tree, valid with -f only);"
+                      "-t: dump tree node characters before memory regions;"
                       "-o: dump region owners/parents;"
                       "-D: dump disabled regions",
         .cmd        = hmp_info_mtree,
include/system/memory.h +2 -1
index 0848690ea4..3bb9c3e2dd 100644
@@ -2576,7 +2576,8 @@ bool memory_global_dirty_log_start(unsigned int flags, Error **errp);
  */
 void memory_global_dirty_log_stop(unsigned int flags);
 
-void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled);
+void mtree_info(bool flatview, bool dispatch_tree, bool treeview,
+                bool owner, bool disabled);
 
 bool memory_region_access_valid(MemoryRegion *mr, hwaddr addr,
                                 unsigned size, bool is_write,
monitor/hmp-cmds.c +2 -1
index 74a0f56566..6378636406 100644
@@ -412,11 +412,12 @@ void hmp_boot_set(Monitor *mon, const QDict *qdict)
 void hmp_info_mtree(Monitor *mon, const QDict *qdict)
 {
     bool flatview = qdict_get_try_bool(qdict, "flatview", false);
+    bool treeview = qdict_get_try_bool(qdict, "treeview", false);
     bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
     bool owner = qdict_get_try_bool(qdict, "owner", false);
     bool disabled = qdict_get_try_bool(qdict, "disabled", false);
 
-    mtree_info(flatview, dispatch_tree, owner, disabled);
+    mtree_info(flatview, dispatch_tree, treeview, owner, disabled);
 }
 
 #if defined(CONFIG_FDT)
system/memory.c +136 -14

243 lines not on this page · raw message

Signature · 1 line
2.48.1
#2 lore raw
In reply to Chao Liu
Quoted 4 lines · Chao Liu

From: Chao Liu <[email protected]>

Hi, all:

Quoted 8 lines

After several rounds of discussion, I think that adding a -t option to the info mtree command, which enables the display of tree-like node characters (e.g., +--, |--), is a better approach.

As BALATON Zoltan pointed out, retaining space-based indentation for displaying memory region (mr) nodes helps ensure that the output remains easily parseable by other programs. This also provides better compatibility with existing tools and scripts.

If people really feed the output of HMP info mtree to parsers, we should probably provide the information via QMP.

Quoted 8 lines

Users can choose between:

- Without '-t': The default format using spaces for indentation, suitable for scripting and parsing;
- With '-t': The tree-style format using visual characters, ideal for human-readable output.

This way, both usability and compatibility are preserved.

Maintaining compatibility is a choice in HMP, not a requirement. I'm not judging, mind.

#3 lore raw
Quoted 10 lines · Markus Armbruster
Quoted 8 lines · Chao Liu

From: Chao Liu <[email protected]>

Hi, all:

After several rounds of discussion, I think that adding a -t option to the info mtree command, which enables the display of tree-like node characters (e.g., +--, |--), is a better approach.

Quoted 8 lines
Quoted 4 lines

As BALATON Zoltan pointed out, retaining space-based indentation for displaying memory region (mr) nodes helps ensure that the output remains easily parseable by other programs. This also provides better compatibility with existing tools and scripts.

If people really feed the output of HMP info mtree to parsers, we should probably provide the information via QMP.

Thank you for your helpful advice. I think the next step is to try implementing "info mtree" via QMP first, and then have it called by HMP.

I’ve added it to my to-do list, and I’ll try to implement it using QMP in the next phase.

Quoted 11 lines
Quoted 8 lines

Users can choose between:

- Without '-t': The default format using spaces for indentation, suitable for scripting and parsing;
- With '-t': The tree-style format using visual characters, ideal for human-readable output.

This way, both usability and compatibility are preserved.

Maintaining compatibility is a choice in HMP, not a requirement. I'm not judging, mind.

#4 lore raw
Quoted 16 lines · Chao Liu
Quoted 15 lines · Markus Armbruster
Quoted 12 lines · Chao Liu

From: Chao Liu <[email protected]>

Hi, all:

After several rounds of discussion, I think that adding a -t option to the info mtree command, which enables the display of tree-like node characters (e.g., +--, |--), is a better approach.

As BALATON Zoltan pointed out, retaining space-based indentation for displaying memory region (mr) nodes helps ensure that the output remains easily parseable by other programs. This also provides better compatibility with existing tools and scripts.

Quoted 6 lines
Quoted 2 lines

If people really feed the output of HMP info mtree to parsers, we should probably provide the information via QMP.

Thank you for your helpful advice. I think the next step is to try implementing "info mtree" via QMP first, and then have it called by HMP.

I’ve added it to my to-do list, and I’ll try to implement it using QMP in the next phase.

First question before you actually do that: use cases for feeding the information to programs? You might have answers already; I'm not on top of prior conversations.

[...]

#5 lore raw
Quoted 23 lines · Markus Armbruster
Quoted 21 lines · Chao Liu
Quoted 10 lines · Markus Armbruster
Quoted 8 lines · Chao Liu

From: Chao Liu <[email protected]>

Hi, all:

After several rounds of discussion, I think that adding a -t option to the info mtree command, which enables the display of tree-like node characters (e.g., +--, |--), is a better approach.

Quoted 7 lines
Quoted 4 lines

As BALATON Zoltan pointed out, retaining space-based indentation for displaying memory region (mr) nodes helps ensure that the output remains easily parseable by other programs. This also provides better compatibility with existing tools and scripts.

If people really feed the output of HMP info mtree to parsers, we should probably provide the information via QMP.

Thank you for your helpful advice. I think the next step is to try implementing "info mtree" via QMP first, and then have it called by HMP.

Quoted 7 lines
Quoted 1 line

I’ve added it to my to-do list, and I’ll try to implement it using QMP in the next phase.

First question before you actually do that: use cases for feeding the information to programs? You might have answers already; I'm not on top of prior conversations.

[...]

The main purpose of this patch is not to make the memory-region tree output easier for programs to parse.

Instead, it's to make the output clearer when there are many memory regions in the machine.

Right now, we use spaces for indentation, which makes it hard to see the hierarchy clearly — I want similar to how the tree command works.

During discussions with multiple threads, we also looked into more details, like avoiding the use of special characters.

#6 lore raw
In reply to Markus Armbruster
Quoted 21 lines · Markus Armbruster
Quoted 19 lines · Chao Liu
Quoted 17 lines · Markus Armbruster
Quoted 4 lines · Chao Liu

From: Chao Liu <[email protected]>

Hi, all:

Quoted 8 lines

After several rounds of discussion, I think that adding a -t option to the info mtree command, which enables the display of tree-like node characters (e.g., +--, |--), is a better approach.

As BALATON Zoltan pointed out, retaining space-based indentation for displaying memory region (mr) nodes helps ensure that the output remains easily parseable by other programs. This also provides better compatibility with existing tools and scripts.

If people really feed the output of HMP info mtree to parsers, we should probably provide the information via QMP.

Quoted 7 lines
Quoted 3 lines

Thank you for your helpful advice. I think the next step is to try implementing "info mtree" via QMP first, and then have it called by HMP.

I’ve added it to my to-do list, and I’ll try to implement it using QMP in the next phase.

First question before you actually do that: use cases for feeding the information to programs? You might have answers already; I'm not on top of prior conversations.

My request was to not make the output much wider than it is now as that would result in broken lines and less readable output. The comment was not about parsing output but keeping the result fit in not too wide terminals as 64 bit addresses makes it wide already. Thus replacing spaces with tree chars is OK with me as long as no new vertical space is added. The first version of patch increased vertical space. Using ASCII chars was request from somebody else but that makes the output look less nice so I'm not sure it worth the change with that. Maybe leaving current output then adding a separate tree mode with the original line drawing chars could satisfy all preferences.

Regards,
BALATON Zoltan