xref: /openbmc/qemu/scripts/qmp/qom-tree (revision 65a117da)
1#!/usr/bin/env python3
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
16import sys
17import os
18
19sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
20from qemu.qmp import QEMUMonitorProtocol
21
22cmd, args = sys.argv[0], sys.argv[1:]
23socket_path = None
24path = None
25prop = None
26
27def usage():
28    return '''environment variables:
29    QMP_SOCKET=<path | addr:port>
30usage:
31    %s [-h] [-s <QMP socket path | addr:port>] [<path>]
32''' % cmd
33
34def usage_error(error_msg = "unspecified error"):
35    sys.stderr.write('%s\nERROR: %s\n' % (usage(), error_msg))
36    exit(1)
37
38if len(args) > 0:
39    if args[0] == "-h":
40        print(usage())
41        exit(0);
42    elif args[0] == "-s":
43        try:
44            socket_path = args[1]
45        except:
46            usage_error("missing argument: QMP socket path or address");
47        args = args[2:]
48
49if not socket_path:
50    if 'QMP_SOCKET' in os.environ:
51        socket_path = os.environ['QMP_SOCKET']
52    else:
53        usage_error("no QMP socket path or address given");
54
55srv = QEMUMonitorProtocol(socket_path)
56srv.connect()
57
58def list_node(path):
59    print('%s' % path)
60    items = srv.command('qom-list', path=path)
61    for item in items:
62        if not item['type'].startswith('child<'):
63            try:
64                print('  %s: %s (%s)' % (item['name'], srv.command('qom-get', path=path, property=item['name']), item['type']))
65            except:
66                print('  %s: <EXCEPTION> (%s)' % (item['name'], item['type']))
67    print('')
68    for item in items:
69        if item['type'].startswith('child<'):
70            list_node((path if (path != '/') else '')  + '/' + item['name'])
71
72if len(args) == 0:
73    path = '/'
74else:
75    path = args[0]
76
77list_node(path)
78