xref: /openbmc/qemu/scripts/qmp/qom-tree (revision 438c78da)
1#!/usr/bin/python
2##
3# QEMU Object Model test tools
4#
5# Copyright IBM, Corp. 2011
6# Copyright (c) 2013 SUSE LINUX Products GmbH
7#
8# Authors:
9#  Anthony Liguori   <aliguori@amazon.com>
10#  Andreas Faerber   <afaerber@suse.de>
11#
12# This work is licensed under the terms of the GNU GPL, version 2 or later.  See
13# the COPYING file in the top-level directory.
14##
15
16from __future__ import print_function
17import sys
18import os
19from qmp import QEMUMonitorProtocol
20
21cmd, args = sys.argv[0], sys.argv[1:]
22socket_path = None
23path = None
24prop = None
25
26def usage():
27    return '''environment variables:
28    QMP_SOCKET=<path | addr:port>
29usage:
30    %s [-h] [-s <QMP socket path | addr:port>] [<path>]
31''' % cmd
32
33def usage_error(error_msg = "unspecified error"):
34    sys.stderr.write('%s\nERROR: %s\n' % (usage(), error_msg))
35    exit(1)
36
37if len(args) > 0:
38    if args[0] == "-h":
39        print(usage())
40        exit(0);
41    elif args[0] == "-s":
42        try:
43            socket_path = args[1]
44        except:
45            usage_error("missing argument: QMP socket path or address");
46        args = args[2:]
47
48if not socket_path:
49    if 'QMP_SOCKET' in os.environ:
50        socket_path = os.environ['QMP_SOCKET']
51    else:
52        usage_error("no QMP socket path or address given");
53
54srv = QEMUMonitorProtocol(socket_path)
55srv.connect()
56
57def list_node(path):
58    print('%s' % path)
59    items = srv.command('qom-list', path=path)
60    for item in items:
61        if not item['type'].startswith('child<'):
62            try:
63                print('  %s: %s (%s)' % (item['name'], srv.command('qom-get', path=path, property=item['name']), item['type']))
64            except:
65                print('  %s: <EXCEPTION> (%s)' % (item['name'], item['type']))
66    print('')
67    for item in items:
68        if item['type'].startswith('child<'):
69            list_node((path if (path != '/') else '')  + '/' + item['name'])
70
71if len(args) == 0:
72    path = '/'
73else:
74    path = args[0]
75
76list_node(path)
77