~jderose/ubuntu/raring/qemu/vde-again

« back to all changes in this revision

Viewing changes to monitor.c

  • Committer: Bazaar Package Importer
  • Author(s): Riku Voipio, Josh Triplett, Riku Voipio
  • Date: 2009-07-29 13:28:05 UTC
  • mfrom: (1.4.1 upstream)
  • mto: (12.1.1 sid) (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20090729132805-cau7rfexh7dawyb8
Tags: 0.10.50+git20090729-1
[ Josh Triplett ]
* Remove myself from Uploaders.

[ Riku Voipio ]
* new upstream RC version
* nuke all linux-user patches (applied upstream)
  06_exit_segfault
  12_signal_powerpc_support
  21_net_soopts
  30_syscall_ipc
  32_syscall_sysctl
  35_syscall_sockaddr
  48_signal_terminate
  55_unmux_socketcall
* nuke all other applied-upstream patches
  01_nostrip (better version upstream)
  07_i386_exec_name (can be reintroduced in debian/rules)
  50_linuxbios_isa_bios_ram (shouldn't be needed anymore)
  51_linuxbios_piix_ram_size (applied)
  56_dhcp (crap)
  60_ppc_ld (reintroduce if needed)
  64_ppc_asm_constraints (ditto)
  66_tls_ld.patch (ditto)
  81_compile_dtb.patch (applied upstream)
  82_qemu-img_decimal (ditto)
* move to git
* simplify build rules
* Correct my email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
22
 * THE SOFTWARE.
23
23
 */
 
24
#include <dirent.h>
24
25
#include "hw/hw.h"
 
26
#include "hw/qdev.h"
25
27
#include "hw/usb.h"
26
28
#include "hw/pcmcia.h"
27
29
#include "hw/pc.h"
28
30
#include "hw/pci.h"
 
31
#include "hw/watchdog.h"
29
32
#include "gdbstub.h"
30
33
#include "net.h"
31
34
#include "qemu-char.h"
32
35
#include "sysemu.h"
 
36
#include "monitor.h"
 
37
#include "readline.h"
33
38
#include "console.h"
34
39
#include "block.h"
35
40
#include "audio/audio.h"
36
41
#include "disas.h"
37
42
#include "balloon.h"
38
 
#include <dirent.h>
39
43
#include "qemu-timer.h"
40
44
#include "migration.h"
41
45
#include "kvm.h"
 
46
#include "acl.h"
42
47
 
43
48
//#define DEBUG
44
49
//#define DEBUG_COMPLETION
57
62
 *
58
63
 */
59
64
 
60
 
typedef struct term_cmd_t {
 
65
typedef struct mon_cmd_t {
61
66
    const char *name;
62
67
    const char *args_type;
63
68
    void *handler;
64
69
    const char *params;
65
70
    const char *help;
66
 
} term_cmd_t;
67
 
 
68
 
#define MAX_MON 4
69
 
static CharDriverState *monitor_hd[MAX_MON];
70
 
static int hide_banner;
71
 
 
72
 
static const term_cmd_t term_cmds[];
73
 
static const term_cmd_t info_cmds[];
74
 
 
75
 
static uint8_t term_outbuf[1024];
76
 
static int term_outbuf_index;
77
 
 
78
 
static void monitor_start_input(void);
79
 
static void monitor_readline(const char *prompt, int is_password,
80
 
                             char *buf, int buf_size);
81
 
 
82
 
static CPUState *mon_cpu = NULL;
83
 
 
84
 
void term_flush(void)
85
 
{
86
 
    int i;
87
 
    if (term_outbuf_index > 0) {
88
 
        for (i = 0; i < MAX_MON; i++)
89
 
            if (monitor_hd[i] && monitor_hd[i]->focus == 0)
90
 
                qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
91
 
        term_outbuf_index = 0;
 
71
} mon_cmd_t;
 
72
 
 
73
/* file descriptors passed via SCM_RIGHTS */
 
74
typedef struct mon_fd_t mon_fd_t;
 
75
struct mon_fd_t {
 
76
    char *name;
 
77
    int fd;
 
78
    LIST_ENTRY(mon_fd_t) next;
 
79
};
 
80
 
 
81
struct Monitor {
 
82
    CharDriverState *chr;
 
83
    int flags;
 
84
    int suspend_cnt;
 
85
    uint8_t outbuf[1024];
 
86
    int outbuf_index;
 
87
    ReadLineState *rs;
 
88
    CPUState *mon_cpu;
 
89
    BlockDriverCompletionFunc *password_completion_cb;
 
90
    void *password_opaque;
 
91
    LIST_HEAD(,mon_fd_t) fds;
 
92
    LIST_ENTRY(Monitor) entry;
 
93
};
 
94
 
 
95
static LIST_HEAD(mon_list, Monitor) mon_list;
 
96
 
 
97
static const mon_cmd_t mon_cmds[];
 
98
static const mon_cmd_t info_cmds[];
 
99
 
 
100
Monitor *cur_mon = NULL;
 
101
 
 
102
static void monitor_command_cb(Monitor *mon, const char *cmdline,
 
103
                               void *opaque);
 
104
 
 
105
static void monitor_read_command(Monitor *mon, int show_prompt)
 
106
{
 
107
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
 
108
    if (show_prompt)
 
109
        readline_show_prompt(mon->rs);
 
110
}
 
111
 
 
112
static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
 
113
                                 void *opaque)
 
114
{
 
115
    if (mon->rs) {
 
116
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
 
117
        /* prompt is printed on return from the command handler */
 
118
        return 0;
 
119
    } else {
 
120
        monitor_printf(mon, "terminal does not support password prompting\n");
 
121
        return -ENOTTY;
 
122
    }
 
123
}
 
124
 
 
125
void monitor_flush(Monitor *mon)
 
126
{
 
127
    if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
 
128
        qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
 
129
        mon->outbuf_index = 0;
92
130
    }
93
131
}
94
132
 
95
133
/* flush at every end of line or if the buffer is full */
96
 
void term_puts(const char *str)
 
134
static void monitor_puts(Monitor *mon, const char *str)
97
135
{
98
136
    char c;
 
137
 
 
138
    if (!mon)
 
139
        return;
 
140
 
99
141
    for(;;) {
100
142
        c = *str++;
101
143
        if (c == '\0')
102
144
            break;
103
145
        if (c == '\n')
104
 
            term_outbuf[term_outbuf_index++] = '\r';
105
 
        term_outbuf[term_outbuf_index++] = c;
106
 
        if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
107
 
            c == '\n')
108
 
            term_flush();
 
146
            mon->outbuf[mon->outbuf_index++] = '\r';
 
147
        mon->outbuf[mon->outbuf_index++] = c;
 
148
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
 
149
            || c == '\n')
 
150
            monitor_flush(mon);
109
151
    }
110
152
}
111
153
 
112
 
void term_vprintf(const char *fmt, va_list ap)
 
154
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
113
155
{
114
156
    char buf[4096];
115
157
    vsnprintf(buf, sizeof(buf), fmt, ap);
116
 
    term_puts(buf);
 
158
    monitor_puts(mon, buf);
117
159
}
118
160
 
119
 
void term_printf(const char *fmt, ...)
 
161
void monitor_printf(Monitor *mon, const char *fmt, ...)
120
162
{
121
163
    va_list ap;
122
164
    va_start(ap, fmt);
123
 
    term_vprintf(fmt, ap);
 
165
    monitor_vprintf(mon, fmt, ap);
124
166
    va_end(ap);
125
167
}
126
168
 
127
 
void term_print_filename(const char *filename)
 
169
void monitor_print_filename(Monitor *mon, const char *filename)
128
170
{
129
171
    int i;
130
172
 
131
173
    for (i = 0; filename[i]; i++) {
132
 
        switch (filename[i]) {
133
 
        case ' ':
134
 
        case '"':
135
 
        case '\\':
136
 
            term_printf("\\%c", filename[i]);
137
 
            break;
138
 
        case '\t':
139
 
            term_printf("\\t");
140
 
            break;
141
 
        case '\r':
142
 
            term_printf("\\r");
143
 
            break;
144
 
        case '\n':
145
 
            term_printf("\\n");
146
 
            break;
147
 
        default:
148
 
            term_printf("%c", filename[i]);
149
 
            break;
150
 
        }
 
174
        switch (filename[i]) {
 
175
        case ' ':
 
176
        case '"':
 
177
        case '\\':
 
178
            monitor_printf(mon, "\\%c", filename[i]);
 
179
            break;
 
180
        case '\t':
 
181
            monitor_printf(mon, "\\t");
 
182
            break;
 
183
        case '\r':
 
184
            monitor_printf(mon, "\\r");
 
185
            break;
 
186
        case '\n':
 
187
            monitor_printf(mon, "\\n");
 
188
            break;
 
189
        default:
 
190
            monitor_printf(mon, "%c", filename[i]);
 
191
            break;
 
192
        }
151
193
    }
152
194
}
153
195
 
155
197
{
156
198
    va_list ap;
157
199
    va_start(ap, fmt);
158
 
    term_vprintf(fmt, ap);
 
200
    monitor_vprintf((Monitor *)stream, fmt, ap);
159
201
    va_end(ap);
160
202
    return 0;
161
203
}
180
222
    return 0;
181
223
}
182
224
 
183
 
static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
 
225
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
 
226
                          const char *prefix, const char *name)
184
227
{
185
 
    const term_cmd_t *cmd;
 
228
    const mon_cmd_t *cmd;
186
229
 
187
230
    for(cmd = cmds; cmd->name != NULL; cmd++) {
188
231
        if (!name || !strcmp(name, cmd->name))
189
 
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
 
232
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
 
233
                           cmd->params, cmd->help);
190
234
    }
191
235
}
192
236
 
193
 
static void help_cmd(const char *name)
 
237
static void help_cmd(Monitor *mon, const char *name)
194
238
{
195
239
    if (name && !strcmp(name, "info")) {
196
 
        help_cmd1(info_cmds, "info ", NULL);
 
240
        help_cmd_dump(mon, info_cmds, "info ", NULL);
197
241
    } else {
198
 
        help_cmd1(term_cmds, "", name);
 
242
        help_cmd_dump(mon, mon_cmds, "", name);
199
243
        if (name && !strcmp(name, "log")) {
200
244
            const CPULogItem *item;
201
 
            term_printf("Log items (comma separated):\n");
202
 
            term_printf("%-10s %s\n", "none", "remove all logs");
 
245
            monitor_printf(mon, "Log items (comma separated):\n");
 
246
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
203
247
            for(item = cpu_log_items; item->mask != 0; item++) {
204
 
                term_printf("%-10s %s\n", item->name, item->help);
 
248
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
205
249
            }
206
250
        }
207
251
    }
208
252
}
209
253
 
210
 
static void do_help(const char *name)
211
 
{
212
 
    help_cmd(name);
213
 
}
214
 
 
215
 
static void do_commit(const char *device)
216
 
{
217
 
    int i, all_devices;
 
254
static void do_commit(Monitor *mon, const char *device)
 
255
{
 
256
    int all_devices;
 
257
    DriveInfo *dinfo;
218
258
 
219
259
    all_devices = !strcmp(device, "all");
220
 
    for (i = 0; i < nb_drives; i++) {
221
 
            if (all_devices ||
222
 
                !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
223
 
                bdrv_commit(drives_table[i].bdrv);
 
260
    TAILQ_FOREACH(dinfo, &drives, next) {
 
261
        if (!all_devices)
 
262
            if (!strcmp(bdrv_get_device_name(dinfo->bdrv), device))
 
263
                continue;
 
264
        bdrv_commit(dinfo->bdrv);
224
265
    }
225
266
}
226
267
 
227
 
static void do_info(const char *item)
 
268
static void do_info(Monitor *mon, const char *item)
228
269
{
229
 
    const term_cmd_t *cmd;
230
 
    void (*handler)(void);
 
270
    const mon_cmd_t *cmd;
 
271
    void (*handler)(Monitor *);
231
272
 
232
273
    if (!item)
233
274
        goto help;
236
277
            goto found;
237
278
    }
238
279
 help:
239
 
    help_cmd("info");
 
280
    help_cmd(mon, "info");
240
281
    return;
241
282
 found:
242
283
    handler = cmd->handler;
243
 
    handler();
 
284
    handler(mon);
244
285
}
245
286
 
246
 
static void do_info_version(void)
 
287
static void do_info_version(Monitor *mon)
247
288
{
248
 
  term_printf("%s\n", QEMU_VERSION);
 
289
    monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
249
290
}
250
291
 
251
 
static void do_info_name(void)
 
292
static void do_info_name(Monitor *mon)
252
293
{
253
294
    if (qemu_name)
254
 
        term_printf("%s\n", qemu_name);
 
295
        monitor_printf(mon, "%s\n", qemu_name);
255
296
}
256
297
 
257
298
#if defined(TARGET_I386)
258
 
static void do_info_hpet(void)
 
299
static void do_info_hpet(Monitor *mon)
259
300
{
260
 
    term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
 
301
    monitor_printf(mon, "HPET is %s by QEMU\n",
 
302
                   (no_hpet) ? "disabled" : "enabled");
261
303
}
262
304
#endif
263
305
 
264
 
static void do_info_uuid(void)
265
 
{
266
 
    term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
267
 
            qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
268
 
            qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
269
 
            qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
270
 
            qemu_uuid[15]);
271
 
}
272
 
 
273
 
static void do_info_block(void)
274
 
{
275
 
    bdrv_info();
276
 
}
277
 
 
278
 
static void do_info_blockstats(void)
279
 
{
280
 
    bdrv_info_stats();
 
306
static void do_info_uuid(Monitor *mon)
 
307
{
 
308
    monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
 
309
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
 
310
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
 
311
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
 
312
                   qemu_uuid[14], qemu_uuid[15]);
281
313
}
282
314
 
283
315
/* get the current CPU defined by the user */
287
319
 
288
320
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
289
321
        if (env->cpu_index == cpu_index) {
290
 
            mon_cpu = env;
 
322
            cur_mon->mon_cpu = env;
291
323
            return 0;
292
324
        }
293
325
    }
296
328
 
297
329
static CPUState *mon_get_cpu(void)
298
330
{
299
 
    if (!mon_cpu) {
 
331
    if (!cur_mon->mon_cpu) {
300
332
        mon_set_cpu(0);
301
333
    }
302
 
    return mon_cpu;
 
334
    cpu_synchronize_state(cur_mon->mon_cpu, 0);
 
335
    return cur_mon->mon_cpu;
303
336
}
304
337
 
305
 
static void do_info_registers(void)
 
338
static void do_info_registers(Monitor *mon)
306
339
{
307
340
    CPUState *env;
308
341
    env = mon_get_cpu();
309
342
    if (!env)
310
343
        return;
311
344
#ifdef TARGET_I386
312
 
    cpu_dump_state(env, NULL, monitor_fprintf,
 
345
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
313
346
                   X86_DUMP_FPU);
314
347
#else
315
 
    cpu_dump_state(env, NULL, monitor_fprintf,
 
348
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
316
349
                   0);
317
350
#endif
318
351
}
319
352
 
320
 
static void do_info_cpus(void)
 
353
static void do_info_cpus(Monitor *mon)
321
354
{
322
355
    CPUState *env;
323
356
 
325
358
    mon_get_cpu();
326
359
 
327
360
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
328
 
        term_printf("%c CPU #%d:",
329
 
                    (env == mon_cpu) ? '*' : ' ',
330
 
                    env->cpu_index);
 
361
        cpu_synchronize_state(env, 0);
 
362
        monitor_printf(mon, "%c CPU #%d:",
 
363
                       (env == mon->mon_cpu) ? '*' : ' ',
 
364
                       env->cpu_index);
331
365
#if defined(TARGET_I386)
332
 
        term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
 
366
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
 
367
                       env->eip + env->segs[R_CS].base);
333
368
#elif defined(TARGET_PPC)
334
 
        term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
 
369
        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
335
370
#elif defined(TARGET_SPARC)
336
 
        term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
 
371
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
 
372
                       env->pc, env->npc);
337
373
#elif defined(TARGET_MIPS)
338
 
        term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
 
374
        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
339
375
#endif
340
376
        if (env->halted)
341
 
            term_printf(" (halted)");
342
 
        term_printf("\n");
 
377
            monitor_printf(mon, " (halted)");
 
378
        monitor_printf(mon, "\n");
343
379
    }
344
380
}
345
381
 
346
 
static void do_cpu_set(int index)
 
382
static void do_cpu_set(Monitor *mon, int index)
347
383
{
348
384
    if (mon_set_cpu(index) < 0)
349
 
        term_printf("Invalid CPU index\n");
 
385
        monitor_printf(mon, "Invalid CPU index\n");
350
386
}
351
387
 
352
 
static void do_info_jit(void)
 
388
static void do_info_jit(Monitor *mon)
353
389
{
354
 
    dump_exec_info(NULL, monitor_fprintf);
 
390
    dump_exec_info((FILE *)mon, monitor_fprintf);
355
391
}
356
392
 
357
 
static void do_info_history (void)
 
393
static void do_info_history(Monitor *mon)
358
394
{
359
395
    int i;
360
396
    const char *str;
361
397
 
 
398
    if (!mon->rs)
 
399
        return;
362
400
    i = 0;
363
401
    for(;;) {
364
 
        str = readline_get_history(i);
 
402
        str = readline_get_history(mon->rs, i);
365
403
        if (!str)
366
404
            break;
367
 
        term_printf("%d: '%s'\n", i, str);
 
405
        monitor_printf(mon, "%d: '%s'\n", i, str);
368
406
        i++;
369
407
    }
370
408
}
371
409
 
372
410
#if defined(TARGET_PPC)
373
411
/* XXX: not implemented in other targets */
374
 
static void do_info_cpu_stats (void)
 
412
static void do_info_cpu_stats(Monitor *mon)
375
413
{
376
414
    CPUState *env;
377
415
 
378
416
    env = mon_get_cpu();
379
 
    cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
 
417
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
380
418
}
381
419
#endif
382
420
 
383
 
static void do_quit(void)
 
421
static void do_quit(Monitor *mon)
384
422
{
385
423
    exit(0);
386
424
}
387
425
 
388
 
static int eject_device(BlockDriverState *bs, int force)
 
426
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
389
427
{
390
428
    if (bdrv_is_inserted(bs)) {
391
429
        if (!force) {
392
430
            if (!bdrv_is_removable(bs)) {
393
 
                term_printf("device is not removable\n");
 
431
                monitor_printf(mon, "device is not removable\n");
394
432
                return -1;
395
433
            }
396
434
            if (bdrv_is_locked(bs)) {
397
 
                term_printf("device is locked\n");
 
435
                monitor_printf(mon, "device is locked\n");
398
436
                return -1;
399
437
            }
400
438
        }
403
441
    return 0;
404
442
}
405
443
 
406
 
static void do_eject(int force, const char *filename)
 
444
static void do_eject(Monitor *mon, int force, const char *filename)
407
445
{
408
446
    BlockDriverState *bs;
409
447
 
410
448
    bs = bdrv_find(filename);
411
449
    if (!bs) {
412
 
        term_printf("device not found\n");
 
450
        monitor_printf(mon, "device not found\n");
413
451
        return;
414
452
    }
415
 
    eject_device(bs, force);
 
453
    eject_device(mon, bs, force);
416
454
}
417
455
 
418
 
static void do_change_block(const char *device, const char *filename, const char *fmt)
 
456
static void do_change_block(Monitor *mon, const char *device,
 
457
                            const char *filename, const char *fmt)
419
458
{
420
459
    BlockDriverState *bs;
421
460
    BlockDriver *drv = NULL;
422
461
 
423
462
    bs = bdrv_find(device);
424
463
    if (!bs) {
425
 
        term_printf("device not found\n");
 
464
        monitor_printf(mon, "device not found\n");
426
465
        return;
427
466
    }
428
467
    if (fmt) {
429
468
        drv = bdrv_find_format(fmt);
430
469
        if (!drv) {
431
 
            term_printf("invalid format %s\n", fmt);
 
470
            monitor_printf(mon, "invalid format %s\n", fmt);
432
471
            return;
433
472
        }
434
473
    }
435
 
    if (eject_device(bs, 0) < 0)
 
474
    if (eject_device(mon, bs, 0) < 0)
436
475
        return;
437
476
    bdrv_open2(bs, filename, 0, drv);
438
 
    monitor_read_bdrv_key(bs);
439
 
}
440
 
 
441
 
static void do_change_vnc(const char *target, const char *arg)
 
477
    monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
 
478
}
 
479
 
 
480
static void change_vnc_password_cb(Monitor *mon, const char *password,
 
481
                                   void *opaque)
 
482
{
 
483
    if (vnc_display_password(NULL, password) < 0)
 
484
        monitor_printf(mon, "could not set VNC server password\n");
 
485
 
 
486
    monitor_read_command(mon, 1);
 
487
}
 
488
 
 
489
static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
442
490
{
443
491
    if (strcmp(target, "passwd") == 0 ||
444
 
        strcmp(target, "password") == 0) {
445
 
        char password[9];
446
 
        if (arg) {
447
 
            strncpy(password, arg, sizeof(password));
448
 
            password[sizeof(password) - 1] = '\0';
449
 
        } else
450
 
            monitor_readline("Password: ", 1, password, sizeof(password));
451
 
        if (vnc_display_password(NULL, password) < 0)
452
 
            term_printf("could not set VNC server password\n");
 
492
        strcmp(target, "password") == 0) {
 
493
        if (arg) {
 
494
            char password[9];
 
495
            strncpy(password, arg, sizeof(password));
 
496
            password[sizeof(password) - 1] = '\0';
 
497
            change_vnc_password_cb(mon, password, NULL);
 
498
        } else {
 
499
            monitor_read_password(mon, change_vnc_password_cb, NULL);
 
500
        }
453
501
    } else {
454
 
        if (vnc_display_open(NULL, target) < 0)
455
 
            term_printf("could not start VNC server on %s\n", target);
 
502
        if (vnc_display_open(NULL, target) < 0)
 
503
            monitor_printf(mon, "could not start VNC server on %s\n", target);
456
504
    }
457
505
}
458
506
 
459
 
static void do_change(const char *device, const char *target, const char *arg)
 
507
static void do_change(Monitor *mon, const char *device, const char *target,
 
508
                      const char *arg)
460
509
{
461
510
    if (strcmp(device, "vnc") == 0) {
462
 
        do_change_vnc(target, arg);
 
511
        do_change_vnc(mon, target, arg);
463
512
    } else {
464
 
        do_change_block(device, target, arg);
 
513
        do_change_block(mon, device, target, arg);
465
514
    }
466
515
}
467
516
 
468
 
static void do_screen_dump(const char *filename)
 
517
static void do_screen_dump(Monitor *mon, const char *filename)
469
518
{
470
519
    vga_hw_screen_dump(filename);
471
520
}
472
521
 
473
 
static void do_logfile(const char *filename)
 
522
static void do_logfile(Monitor *mon, const char *filename)
474
523
{
475
524
    cpu_set_log_filename(filename);
476
525
}
477
526
 
478
 
static void do_log(const char *items)
 
527
static void do_log(Monitor *mon, const char *items)
479
528
{
480
529
    int mask;
481
530
 
484
533
    } else {
485
534
        mask = cpu_str_to_log_mask(items);
486
535
        if (!mask) {
487
 
            help_cmd("log");
 
536
            help_cmd(mon, "log");
488
537
            return;
489
538
        }
490
539
    }
491
540
    cpu_set_log(mask);
492
541
}
493
542
 
494
 
static void do_stop(void)
 
543
static void do_singlestep(Monitor *mon, const char *option)
 
544
{
 
545
    if (!option || !strcmp(option, "on")) {
 
546
        singlestep = 1;
 
547
    } else if (!strcmp(option, "off")) {
 
548
        singlestep = 0;
 
549
    } else {
 
550
        monitor_printf(mon, "unexpected option %s\n", option);
 
551
    }
 
552
}
 
553
 
 
554
static void do_stop(Monitor *mon)
495
555
{
496
556
    vm_stop(EXCP_INTERRUPT);
497
557
}
498
558
 
499
 
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
500
 
{
501
 
    int *err = opaque;
502
 
 
503
 
    if (bdrv_key_required(bs))
504
 
        *err = monitor_read_bdrv_key(bs);
505
 
    else
506
 
        *err = 0;
507
 
}
508
 
 
509
 
static void do_cont(void)
510
 
{
511
 
    int err = 0;
512
 
 
513
 
    bdrv_iterate(encrypted_bdrv_it, &err);
 
559
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
 
560
 
 
561
struct bdrv_iterate_context {
 
562
    Monitor *mon;
 
563
    int err;
 
564
};
 
565
 
 
566
static void do_cont(Monitor *mon)
 
567
{
 
568
    struct bdrv_iterate_context context = { mon, 0 };
 
569
 
 
570
    bdrv_iterate(encrypted_bdrv_it, &context);
514
571
    /* only resume the vm if all keys are set and valid */
 
572
    if (!context.err)
 
573
        vm_start();
 
574
}
 
575
 
 
576
static void bdrv_key_cb(void *opaque, int err)
 
577
{
 
578
    Monitor *mon = opaque;
 
579
 
 
580
    /* another key was set successfully, retry to continue */
515
581
    if (!err)
516
 
        vm_start();
517
 
}
518
 
 
519
 
#ifdef CONFIG_GDBSTUB
520
 
static void do_gdbserver(const char *port)
521
 
{
522
 
    if (!port)
523
 
        port = DEFAULT_GDBSTUB_PORT;
524
 
    if (gdbserver_start(port) < 0) {
525
 
        qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
 
582
        do_cont(mon);
 
583
}
 
584
 
 
585
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
 
586
{
 
587
    struct bdrv_iterate_context *context = opaque;
 
588
 
 
589
    if (!context->err && bdrv_key_required(bs)) {
 
590
        context->err = -EBUSY;
 
591
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
 
592
                                    context->mon);
 
593
    }
 
594
}
 
595
 
 
596
static void do_gdbserver(Monitor *mon, const char *device)
 
597
{
 
598
    if (!device)
 
599
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
 
600
    if (gdbserver_start(device) < 0) {
 
601
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
 
602
                       device);
 
603
    } else if (strcmp(device, "none") == 0) {
 
604
        monitor_printf(mon, "Disabled gdbserver\n");
526
605
    } else {
527
 
        qemu_printf("Waiting gdb connection on port '%s'\n", port);
528
 
    }
529
 
}
530
 
#endif
531
 
 
532
 
static void term_printc(int c)
533
 
{
534
 
    term_printf("'");
 
606
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
 
607
                       device);
 
608
    }
 
609
}
 
610
 
 
611
static void do_watchdog_action(Monitor *mon, const char *action)
 
612
{
 
613
    if (select_watchdog_action(action) == -1) {
 
614
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
 
615
    }
 
616
}
 
617
 
 
618
static void monitor_printc(Monitor *mon, int c)
 
619
{
 
620
    monitor_printf(mon, "'");
535
621
    switch(c) {
536
622
    case '\'':
537
 
        term_printf("\\'");
 
623
        monitor_printf(mon, "\\'");
538
624
        break;
539
625
    case '\\':
540
 
        term_printf("\\\\");
 
626
        monitor_printf(mon, "\\\\");
541
627
        break;
542
628
    case '\n':
543
 
        term_printf("\\n");
 
629
        monitor_printf(mon, "\\n");
544
630
        break;
545
631
    case '\r':
546
 
        term_printf("\\r");
 
632
        monitor_printf(mon, "\\r");
547
633
        break;
548
634
    default:
549
635
        if (c >= 32 && c <= 126) {
550
 
            term_printf("%c", c);
 
636
            monitor_printf(mon, "%c", c);
551
637
        } else {
552
 
            term_printf("\\x%02x", c);
 
638
            monitor_printf(mon, "\\x%02x", c);
553
639
        }
554
640
        break;
555
641
    }
556
 
    term_printf("'");
 
642
    monitor_printf(mon, "'");
557
643
}
558
644
 
559
 
static void memory_dump(int count, int format, int wsize,
 
645
static void memory_dump(Monitor *mon, int count, int format, int wsize,
560
646
                        target_phys_addr_t addr, int is_physical)
561
647
{
562
648
    CPUState *env;
590
676
            }
591
677
        }
592
678
#endif
593
 
        monitor_disas(env, addr, count, is_physical, flags);
 
679
        monitor_disas(mon, env, addr, count, is_physical, flags);
594
680
        return;
595
681
    }
596
682
 
621
707
 
622
708
    while (len > 0) {
623
709
        if (is_physical)
624
 
            term_printf(TARGET_FMT_plx ":", addr);
 
710
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
625
711
        else
626
 
            term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
 
712
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
627
713
        l = len;
628
714
        if (l > line_size)
629
715
            l = line_size;
634
720
            if (!env)
635
721
                break;
636
722
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
637
 
                term_printf(" Cannot access memory\n");
 
723
                monitor_printf(mon, " Cannot access memory\n");
638
724
                break;
639
725
            }
640
726
        }
655
741
                v = ldq_raw(buf + i);
656
742
                break;
657
743
            }
658
 
            term_printf(" ");
 
744
            monitor_printf(mon, " ");
659
745
            switch(format) {
660
746
            case 'o':
661
 
                term_printf("%#*" PRIo64, max_digits, v);
 
747
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
662
748
                break;
663
749
            case 'x':
664
 
                term_printf("0x%0*" PRIx64, max_digits, v);
 
750
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
665
751
                break;
666
752
            case 'u':
667
 
                term_printf("%*" PRIu64, max_digits, v);
 
753
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
668
754
                break;
669
755
            case 'd':
670
 
                term_printf("%*" PRId64, max_digits, v);
 
756
                monitor_printf(mon, "%*" PRId64, max_digits, v);
671
757
                break;
672
758
            case 'c':
673
 
                term_printc(v);
 
759
                monitor_printc(mon, v);
674
760
                break;
675
761
            }
676
762
            i += wsize;
677
763
        }
678
 
        term_printf("\n");
 
764
        monitor_printf(mon, "\n");
679
765
        addr += l;
680
766
        len -= l;
681
767
    }
687
773
#define GET_TLONG(h, l) (l)
688
774
#endif
689
775
 
690
 
static void do_memory_dump(int count, int format, int size,
 
776
static void do_memory_dump(Monitor *mon, int count, int format, int size,
691
777
                           uint32_t addrh, uint32_t addrl)
692
778
{
693
779
    target_long addr = GET_TLONG(addrh, addrl);
694
 
    memory_dump(count, format, size, addr, 0);
 
780
    memory_dump(mon, count, format, size, addr, 0);
695
781
}
696
782
 
697
783
#if TARGET_PHYS_ADDR_BITS > 32
700
786
#define GET_TPHYSADDR(h, l) (l)
701
787
#endif
702
788
 
703
 
static void do_physical_memory_dump(int count, int format, int size,
704
 
                                    uint32_t addrh, uint32_t addrl)
 
789
static void do_physical_memory_dump(Monitor *mon, int count, int format,
 
790
                                    int size, uint32_t addrh, uint32_t addrl)
705
791
 
706
792
{
707
793
    target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
708
 
    memory_dump(count, format, size, addr, 1);
 
794
    memory_dump(mon, count, format, size, addr, 1);
709
795
}
710
796
 
711
 
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
 
797
static void do_print(Monitor *mon, int count, int format, int size,
 
798
                     unsigned int valh, unsigned int vall)
712
799
{
713
800
    target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
714
801
#if TARGET_PHYS_ADDR_BITS == 32
715
802
    switch(format) {
716
803
    case 'o':
717
 
        term_printf("%#o", val);
 
804
        monitor_printf(mon, "%#o", val);
718
805
        break;
719
806
    case 'x':
720
 
        term_printf("%#x", val);
 
807
        monitor_printf(mon, "%#x", val);
721
808
        break;
722
809
    case 'u':
723
 
        term_printf("%u", val);
 
810
        monitor_printf(mon, "%u", val);
724
811
        break;
725
812
    default:
726
813
    case 'd':
727
 
        term_printf("%d", val);
 
814
        monitor_printf(mon, "%d", val);
728
815
        break;
729
816
    case 'c':
730
 
        term_printc(val);
 
817
        monitor_printc(mon, val);
731
818
        break;
732
819
    }
733
820
#else
734
821
    switch(format) {
735
822
    case 'o':
736
 
        term_printf("%#" PRIo64, val);
 
823
        monitor_printf(mon, "%#" PRIo64, val);
737
824
        break;
738
825
    case 'x':
739
 
        term_printf("%#" PRIx64, val);
 
826
        monitor_printf(mon, "%#" PRIx64, val);
740
827
        break;
741
828
    case 'u':
742
 
        term_printf("%" PRIu64, val);
 
829
        monitor_printf(mon, "%" PRIu64, val);
743
830
        break;
744
831
    default:
745
832
    case 'd':
746
 
        term_printf("%" PRId64, val);
 
833
        monitor_printf(mon, "%" PRId64, val);
747
834
        break;
748
835
    case 'c':
749
 
        term_printc(val);
 
836
        monitor_printc(mon, val);
750
837
        break;
751
838
    }
752
839
#endif
753
 
    term_printf("\n");
 
840
    monitor_printf(mon, "\n");
754
841
}
755
842
 
756
 
static void do_memory_save(unsigned int valh, unsigned int vall,
 
843
static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
757
844
                           uint32_t size, const char *filename)
758
845
{
759
846
    FILE *f;
768
855
 
769
856
    f = fopen(filename, "wb");
770
857
    if (!f) {
771
 
        term_printf("could not open '%s'\n", filename);
 
858
        monitor_printf(mon, "could not open '%s'\n", filename);
772
859
        return;
773
860
    }
774
861
    while (size != 0) {
783
870
    fclose(f);
784
871
}
785
872
 
786
 
static void do_physical_memory_save(unsigned int valh, unsigned int vall,
787
 
                                    uint32_t size, const char *filename)
 
873
static void do_physical_memory_save(Monitor *mon, unsigned int valh,
 
874
                                    unsigned int vall, uint32_t size,
 
875
                                    const char *filename)
788
876
{
789
877
    FILE *f;
790
878
    uint32_t l;
793
881
 
794
882
    f = fopen(filename, "wb");
795
883
    if (!f) {
796
 
        term_printf("could not open '%s'\n", filename);
 
884
        monitor_printf(mon, "could not open '%s'\n", filename);
797
885
        return;
798
886
    }
799
887
    while (size != 0) {
809
897
    fclose(f);
810
898
}
811
899
 
812
 
static void do_sum(uint32_t start, uint32_t size)
 
900
static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
813
901
{
814
902
    uint32_t addr;
815
903
    uint8_t buf[1];
822
910
        sum = (sum >> 1) | (sum << 15);
823
911
        sum += buf[0];
824
912
    }
825
 
    term_printf("%05d\n", sum);
 
913
    monitor_printf(mon, "%05d\n", sum);
826
914
}
827
915
 
828
916
typedef struct {
1005
1093
    }
1006
1094
}
1007
1095
 
1008
 
static void do_sendkey(const char *string, int has_hold_time, int hold_time)
 
1096
static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
 
1097
                       int hold_time)
1009
1098
{
1010
1099
    char keyname_buf[16];
1011
1100
    char *separator;
1024
1113
        if (keyname_len > 0) {
1025
1114
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1026
1115
            if (keyname_len > sizeof(keyname_buf) - 1) {
1027
 
                term_printf("invalid key: '%s...'\n", keyname_buf);
 
1116
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1028
1117
                return;
1029
1118
            }
1030
1119
            if (i == MAX_KEYCODES) {
1031
 
                term_printf("too many keys\n");
 
1120
                monitor_printf(mon, "too many keys\n");
1032
1121
                return;
1033
1122
            }
1034
1123
            keyname_buf[keyname_len] = 0;
1035
1124
            keycode = get_keycode(keyname_buf);
1036
1125
            if (keycode < 0) {
1037
 
                term_printf("unknown key: '%s'\n", keyname_buf);
 
1126
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1038
1127
                return;
1039
1128
            }
1040
1129
            keycodes[i++] = keycode;
1058
1147
 
1059
1148
static int mouse_button_state;
1060
1149
 
1061
 
static void do_mouse_move(const char *dx_str, const char *dy_str,
 
1150
static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1062
1151
                          const char *dz_str)
1063
1152
{
1064
1153
    int dx, dy, dz;
1070
1159
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1071
1160
}
1072
1161
 
1073
 
static void do_mouse_button(int button_state)
 
1162
static void do_mouse_button(Monitor *mon, int button_state)
1074
1163
{
1075
1164
    mouse_button_state = button_state;
1076
1165
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1077
1166
}
1078
1167
 
1079
 
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
 
1168
static void do_ioport_read(Monitor *mon, int count, int format, int size,
 
1169
                           int addr, int has_index, int index)
1080
1170
{
1081
1171
    uint32_t val;
1082
1172
    int suffix;
1083
1173
 
1084
1174
    if (has_index) {
1085
 
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
 
1175
        cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1086
1176
        addr++;
1087
1177
    }
1088
1178
    addr &= 0xffff;
1102
1192
        suffix = 'l';
1103
1193
        break;
1104
1194
    }
1105
 
    term_printf("port%c[0x%04x] = %#0*x\n",
1106
 
                suffix, addr, size * 2, val);
 
1195
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
 
1196
                   suffix, addr, size * 2, val);
1107
1197
}
1108
1198
 
1109
 
/* boot_set handler */
1110
 
static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1111
 
static void *boot_opaque;
1112
 
 
1113
 
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
 
1199
static void do_ioport_write(Monitor *mon, int count, int format, int size,
 
1200
                            int addr, int val)
1114
1201
{
1115
 
    qemu_boot_set_handler = func;
1116
 
    boot_opaque = opaque;
 
1202
    addr &= IOPORTS_MASK;
 
1203
 
 
1204
    switch (size) {
 
1205
    default:
 
1206
    case 1:
 
1207
        cpu_outb(NULL, addr, val);
 
1208
        break;
 
1209
    case 2:
 
1210
        cpu_outw(NULL, addr, val);
 
1211
        break;
 
1212
    case 4:
 
1213
        cpu_outl(NULL, addr, val);
 
1214
        break;
 
1215
    }
1117
1216
}
1118
1217
 
1119
 
static void do_boot_set(const char *bootdevice)
 
1218
static void do_boot_set(Monitor *mon, const char *bootdevice)
1120
1219
{
1121
1220
    int res;
1122
1221
 
1123
 
    if (qemu_boot_set_handler)  {
1124
 
        res = qemu_boot_set_handler(boot_opaque, bootdevice);
1125
 
        if (res == 0)
1126
 
            term_printf("boot device list now set to %s\n", bootdevice);
1127
 
        else
1128
 
            term_printf("setting boot device list failed with error %i\n", res);
 
1222
    res = qemu_boot_set(bootdevice);
 
1223
    if (res == 0) {
 
1224
        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
 
1225
    } else if (res > 0) {
 
1226
        monitor_printf(mon, "setting boot device list failed\n");
1129
1227
    } else {
1130
 
        term_printf("no function defined to set boot device list for this architecture\n");
 
1228
        monitor_printf(mon, "no function defined to set boot device list for "
 
1229
                       "this architecture\n");
1131
1230
    }
1132
1231
}
1133
1232
 
1134
 
static void do_system_reset(void)
 
1233
static void do_system_reset(Monitor *mon)
1135
1234
{
1136
1235
    qemu_system_reset_request();
1137
1236
}
1138
1237
 
1139
 
static void do_system_powerdown(void)
 
1238
static void do_system_powerdown(Monitor *mon)
1140
1239
{
1141
1240
    qemu_system_powerdown_request();
1142
1241
}
1143
1242
 
1144
1243
#if defined(TARGET_I386)
1145
 
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
 
1244
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1146
1245
{
1147
 
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1148
 
                addr,
1149
 
                pte & mask,
1150
 
                pte & PG_GLOBAL_MASK ? 'G' : '-',
1151
 
                pte & PG_PSE_MASK ? 'P' : '-',
1152
 
                pte & PG_DIRTY_MASK ? 'D' : '-',
1153
 
                pte & PG_ACCESSED_MASK ? 'A' : '-',
1154
 
                pte & PG_PCD_MASK ? 'C' : '-',
1155
 
                pte & PG_PWT_MASK ? 'T' : '-',
1156
 
                pte & PG_USER_MASK ? 'U' : '-',
1157
 
                pte & PG_RW_MASK ? 'W' : '-');
 
1246
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
 
1247
                   addr,
 
1248
                   pte & mask,
 
1249
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
 
1250
                   pte & PG_PSE_MASK ? 'P' : '-',
 
1251
                   pte & PG_DIRTY_MASK ? 'D' : '-',
 
1252
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
 
1253
                   pte & PG_PCD_MASK ? 'C' : '-',
 
1254
                   pte & PG_PWT_MASK ? 'T' : '-',
 
1255
                   pte & PG_USER_MASK ? 'U' : '-',
 
1256
                   pte & PG_RW_MASK ? 'W' : '-');
1158
1257
}
1159
1258
 
1160
 
static void tlb_info(void)
 
1259
static void tlb_info(Monitor *mon)
1161
1260
{
1162
1261
    CPUState *env;
1163
1262
    int l1, l2;
1168
1267
        return;
1169
1268
 
1170
1269
    if (!(env->cr[0] & CR0_PG_MASK)) {
1171
 
        term_printf("PG disabled\n");
 
1270
        monitor_printf(mon, "PG disabled\n");
1172
1271
        return;
1173
1272
    }
1174
1273
    pgd = env->cr[3] & ~0xfff;
1177
1276
        pde = le32_to_cpu(pde);
1178
1277
        if (pde & PG_PRESENT_MASK) {
1179
1278
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1180
 
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
 
1279
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1181
1280
            } else {
1182
1281
                for(l2 = 0; l2 < 1024; l2++) {
1183
1282
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1184
1283
                                             (uint8_t *)&pte, 4);
1185
1284
                    pte = le32_to_cpu(pte);
1186
1285
                    if (pte & PG_PRESENT_MASK) {
1187
 
                        print_pte((l1 << 22) + (l2 << 12),
 
1286
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1188
1287
                                  pte & ~PG_PSE_MASK,
1189
1288
                                  ~0xfff);
1190
1289
                    }
1194
1293
    }
1195
1294
}
1196
1295
 
1197
 
static void mem_print(uint32_t *pstart, int *plast_prot,
 
1296
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1198
1297
                      uint32_t end, int prot)
1199
1298
{
1200
1299
    int prot1;
1201
1300
    prot1 = *plast_prot;
1202
1301
    if (prot != prot1) {
1203
1302
        if (*pstart != -1) {
1204
 
            term_printf("%08x-%08x %08x %c%c%c\n",
1205
 
                        *pstart, end, end - *pstart,
1206
 
                        prot1 & PG_USER_MASK ? 'u' : '-',
1207
 
                        'r',
1208
 
                        prot1 & PG_RW_MASK ? 'w' : '-');
 
1303
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
 
1304
                           *pstart, end, end - *pstart,
 
1305
                           prot1 & PG_USER_MASK ? 'u' : '-',
 
1306
                           'r',
 
1307
                           prot1 & PG_RW_MASK ? 'w' : '-');
1209
1308
        }
1210
1309
        if (prot != 0)
1211
1310
            *pstart = end;
1215
1314
    }
1216
1315
}
1217
1316
 
1218
 
static void mem_info(void)
 
1317
static void mem_info(Monitor *mon)
1219
1318
{
1220
1319
    CPUState *env;
1221
1320
    int l1, l2, prot, last_prot;
1226
1325
        return;
1227
1326
 
1228
1327
    if (!(env->cr[0] & CR0_PG_MASK)) {
1229
 
        term_printf("PG disabled\n");
 
1328
        monitor_printf(mon, "PG disabled\n");
1230
1329
        return;
1231
1330
    }
1232
1331
    pgd = env->cr[3] & ~0xfff;
1239
1338
        if (pde & PG_PRESENT_MASK) {
1240
1339
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1241
1340
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1242
 
                mem_print(&start, &last_prot, end, prot);
 
1341
                mem_print(mon, &start, &last_prot, end, prot);
1243
1342
            } else {
1244
1343
                for(l2 = 0; l2 < 1024; l2++) {
1245
1344
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1251
1350
                    } else {
1252
1351
                        prot = 0;
1253
1352
                    }
1254
 
                    mem_print(&start, &last_prot, end, prot);
 
1353
                    mem_print(mon, &start, &last_prot, end, prot);
1255
1354
                }
1256
1355
            }
1257
1356
        } else {
1258
1357
            prot = 0;
1259
 
            mem_print(&start, &last_prot, end, prot);
 
1358
            mem_print(mon, &start, &last_prot, end, prot);
1260
1359
        }
1261
1360
    }
1262
1361
}
1264
1363
 
1265
1364
#if defined(TARGET_SH4)
1266
1365
 
1267
 
static void print_tlb(int idx, tlb_t *tlb)
 
1366
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1268
1367
{
1269
 
    term_printf(" tlb%i:\t"
1270
 
                "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1271
 
                "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1272
 
                "dirty=%hhu writethrough=%hhu\n",
1273
 
                idx,
1274
 
                tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1275
 
                tlb->v, tlb->sh, tlb->c, tlb->pr,
1276
 
                tlb->d, tlb->wt);
 
1368
    monitor_printf(mon, " tlb%i:\t"
 
1369
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
 
1370
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
 
1371
                   "dirty=%hhu writethrough=%hhu\n",
 
1372
                   idx,
 
1373
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
 
1374
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
 
1375
                   tlb->d, tlb->wt);
1277
1376
}
1278
1377
 
1279
 
static void tlb_info(void)
 
1378
static void tlb_info(Monitor *mon)
1280
1379
{
1281
1380
    CPUState *env = mon_get_cpu();
1282
1381
    int i;
1283
1382
 
1284
 
    term_printf ("ITLB:\n");
 
1383
    monitor_printf (mon, "ITLB:\n");
1285
1384
    for (i = 0 ; i < ITLB_SIZE ; i++)
1286
 
        print_tlb (i, &env->itlb[i]);
1287
 
    term_printf ("UTLB:\n");
 
1385
        print_tlb (mon, i, &env->itlb[i]);
 
1386
    monitor_printf (mon, "UTLB:\n");
1288
1387
    for (i = 0 ; i < UTLB_SIZE ; i++)
1289
 
        print_tlb (i, &env->utlb[i]);
 
1388
        print_tlb (mon, i, &env->utlb[i]);
1290
1389
}
1291
1390
 
1292
1391
#endif
1293
1392
 
1294
 
static void do_info_kqemu(void)
 
1393
static void do_info_kqemu(Monitor *mon)
1295
1394
{
1296
 
#ifdef USE_KQEMU
 
1395
#ifdef CONFIG_KQEMU
1297
1396
    CPUState *env;
1298
1397
    int val;
1299
1398
    val = 0;
1300
1399
    env = mon_get_cpu();
1301
1400
    if (!env) {
1302
 
        term_printf("No cpu initialized yet");
 
1401
        monitor_printf(mon, "No cpu initialized yet");
1303
1402
        return;
1304
1403
    }
1305
1404
    val = env->kqemu_enabled;
1306
 
    term_printf("kqemu support: ");
 
1405
    monitor_printf(mon, "kqemu support: ");
1307
1406
    switch(val) {
1308
1407
    default:
1309
1408
    case 0:
1310
 
        term_printf("disabled\n");
 
1409
        monitor_printf(mon, "disabled\n");
1311
1410
        break;
1312
1411
    case 1:
1313
 
        term_printf("enabled for user code\n");
 
1412
        monitor_printf(mon, "enabled for user code\n");
1314
1413
        break;
1315
1414
    case 2:
1316
 
        term_printf("enabled for user and kernel code\n");
 
1415
        monitor_printf(mon, "enabled for user and kernel code\n");
1317
1416
        break;
1318
1417
    }
1319
1418
#else
1320
 
    term_printf("kqemu support: not compiled\n");
 
1419
    monitor_printf(mon, "kqemu support: not compiled\n");
1321
1420
#endif
1322
1421
}
1323
1422
 
1324
 
static void do_info_kvm(void)
 
1423
static void do_info_kvm(Monitor *mon)
1325
1424
{
1326
1425
#ifdef CONFIG_KVM
1327
 
    term_printf("kvm support: ");
 
1426
    monitor_printf(mon, "kvm support: ");
1328
1427
    if (kvm_enabled())
1329
 
        term_printf("enabled\n");
 
1428
        monitor_printf(mon, "enabled\n");
1330
1429
    else
1331
 
        term_printf("disabled\n");
 
1430
        monitor_printf(mon, "disabled\n");
1332
1431
#else
1333
 
    term_printf("kvm support: not compiled\n");
 
1432
    monitor_printf(mon, "kvm support: not compiled\n");
1334
1433
#endif
1335
1434
}
1336
1435
 
 
1436
static void do_info_numa(Monitor *mon)
 
1437
{
 
1438
    int i;
 
1439
    CPUState *env;
 
1440
 
 
1441
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
 
1442
    for (i = 0; i < nb_numa_nodes; i++) {
 
1443
        monitor_printf(mon, "node %d cpus:", i);
 
1444
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
 
1445
            if (env->numa_node == i) {
 
1446
                monitor_printf(mon, " %d", env->cpu_index);
 
1447
            }
 
1448
        }
 
1449
        monitor_printf(mon, "\n");
 
1450
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
 
1451
            node_mem[i] >> 20);
 
1452
    }
 
1453
}
 
1454
 
1337
1455
#ifdef CONFIG_PROFILER
1338
1456
 
1339
1457
int64_t kqemu_time;
1344
1462
int64_t kqemu_ret_excp_count;
1345
1463
int64_t kqemu_ret_intr_count;
1346
1464
 
1347
 
static void do_info_profile(void)
 
1465
static void do_info_profile(Monitor *mon)
1348
1466
{
1349
1467
    int64_t total;
1350
1468
    total = qemu_time;
1351
1469
    if (total == 0)
1352
1470
        total = 1;
1353
 
    term_printf("async time  %" PRId64 " (%0.3f)\n",
1354
 
                dev_time, dev_time / (double)ticks_per_sec);
1355
 
    term_printf("qemu time   %" PRId64 " (%0.3f)\n",
1356
 
                qemu_time, qemu_time / (double)ticks_per_sec);
1357
 
    term_printf("kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1358
 
                kqemu_time, kqemu_time / (double)ticks_per_sec,
1359
 
                kqemu_time / (double)total * 100.0,
1360
 
                kqemu_exec_count,
1361
 
                kqemu_ret_int_count,
1362
 
                kqemu_ret_excp_count,
1363
 
                kqemu_ret_intr_count);
 
1471
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
 
1472
                   dev_time, dev_time / (double)ticks_per_sec);
 
1473
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
 
1474
                   qemu_time, qemu_time / (double)ticks_per_sec);
 
1475
    monitor_printf(mon, "kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%"
 
1476
                        PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
 
1477
                        PRId64 "\n",
 
1478
                   kqemu_time, kqemu_time / (double)ticks_per_sec,
 
1479
                   kqemu_time / (double)total * 100.0,
 
1480
                   kqemu_exec_count,
 
1481
                   kqemu_ret_int_count,
 
1482
                   kqemu_ret_excp_count,
 
1483
                   kqemu_ret_intr_count);
1364
1484
    qemu_time = 0;
1365
1485
    kqemu_time = 0;
1366
1486
    kqemu_exec_count = 0;
1368
1488
    kqemu_ret_int_count = 0;
1369
1489
    kqemu_ret_excp_count = 0;
1370
1490
    kqemu_ret_intr_count = 0;
1371
 
#ifdef USE_KQEMU
 
1491
#ifdef CONFIG_KQEMU
1372
1492
    kqemu_record_dump();
1373
1493
#endif
1374
1494
}
1375
1495
#else
1376
 
static void do_info_profile(void)
 
1496
static void do_info_profile(Monitor *mon)
1377
1497
{
1378
 
    term_printf("Internal profiler not compiled\n");
 
1498
    monitor_printf(mon, "Internal profiler not compiled\n");
1379
1499
}
1380
1500
#endif
1381
1501
 
1382
1502
/* Capture support */
1383
1503
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1384
1504
 
1385
 
static void do_info_capture (void)
 
1505
static void do_info_capture(Monitor *mon)
1386
1506
{
1387
1507
    int i;
1388
1508
    CaptureState *s;
1389
1509
 
1390
1510
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1391
 
        term_printf ("[%d]: ", i);
 
1511
        monitor_printf(mon, "[%d]: ", i);
1392
1512
        s->ops.info (s->opaque);
1393
1513
    }
1394
1514
}
1395
1515
 
1396
 
static void do_stop_capture (int n)
 
1516
#ifdef HAS_AUDIO
 
1517
static void do_stop_capture(Monitor *mon, int n)
1397
1518
{
1398
1519
    int i;
1399
1520
    CaptureState *s;
1408
1529
    }
1409
1530
}
1410
1531
 
1411
 
#ifdef HAS_AUDIO
1412
 
static void do_wav_capture (const char *path,
1413
 
                            int has_freq, int freq,
1414
 
                            int has_bits, int bits,
1415
 
                            int has_channels, int nchannels)
 
1532
static void do_wav_capture(Monitor *mon, const char *path,
 
1533
                           int has_freq, int freq,
 
1534
                           int has_bits, int bits,
 
1535
                           int has_channels, int nchannels)
1416
1536
{
1417
1537
    CaptureState *s;
1418
1538
 
1423
1543
    nchannels = has_channels ? nchannels : 2;
1424
1544
 
1425
1545
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1426
 
        term_printf ("Faied to add wave capture\n");
 
1546
        monitor_printf(mon, "Faied to add wave capture\n");
1427
1547
        qemu_free (s);
1428
1548
    }
1429
1549
    LIST_INSERT_HEAD (&capture_head, s, entries);
1431
1551
#endif
1432
1552
 
1433
1553
#if defined(TARGET_I386)
1434
 
static void do_inject_nmi(int cpu_index)
 
1554
static void do_inject_nmi(Monitor *mon, int cpu_index)
1435
1555
{
1436
1556
    CPUState *env;
1437
1557
 
1443
1563
}
1444
1564
#endif
1445
1565
 
1446
 
static void do_info_status(void)
 
1566
static void do_info_status(Monitor *mon)
1447
1567
{
1448
 
    if (vm_running)
1449
 
       term_printf("VM status: running\n");
1450
 
    else
1451
 
       term_printf("VM status: paused\n");
 
1568
    if (vm_running) {
 
1569
        if (singlestep) {
 
1570
            monitor_printf(mon, "VM status: running (single step mode)\n");
 
1571
        } else {
 
1572
            monitor_printf(mon, "VM status: running\n");
 
1573
        }
 
1574
    } else
 
1575
       monitor_printf(mon, "VM status: paused\n");
1452
1576
}
1453
1577
 
1454
1578
 
1455
 
static void do_balloon(int value)
 
1579
static void do_balloon(Monitor *mon, int value)
1456
1580
{
1457
1581
    ram_addr_t target = value;
1458
1582
    qemu_balloon(target << 20);
1459
1583
}
1460
1584
 
1461
 
static void do_info_balloon(void)
 
1585
static void do_info_balloon(Monitor *mon)
1462
1586
{
1463
1587
    ram_addr_t actual;
1464
1588
 
1465
1589
    actual = qemu_balloon_status();
1466
1590
    if (kvm_enabled() && !kvm_has_sync_mmu())
1467
 
        term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
 
1591
        monitor_printf(mon, "Using KVM without synchronous MMU, "
 
1592
                       "ballooning disabled\n");
1468
1593
    else if (actual == 0)
1469
 
        term_printf("Ballooning not activated in VM\n");
 
1594
        monitor_printf(mon, "Ballooning not activated in VM\n");
1470
1595
    else
1471
 
        term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1472
 
}
1473
 
 
1474
 
/* Please update qemu-doc.texi when adding or changing commands */
1475
 
static const term_cmd_t term_cmds[] = {
1476
 
    { "help|?", "s?", do_help,
1477
 
      "[cmd]", "show the help" },
1478
 
    { "commit", "s", do_commit,
1479
 
      "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1480
 
    { "info", "s?", do_info,
1481
 
      "subcommand", "show various information about the system state" },
1482
 
    { "q|quit", "", do_quit,
1483
 
      "", "quit the emulator" },
1484
 
    { "eject", "-fB", do_eject,
1485
 
      "[-f] device", "eject a removable medium (use -f to force it)" },
1486
 
    { "change", "BFs?", do_change,
1487
 
      "device filename [format]", "change a removable medium, optional format" },
1488
 
    { "screendump", "F", do_screen_dump,
1489
 
      "filename", "save screen into PPM image 'filename'" },
1490
 
    { "logfile", "F", do_logfile,
1491
 
      "filename", "output logs to 'filename'" },
1492
 
    { "log", "s", do_log,
1493
 
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1494
 
    { "savevm", "s?", do_savevm,
1495
 
      "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1496
 
    { "loadvm", "s", do_loadvm,
1497
 
      "tag|id", "restore a VM snapshot from its tag or id" },
1498
 
    { "delvm", "s", do_delvm,
1499
 
      "tag|id", "delete a VM snapshot from its tag or id" },
1500
 
    { "stop", "", do_stop,
1501
 
      "", "stop emulation", },
1502
 
    { "c|cont", "", do_cont,
1503
 
      "", "resume emulation", },
1504
 
#ifdef CONFIG_GDBSTUB
1505
 
    { "gdbserver", "s?", do_gdbserver,
1506
 
      "[port]", "start gdbserver session (default port=1234)", },
1507
 
#endif
1508
 
    { "x", "/l", do_memory_dump,
1509
 
      "/fmt addr", "virtual memory dump starting at 'addr'", },
1510
 
    { "xp", "/l", do_physical_memory_dump,
1511
 
      "/fmt addr", "physical memory dump starting at 'addr'", },
1512
 
    { "p|print", "/l", do_print,
1513
 
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
1514
 
    { "i", "/ii.", do_ioport_read,
1515
 
      "/fmt addr", "I/O port read" },
1516
 
 
1517
 
    { "sendkey", "si?", do_sendkey,
1518
 
      "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1519
 
    { "system_reset", "", do_system_reset,
1520
 
      "", "reset the system" },
1521
 
    { "system_powerdown", "", do_system_powerdown,
1522
 
      "", "send system power down event" },
1523
 
    { "sum", "ii", do_sum,
1524
 
      "addr size", "compute the checksum of a memory region" },
1525
 
    { "usb_add", "s", do_usb_add,
1526
 
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1527
 
    { "usb_del", "s", do_usb_del,
1528
 
      "device", "remove USB device 'bus.addr'" },
1529
 
    { "cpu", "i", do_cpu_set,
1530
 
      "index", "set the default CPU" },
1531
 
    { "mouse_move", "sss?", do_mouse_move,
1532
 
      "dx dy [dz]", "send mouse move events" },
1533
 
    { "mouse_button", "i", do_mouse_button,
1534
 
      "state", "change mouse button state (1=L, 2=M, 4=R)" },
1535
 
    { "mouse_set", "i", do_mouse_set,
1536
 
      "index", "set which mouse device receives events" },
1537
 
#ifdef HAS_AUDIO
1538
 
    { "wavcapture", "si?i?i?", do_wav_capture,
1539
 
      "path [frequency bits channels]",
1540
 
      "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1541
 
#endif
1542
 
    { "stopcapture", "i", do_stop_capture,
1543
 
      "capture index", "stop capture" },
1544
 
    { "memsave", "lis", do_memory_save,
1545
 
      "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1546
 
    { "pmemsave", "lis", do_physical_memory_save,
1547
 
      "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1548
 
    { "boot_set", "s", do_boot_set,
1549
 
      "bootdevice", "define new values for the boot device list" },
1550
 
#if defined(TARGET_I386)
1551
 
    { "nmi", "i", do_inject_nmi,
1552
 
      "cpu", "inject an NMI on the given CPU", },
1553
 
#endif
1554
 
    { "migrate", "-ds", do_migrate,
1555
 
      "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1556
 
    { "migrate_cancel", "", do_migrate_cancel,
1557
 
      "", "cancel the current VM migration" },
1558
 
    { "migrate_set_speed", "s", do_migrate_set_speed,
1559
 
      "value", "set maximum speed (in bytes) for migrations" },
1560
 
#if defined(TARGET_I386)
1561
 
    { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1562
 
                                         "[file=file][,if=type][,bus=n]\n"
1563
 
                                        "[,unit=m][,media=d][index=i]\n"
1564
 
                                        "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1565
 
                                        "[snapshot=on|off][,cache=on|off]",
1566
 
                                        "add drive to PCI storage controller" },
1567
 
    { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1568
 
    { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1569
 
    { "host_net_add", "ss", net_host_device_add,
1570
 
      "[tap,user,socket,vde] options", "add host VLAN client" },
1571
 
    { "host_net_remove", "is", net_host_device_remove,
1572
 
      "vlan_id name", "remove host VLAN client" },
1573
 
#endif
1574
 
    { "balloon", "i", do_balloon,
1575
 
      "target", "request VM to change it's memory allocation (in MB)" },
1576
 
    { "set_link", "ss", do_set_link,
1577
 
      "name [up|down]", "change the link status of a network adapter" },
 
1596
        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
 
1597
}
 
1598
 
 
1599
static qemu_acl *find_acl(Monitor *mon, const char *name)
 
1600
{
 
1601
    qemu_acl *acl = qemu_acl_find(name);
 
1602
 
 
1603
    if (!acl) {
 
1604
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
 
1605
    }
 
1606
    return acl;
 
1607
}
 
1608
 
 
1609
static void do_acl_show(Monitor *mon, const char *aclname)
 
1610
{
 
1611
    qemu_acl *acl = find_acl(mon, aclname);
 
1612
    qemu_acl_entry *entry;
 
1613
    int i = 0;
 
1614
 
 
1615
    if (acl) {
 
1616
        monitor_printf(mon, "policy: %s\n",
 
1617
                       acl->defaultDeny ? "deny" : "allow");
 
1618
        TAILQ_FOREACH(entry, &acl->entries, next) {
 
1619
            i++;
 
1620
            monitor_printf(mon, "%d: %s %s\n", i,
 
1621
                           entry->deny ? "deny" : "allow", entry->match);
 
1622
        }
 
1623
    }
 
1624
}
 
1625
 
 
1626
static void do_acl_reset(Monitor *mon, const char *aclname)
 
1627
{
 
1628
    qemu_acl *acl = find_acl(mon, aclname);
 
1629
 
 
1630
    if (acl) {
 
1631
        qemu_acl_reset(acl);
 
1632
        monitor_printf(mon, "acl: removed all rules\n");
 
1633
    }
 
1634
}
 
1635
 
 
1636
static void do_acl_policy(Monitor *mon, const char *aclname,
 
1637
                          const char *policy)
 
1638
{
 
1639
    qemu_acl *acl = find_acl(mon, aclname);
 
1640
 
 
1641
    if (acl) {
 
1642
        if (strcmp(policy, "allow") == 0) {
 
1643
            acl->defaultDeny = 0;
 
1644
            monitor_printf(mon, "acl: policy set to 'allow'\n");
 
1645
        } else if (strcmp(policy, "deny") == 0) {
 
1646
            acl->defaultDeny = 1;
 
1647
            monitor_printf(mon, "acl: policy set to 'deny'\n");
 
1648
        } else {
 
1649
            monitor_printf(mon, "acl: unknown policy '%s', "
 
1650
                           "expected 'deny' or 'allow'\n", policy);
 
1651
        }
 
1652
    }
 
1653
}
 
1654
 
 
1655
static void do_acl_add(Monitor *mon, const char *aclname,
 
1656
                       const char *match, const char *policy,
 
1657
                       int has_index, int index)
 
1658
{
 
1659
    qemu_acl *acl = find_acl(mon, aclname);
 
1660
    int deny, ret;
 
1661
 
 
1662
    if (acl) {
 
1663
        if (strcmp(policy, "allow") == 0) {
 
1664
            deny = 0;
 
1665
        } else if (strcmp(policy, "deny") == 0) {
 
1666
            deny = 1;
 
1667
        } else {
 
1668
            monitor_printf(mon, "acl: unknown policy '%s', "
 
1669
                           "expected 'deny' or 'allow'\n", policy);
 
1670
            return;
 
1671
        }
 
1672
        if (has_index)
 
1673
            ret = qemu_acl_insert(acl, deny, match, index);
 
1674
        else
 
1675
            ret = qemu_acl_append(acl, deny, match);
 
1676
        if (ret < 0)
 
1677
            monitor_printf(mon, "acl: unable to add acl entry\n");
 
1678
        else
 
1679
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
 
1680
    }
 
1681
}
 
1682
 
 
1683
static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
 
1684
{
 
1685
    qemu_acl *acl = find_acl(mon, aclname);
 
1686
    int ret;
 
1687
 
 
1688
    if (acl) {
 
1689
        ret = qemu_acl_remove(acl, match);
 
1690
        if (ret < 0)
 
1691
            monitor_printf(mon, "acl: no matching acl entry\n");
 
1692
        else
 
1693
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
 
1694
    }
 
1695
}
 
1696
 
 
1697
#if defined(TARGET_I386)
 
1698
static void do_inject_mce(Monitor *mon,
 
1699
                          int cpu_index, int bank,
 
1700
                          unsigned status_hi, unsigned status_lo,
 
1701
                          unsigned mcg_status_hi, unsigned mcg_status_lo,
 
1702
                          unsigned addr_hi, unsigned addr_lo,
 
1703
                          unsigned misc_hi, unsigned misc_lo)
 
1704
{
 
1705
    CPUState *cenv;
 
1706
    uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
 
1707
    uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
 
1708
    uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
 
1709
    uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
 
1710
 
 
1711
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
 
1712
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
 
1713
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
 
1714
            break;
 
1715
        }
 
1716
}
 
1717
#endif
 
1718
 
 
1719
static void do_getfd(Monitor *mon, const char *fdname)
 
1720
{
 
1721
    mon_fd_t *monfd;
 
1722
    int fd;
 
1723
 
 
1724
    fd = qemu_chr_get_msgfd(mon->chr);
 
1725
    if (fd == -1) {
 
1726
        monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
 
1727
        return;
 
1728
    }
 
1729
 
 
1730
    if (qemu_isdigit(fdname[0])) {
 
1731
        monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
 
1732
        return;
 
1733
    }
 
1734
 
 
1735
    fd = dup(fd);
 
1736
    if (fd == -1) {
 
1737
        monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
 
1738
                       strerror(errno));
 
1739
        return;
 
1740
    }
 
1741
 
 
1742
    LIST_FOREACH(monfd, &mon->fds, next) {
 
1743
        if (strcmp(monfd->name, fdname) != 0) {
 
1744
            continue;
 
1745
        }
 
1746
 
 
1747
        close(monfd->fd);
 
1748
        monfd->fd = fd;
 
1749
        return;
 
1750
    }
 
1751
 
 
1752
    monfd = qemu_mallocz(sizeof(mon_fd_t));
 
1753
    monfd->name = qemu_strdup(fdname);
 
1754
    monfd->fd = fd;
 
1755
 
 
1756
    LIST_INSERT_HEAD(&mon->fds, monfd, next);
 
1757
}
 
1758
 
 
1759
static void do_closefd(Monitor *mon, const char *fdname)
 
1760
{
 
1761
    mon_fd_t *monfd;
 
1762
 
 
1763
    LIST_FOREACH(monfd, &mon->fds, next) {
 
1764
        if (strcmp(monfd->name, fdname) != 0) {
 
1765
            continue;
 
1766
        }
 
1767
 
 
1768
        LIST_REMOVE(monfd, next);
 
1769
        close(monfd->fd);
 
1770
        qemu_free(monfd->name);
 
1771
        qemu_free(monfd);
 
1772
        return;
 
1773
    }
 
1774
 
 
1775
    monitor_printf(mon, "Failed to find file descriptor named %s\n",
 
1776
                   fdname);
 
1777
}
 
1778
 
 
1779
int monitor_get_fd(Monitor *mon, const char *fdname)
 
1780
{
 
1781
    mon_fd_t *monfd;
 
1782
 
 
1783
    LIST_FOREACH(monfd, &mon->fds, next) {
 
1784
        int fd;
 
1785
 
 
1786
        if (strcmp(monfd->name, fdname) != 0) {
 
1787
            continue;
 
1788
        }
 
1789
 
 
1790
        fd = monfd->fd;
 
1791
 
 
1792
        /* caller takes ownership of fd */
 
1793
        LIST_REMOVE(monfd, next);
 
1794
        qemu_free(monfd->name);
 
1795
        qemu_free(monfd);
 
1796
 
 
1797
        return fd;
 
1798
    }
 
1799
 
 
1800
    return -1;
 
1801
}
 
1802
 
 
1803
static const mon_cmd_t mon_cmds[] = {
 
1804
#include "qemu-monitor.h"
1578
1805
    { NULL, NULL, },
1579
1806
};
1580
1807
 
1581
 
/* Please update qemu-doc.texi when adding or changing commands */
1582
 
static const term_cmd_t info_cmds[] = {
 
1808
/* Please update qemu-monitor.hx when adding or changing commands */
 
1809
static const mon_cmd_t info_cmds[] = {
1583
1810
    { "version", "", do_info_version,
1584
1811
      "", "show the version of QEMU" },
1585
1812
    { "network", "", do_info_network,
1586
1813
      "", "show the network state" },
1587
1814
    { "chardev", "", qemu_chr_info,
1588
1815
      "", "show the character devices" },
1589
 
    { "block", "", do_info_block,
 
1816
    { "block", "", bdrv_info,
1590
1817
      "", "show the block devices" },
1591
 
    { "blockstats", "", do_info_blockstats,
 
1818
    { "blockstats", "", bdrv_info_stats,
1592
1819
      "", "show block device statistics" },
1593
1820
    { "registers", "", do_info_registers,
1594
1821
      "", "show the cpu registers" },
1618
1845
      "", "show KQEMU information", },
1619
1846
    { "kvm", "", do_info_kvm,
1620
1847
      "", "show KVM information", },
 
1848
    { "numa", "", do_info_numa,
 
1849
      "", "show NUMA information", },
1621
1850
    { "usb", "", usb_info,
1622
1851
      "", "show guest USB devices", },
1623
1852
    { "usbhost", "", usb_host_info,
1645
1874
      "", "show CPU statistics", },
1646
1875
#endif
1647
1876
#if defined(CONFIG_SLIRP)
1648
 
    { "slirp", "", do_info_slirp,
1649
 
      "", "show SLIRP statistics", },
 
1877
    { "usernet", "", do_info_usernet,
 
1878
      "", "show user network stack connection states", },
1650
1879
#endif
1651
1880
    { "migrate", "", do_info_migrate, "", "show migration status" },
1652
1881
    { "balloon", "", do_info_balloon,
1653
1882
      "", "show balloon information" },
 
1883
    { "qtree", "", do_info_qtree,
 
1884
      "", "show device tree" },
1654
1885
    { NULL, NULL, },
1655
1886
};
1656
1887
 
1691
1922
 
1692
1923
    u = 0;
1693
1924
    for (i = 0; i < 8; i++)
1694
 
        u |= env->crf[i] << (32 - (4 * i));
 
1925
        u |= env->crf[i] << (32 - (4 * i));
1695
1926
 
1696
1927
    return u;
1697
1928
}
1998
2229
    { NULL },
1999
2230
};
2000
2231
 
2001
 
static void expr_error(const char *msg)
 
2232
static void expr_error(Monitor *mon, const char *msg)
2002
2233
{
2003
 
    term_printf("%s\n", msg);
 
2234
    monitor_printf(mon, "%s\n", msg);
2004
2235
    longjmp(expr_env, 1);
2005
2236
}
2006
2237
 
2046
2277
    }
2047
2278
}
2048
2279
 
2049
 
static int64_t expr_sum(void);
 
2280
static int64_t expr_sum(Monitor *mon);
2050
2281
 
2051
 
static int64_t expr_unary(void)
 
2282
static int64_t expr_unary(Monitor *mon)
2052
2283
{
2053
2284
    int64_t n;
2054
2285
    char *p;
2057
2288
    switch(*pch) {
2058
2289
    case '+':
2059
2290
        next();
2060
 
        n = expr_unary();
 
2291
        n = expr_unary(mon);
2061
2292
        break;
2062
2293
    case '-':
2063
2294
        next();
2064
 
        n = -expr_unary();
 
2295
        n = -expr_unary(mon);
2065
2296
        break;
2066
2297
    case '~':
2067
2298
        next();
2068
 
        n = ~expr_unary();
 
2299
        n = ~expr_unary(mon);
2069
2300
        break;
2070
2301
    case '(':
2071
2302
        next();
2072
 
        n = expr_sum();
 
2303
        n = expr_sum(mon);
2073
2304
        if (*pch != ')') {
2074
 
            expr_error("')' expected");
 
2305
            expr_error(mon, "')' expected");
2075
2306
        }
2076
2307
        next();
2077
2308
        break;
2078
2309
    case '\'':
2079
2310
        pch++;
2080
2311
        if (*pch == '\0')
2081
 
            expr_error("character constant expected");
 
2312
            expr_error(mon, "character constant expected");
2082
2313
        n = *pch;
2083
2314
        pch++;
2084
2315
        if (*pch != '\'')
2085
 
            expr_error("missing terminating \' character");
 
2316
            expr_error(mon, "missing terminating \' character");
2086
2317
        next();
2087
2318
        break;
2088
2319
    case '$':
2105
2336
            *q = 0;
2106
2337
            ret = get_monitor_def(&reg, buf);
2107
2338
            if (ret == -1)
2108
 
                expr_error("unknown register");
 
2339
                expr_error(mon, "unknown register");
2109
2340
            else if (ret == -2)
2110
 
                expr_error("no cpu defined");
 
2341
                expr_error(mon, "no cpu defined");
2111
2342
            n = reg;
2112
2343
        }
2113
2344
        break;
2114
2345
    case '\0':
2115
 
        expr_error("unexpected end of expression");
 
2346
        expr_error(mon, "unexpected end of expression");
2116
2347
        n = 0;
2117
2348
        break;
2118
2349
    default:
2122
2353
        n = strtoul(pch, &p, 0);
2123
2354
#endif
2124
2355
        if (pch == p) {
2125
 
            expr_error("invalid char in expression");
 
2356
            expr_error(mon, "invalid char in expression");
2126
2357
        }
2127
2358
        pch = p;
2128
2359
        while (qemu_isspace(*pch))
2133
2364
}
2134
2365
 
2135
2366
 
2136
 
static int64_t expr_prod(void)
 
2367
static int64_t expr_prod(Monitor *mon)
2137
2368
{
2138
2369
    int64_t val, val2;
2139
2370
    int op;
2140
2371
 
2141
 
    val = expr_unary();
 
2372
    val = expr_unary(mon);
2142
2373
    for(;;) {
2143
2374
        op = *pch;
2144
2375
        if (op != '*' && op != '/' && op != '%')
2145
2376
            break;
2146
2377
        next();
2147
 
        val2 = expr_unary();
 
2378
        val2 = expr_unary(mon);
2148
2379
        switch(op) {
2149
2380
        default:
2150
2381
        case '*':
2153
2384
        case '/':
2154
2385
        case '%':
2155
2386
            if (val2 == 0)
2156
 
                expr_error("division by zero");
 
2387
                expr_error(mon, "division by zero");
2157
2388
            if (op == '/')
2158
2389
                val /= val2;
2159
2390
            else
2164
2395
    return val;
2165
2396
}
2166
2397
 
2167
 
static int64_t expr_logic(void)
 
2398
static int64_t expr_logic(Monitor *mon)
2168
2399
{
2169
2400
    int64_t val, val2;
2170
2401
    int op;
2171
2402
 
2172
 
    val = expr_prod();
 
2403
    val = expr_prod(mon);
2173
2404
    for(;;) {
2174
2405
        op = *pch;
2175
2406
        if (op != '&' && op != '|' && op != '^')
2176
2407
            break;
2177
2408
        next();
2178
 
        val2 = expr_prod();
 
2409
        val2 = expr_prod(mon);
2179
2410
        switch(op) {
2180
2411
        default:
2181
2412
        case '&':
2192
2423
    return val;
2193
2424
}
2194
2425
 
2195
 
static int64_t expr_sum(void)
 
2426
static int64_t expr_sum(Monitor *mon)
2196
2427
{
2197
2428
    int64_t val, val2;
2198
2429
    int op;
2199
2430
 
2200
 
    val = expr_logic();
 
2431
    val = expr_logic(mon);
2201
2432
    for(;;) {
2202
2433
        op = *pch;
2203
2434
        if (op != '+' && op != '-')
2204
2435
            break;
2205
2436
        next();
2206
 
        val2 = expr_logic();
 
2437
        val2 = expr_logic(mon);
2207
2438
        if (op == '+')
2208
2439
            val += val2;
2209
2440
        else
2212
2443
    return val;
2213
2444
}
2214
2445
 
2215
 
static int get_expr(int64_t *pval, const char **pp)
 
2446
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2216
2447
{
2217
2448
    pch = *pp;
2218
2449
    if (setjmp(expr_env)) {
2221
2452
    }
2222
2453
    while (qemu_isspace(*pch))
2223
2454
        pch++;
2224
 
    *pval = expr_sum();
 
2455
    *pval = expr_sum(mon);
2225
2456
    *pp = pch;
2226
2457
    return 0;
2227
2458
}
2291
2522
    return 0;
2292
2523
}
2293
2524
 
 
2525
/*
 
2526
 * Store the command-name in cmdname, and return a pointer to
 
2527
 * the remaining of the command string.
 
2528
 */
 
2529
static const char *get_command_name(const char *cmdline,
 
2530
                                    char *cmdname, size_t nlen)
 
2531
{
 
2532
    size_t len;
 
2533
    const char *p, *pstart;
 
2534
 
 
2535
    p = cmdline;
 
2536
    while (qemu_isspace(*p))
 
2537
        p++;
 
2538
    if (*p == '\0')
 
2539
        return NULL;
 
2540
    pstart = p;
 
2541
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
 
2542
        p++;
 
2543
    len = p - pstart;
 
2544
    if (len > nlen - 1)
 
2545
        len = nlen - 1;
 
2546
    memcpy(cmdname, pstart, len);
 
2547
    cmdname[len] = '\0';
 
2548
    return p;
 
2549
}
 
2550
 
2294
2551
static int default_fmt_format = 'x';
2295
2552
static int default_fmt_size = 4;
2296
2553
 
2297
2554
#define MAX_ARGS 16
2298
2555
 
2299
 
static void monitor_handle_command(const char *cmdline)
 
2556
static void monitor_handle_command(Monitor *mon, const char *cmdline)
2300
2557
{
2301
 
    const char *p, *pstart, *typestr;
2302
 
    char *q;
2303
 
    int c, nb_args, len, i, has_arg;
2304
 
    const term_cmd_t *cmd;
 
2558
    const char *p, *typestr;
 
2559
    int c, nb_args, i, has_arg;
 
2560
    const mon_cmd_t *cmd;
2305
2561
    char cmdname[256];
2306
2562
    char buf[1024];
2307
2563
    void *str_allocated[MAX_ARGS];
2308
2564
    void *args[MAX_ARGS];
2309
 
    void (*handler_0)(void);
2310
 
    void (*handler_1)(void *arg0);
2311
 
    void (*handler_2)(void *arg0, void *arg1);
2312
 
    void (*handler_3)(void *arg0, void *arg1, void *arg2);
2313
 
    void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2314
 
    void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2315
 
                      void *arg4);
2316
 
    void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2317
 
                      void *arg4, void *arg5);
2318
 
    void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2319
 
                      void *arg4, void *arg5, void *arg6);
 
2565
    void (*handler_0)(Monitor *mon);
 
2566
    void (*handler_1)(Monitor *mon, void *arg0);
 
2567
    void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
 
2568
    void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
 
2569
    void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
 
2570
                      void *arg3);
 
2571
    void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
 
2572
                      void *arg3, void *arg4);
 
2573
    void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
 
2574
                      void *arg3, void *arg4, void *arg5);
 
2575
    void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
 
2576
                      void *arg3, void *arg4, void *arg5, void *arg6);
 
2577
    void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
 
2578
                      void *arg3, void *arg4, void *arg5, void *arg6,
 
2579
                      void *arg7);
 
2580
    void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
 
2581
                      void *arg3, void *arg4, void *arg5, void *arg6,
 
2582
                      void *arg7, void *arg8);
 
2583
    void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
 
2584
                       void *arg3, void *arg4, void *arg5, void *arg6,
 
2585
                       void *arg7, void *arg8, void *arg9);
2320
2586
 
2321
2587
#ifdef DEBUG
2322
 
    term_printf("command='%s'\n", cmdline);
 
2588
    monitor_printf(mon, "command='%s'\n", cmdline);
2323
2589
#endif
2324
2590
 
2325
2591
    /* extract the command name */
2326
 
    p = cmdline;
2327
 
    q = cmdname;
2328
 
    while (qemu_isspace(*p))
2329
 
        p++;
2330
 
    if (*p == '\0')
 
2592
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
 
2593
    if (!p)
2331
2594
        return;
2332
 
    pstart = p;
2333
 
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2334
 
        p++;
2335
 
    len = p - pstart;
2336
 
    if (len > sizeof(cmdname) - 1)
2337
 
        len = sizeof(cmdname) - 1;
2338
 
    memcpy(cmdname, pstart, len);
2339
 
    cmdname[len] = '\0';
2340
2595
 
2341
2596
    /* find the command */
2342
 
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
 
2597
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2343
2598
        if (compare_cmd(cmdname, cmd->name))
2344
 
            goto found;
2345
 
    }
2346
 
    term_printf("unknown command: '%s'\n", cmdname);
2347
 
    return;
2348
 
 found:
 
2599
            break;
 
2600
    }
 
2601
 
 
2602
    if (cmd->name == NULL) {
 
2603
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
 
2604
        return;
 
2605
    }
2349
2606
 
2350
2607
    for(i = 0; i < MAX_ARGS; i++)
2351
2608
        str_allocated[i] = NULL;
2380
2637
                if (ret < 0) {
2381
2638
                    switch(c) {
2382
2639
                    case 'F':
2383
 
                        term_printf("%s: filename expected\n", cmdname);
 
2640
                        monitor_printf(mon, "%s: filename expected\n",
 
2641
                                       cmdname);
2384
2642
                        break;
2385
2643
                    case 'B':
2386
 
                        term_printf("%s: block device name expected\n", cmdname);
 
2644
                        monitor_printf(mon, "%s: block device name expected\n",
 
2645
                                       cmdname);
2387
2646
                        break;
2388
2647
                    default:
2389
 
                        term_printf("%s: string expected\n", cmdname);
 
2648
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2390
2649
                        break;
2391
2650
                    }
2392
2651
                    goto fail;
2397
2656
            add_str:
2398
2657
                if (nb_args >= MAX_ARGS) {
2399
2658
                error_args:
2400
 
                    term_printf("%s: too many arguments\n", cmdname);
 
2659
                    monitor_printf(mon, "%s: too many arguments\n", cmdname);
2401
2660
                    goto fail;
2402
2661
                }
2403
2662
                args[nb_args++] = str;
2455
2714
                    }
2456
2715
                next:
2457
2716
                    if (*p != '\0' && !qemu_isspace(*p)) {
2458
 
                        term_printf("invalid char in format: '%c'\n", *p);
 
2717
                        monitor_printf(mon, "invalid char in format: '%c'\n",
 
2718
                                       *p);
2459
2719
                        goto fail;
2460
2720
                    }
2461
2721
                    if (format < 0)
2517
2777
                        goto add_num;
2518
2778
                    }
2519
2779
                }
2520
 
                if (get_expr(&val, &p))
 
2780
                if (get_expr(mon, &val, &p))
2521
2781
                    goto fail;
2522
2782
            add_num:
2523
2783
                if (c == 'i') {
2550
2810
                if (*p == '-') {
2551
2811
                    p++;
2552
2812
                    if (*p != c) {
2553
 
                        term_printf("%s: unsupported option -%c\n",
2554
 
                                    cmdname, *p);
 
2813
                        monitor_printf(mon, "%s: unsupported option -%c\n",
 
2814
                                       cmdname, *p);
2555
2815
                        goto fail;
2556
2816
                    }
2557
2817
                    p++;
2564
2824
            break;
2565
2825
        default:
2566
2826
        bad_type:
2567
 
            term_printf("%s: unknown type '%c'\n", cmdname, c);
 
2827
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2568
2828
            goto fail;
2569
2829
        }
2570
2830
    }
2572
2832
    while (qemu_isspace(*p))
2573
2833
        p++;
2574
2834
    if (*p != '\0') {
2575
 
        term_printf("%s: extraneous characters at the end of line\n",
2576
 
                    cmdname);
 
2835
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
 
2836
                       cmdname);
2577
2837
        goto fail;
2578
2838
    }
2579
2839
 
2580
2840
    switch(nb_args) {
2581
2841
    case 0:
2582
2842
        handler_0 = cmd->handler;
2583
 
        handler_0();
 
2843
        handler_0(mon);
2584
2844
        break;
2585
2845
    case 1:
2586
2846
        handler_1 = cmd->handler;
2587
 
        handler_1(args[0]);
 
2847
        handler_1(mon, args[0]);
2588
2848
        break;
2589
2849
    case 2:
2590
2850
        handler_2 = cmd->handler;
2591
 
        handler_2(args[0], args[1]);
 
2851
        handler_2(mon, args[0], args[1]);
2592
2852
        break;
2593
2853
    case 3:
2594
2854
        handler_3 = cmd->handler;
2595
 
        handler_3(args[0], args[1], args[2]);
 
2855
        handler_3(mon, args[0], args[1], args[2]);
2596
2856
        break;
2597
2857
    case 4:
2598
2858
        handler_4 = cmd->handler;
2599
 
        handler_4(args[0], args[1], args[2], args[3]);
 
2859
        handler_4(mon, args[0], args[1], args[2], args[3]);
2600
2860
        break;
2601
2861
    case 5:
2602
2862
        handler_5 = cmd->handler;
2603
 
        handler_5(args[0], args[1], args[2], args[3], args[4]);
 
2863
        handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2604
2864
        break;
2605
2865
    case 6:
2606
2866
        handler_6 = cmd->handler;
2607
 
        handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
 
2867
        handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2608
2868
        break;
2609
2869
    case 7:
2610
2870
        handler_7 = cmd->handler;
2611
 
        handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
 
2871
        handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
 
2872
                  args[6]);
 
2873
        break;
 
2874
    case 8:
 
2875
        handler_8 = cmd->handler;
 
2876
        handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
 
2877
                  args[6], args[7]);
 
2878
        break;
 
2879
    case 9:
 
2880
        handler_9 = cmd->handler;
 
2881
        handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
 
2882
                  args[6], args[7], args[8]);
 
2883
        break;
 
2884
    case 10:
 
2885
        handler_10 = cmd->handler;
 
2886
        handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
 
2887
                   args[6], args[7], args[8], args[9]);
2612
2888
        break;
2613
2889
    default:
2614
 
        term_printf("unsupported number of arguments: %d\n", nb_args);
 
2890
        monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2615
2891
        goto fail;
2616
2892
    }
2617
2893
 fail:
2618
2894
    for(i = 0; i < MAX_ARGS; i++)
2619
2895
        qemu_free(str_allocated[i]);
2620
 
    return;
2621
2896
}
2622
2897
 
2623
2898
static void cmd_completion(const char *name, const char *list)
2638
2913
        memcpy(cmd, pstart, len);
2639
2914
        cmd[len] = '\0';
2640
2915
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2641
 
            add_completion(cmd);
 
2916
            readline_add_completion(cur_mon->rs, cmd);
2642
2917
        }
2643
2918
        if (*p == '\0')
2644
2919
            break;
2669
2944
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2670
2945
    }
2671
2946
#ifdef DEBUG_COMPLETION
2672
 
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
 
2947
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
 
2948
                   input, path, file_prefix);
2673
2949
#endif
2674
2950
    ffs = opendir(path);
2675
2951
    if (!ffs)
2690
2966
            stat(file, &sb);
2691
2967
            if(S_ISDIR(sb.st_mode))
2692
2968
                pstrcat(file, sizeof(file), "/");
2693
 
            add_completion(file);
 
2969
            readline_add_completion(cur_mon->rs, file);
2694
2970
        }
2695
2971
    }
2696
2972
    closedir(ffs);
2703
2979
 
2704
2980
    if (input[0] == '\0' ||
2705
2981
        !strncmp(name, (char *)input, strlen(input))) {
2706
 
        add_completion(name);
 
2982
        readline_add_completion(cur_mon->rs, name);
2707
2983
    }
2708
2984
}
2709
2985
 
2733
3009
    *pnb_args = nb_args;
2734
3010
}
2735
3011
 
2736
 
void readline_find_completion(const char *cmdline)
 
3012
static void monitor_find_completion(const char *cmdline)
2737
3013
{
2738
3014
    const char *cmdname;
2739
3015
    char *args[MAX_ARGS];
2740
3016
    int nb_args, i, len;
2741
3017
    const char *ptype, *str;
2742
 
    const term_cmd_t *cmd;
 
3018
    const mon_cmd_t *cmd;
2743
3019
    const KeyDef *key;
2744
3020
 
2745
3021
    parse_cmdline(cmdline, &nb_args, args);
2746
3022
#ifdef DEBUG_COMPLETION
2747
3023
    for(i = 0; i < nb_args; i++) {
2748
 
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
 
3024
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2749
3025
    }
2750
3026
#endif
2751
3027
 
2763
3039
            cmdname = "";
2764
3040
        else
2765
3041
            cmdname = args[0];
2766
 
        completion_index = strlen(cmdname);
2767
 
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
 
3042
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
 
3043
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2768
3044
            cmd_completion(cmdname, cmd->name);
2769
3045
        }
2770
3046
    } else {
2771
3047
        /* find the command */
2772
 
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
 
3048
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2773
3049
            if (compare_cmd(args[0], cmd->name))
2774
3050
                goto found;
2775
3051
        }
2787
3063
        switch(*ptype) {
2788
3064
        case 'F':
2789
3065
            /* file completion */
2790
 
            completion_index = strlen(str);
 
3066
            readline_set_completion_index(cur_mon->rs, strlen(str));
2791
3067
            file_completion(str);
2792
3068
            break;
2793
3069
        case 'B':
2794
3070
            /* block device name completion */
2795
 
            completion_index = strlen(str);
 
3071
            readline_set_completion_index(cur_mon->rs, strlen(str));
2796
3072
            bdrv_iterate(block_completion_it, (void *)str);
2797
3073
            break;
2798
3074
        case 's':
2799
3075
            /* XXX: more generic ? */
2800
3076
            if (!strcmp(cmd->name, "info")) {
2801
 
                completion_index = strlen(str);
 
3077
                readline_set_completion_index(cur_mon->rs, strlen(str));
2802
3078
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2803
3079
                    cmd_completion(str, cmd->name);
2804
3080
                }
2805
3081
            } else if (!strcmp(cmd->name, "sendkey")) {
2806
 
                completion_index = strlen(str);
 
3082
                char *sep = strrchr(str, '-');
 
3083
                if (sep)
 
3084
                    str = sep + 1;
 
3085
                readline_set_completion_index(cur_mon->rs, strlen(str));
2807
3086
                for(key = key_defs; key->name != NULL; key++) {
2808
3087
                    cmd_completion(str, key->name);
2809
3088
                }
 
3089
            } else if (!strcmp(cmd->name, "help|?")) {
 
3090
                readline_set_completion_index(cur_mon->rs, strlen(str));
 
3091
                for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
 
3092
                    cmd_completion(str, cmd->name);
 
3093
                }
2810
3094
            }
2811
3095
            break;
2812
3096
        default:
2817
3101
        qemu_free(args[i]);
2818
3102
}
2819
3103
 
2820
 
static int term_can_read(void *opaque)
2821
 
{
2822
 
    return 128;
2823
 
}
2824
 
 
2825
 
static void term_read(void *opaque, const uint8_t *buf, int size)
2826
 
{
2827
 
    int i;
2828
 
    for(i = 0; i < size; i++)
2829
 
        readline_handle_byte(buf[i]);
2830
 
}
2831
 
 
2832
 
static int monitor_suspended;
2833
 
 
2834
 
static void monitor_handle_command1(void *opaque, const char *cmdline)
2835
 
{
2836
 
    monitor_handle_command(cmdline);
2837
 
    if (!monitor_suspended)
2838
 
        monitor_start_input();
2839
 
    else
2840
 
        monitor_suspended = 2;
2841
 
}
2842
 
 
2843
 
void monitor_suspend(void)
2844
 
{
2845
 
    monitor_suspended = 1;
2846
 
}
2847
 
 
2848
 
void monitor_resume(void)
2849
 
{
2850
 
    if (monitor_suspended == 2)
2851
 
        monitor_start_input();
2852
 
    monitor_suspended = 0;
2853
 
}
2854
 
 
2855
 
static void monitor_start_input(void)
2856
 
{
2857
 
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2858
 
}
2859
 
 
2860
 
static void term_event(void *opaque, int event)
2861
 
{
2862
 
    if (event != CHR_EVENT_RESET)
2863
 
        return;
2864
 
 
2865
 
    if (!hide_banner)
2866
 
            term_printf("QEMU %s monitor - type 'help' for more information\n",
2867
 
                        QEMU_VERSION);
2868
 
    monitor_start_input();
2869
 
}
2870
 
 
2871
 
static int is_first_init = 1;
2872
 
 
2873
 
void monitor_init(CharDriverState *hd, int show_banner)
2874
 
{
2875
 
    int i;
 
3104
static int monitor_can_read(void *opaque)
 
3105
{
 
3106
    Monitor *mon = opaque;
 
3107
 
 
3108
    return (mon->suspend_cnt == 0) ? 128 : 0;
 
3109
}
 
3110
 
 
3111
static void monitor_read(void *opaque, const uint8_t *buf, int size)
 
3112
{
 
3113
    Monitor *old_mon = cur_mon;
 
3114
    int i;
 
3115
 
 
3116
    cur_mon = opaque;
 
3117
 
 
3118
    if (cur_mon->rs) {
 
3119
        for (i = 0; i < size; i++)
 
3120
            readline_handle_byte(cur_mon->rs, buf[i]);
 
3121
    } else {
 
3122
        if (size == 0 || buf[size - 1] != 0)
 
3123
            monitor_printf(cur_mon, "corrupted command\n");
 
3124
        else
 
3125
            monitor_handle_command(cur_mon, (char *)buf);
 
3126
    }
 
3127
 
 
3128
    cur_mon = old_mon;
 
3129
}
 
3130
 
 
3131
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
 
3132
{
 
3133
    monitor_suspend(mon);
 
3134
    monitor_handle_command(mon, cmdline);
 
3135
    monitor_resume(mon);
 
3136
}
 
3137
 
 
3138
int monitor_suspend(Monitor *mon)
 
3139
{
 
3140
    if (!mon->rs)
 
3141
        return -ENOTTY;
 
3142
    mon->suspend_cnt++;
 
3143
    return 0;
 
3144
}
 
3145
 
 
3146
void monitor_resume(Monitor *mon)
 
3147
{
 
3148
    if (!mon->rs)
 
3149
        return;
 
3150
    if (--mon->suspend_cnt == 0)
 
3151
        readline_show_prompt(mon->rs);
 
3152
}
 
3153
 
 
3154
static void monitor_event(void *opaque, int event)
 
3155
{
 
3156
    Monitor *mon = opaque;
 
3157
 
 
3158
    switch (event) {
 
3159
    case CHR_EVENT_MUX_IN:
 
3160
        readline_restart(mon->rs);
 
3161
        monitor_resume(mon);
 
3162
        monitor_flush(mon);
 
3163
        break;
 
3164
 
 
3165
    case CHR_EVENT_MUX_OUT:
 
3166
        if (mon->suspend_cnt == 0)
 
3167
            monitor_printf(mon, "\n");
 
3168
        monitor_flush(mon);
 
3169
        monitor_suspend(mon);
 
3170
        break;
 
3171
 
 
3172
    case CHR_EVENT_RESET:
 
3173
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
 
3174
                       "information\n", QEMU_VERSION);
 
3175
        if (mon->chr->focus == 0)
 
3176
            readline_show_prompt(mon->rs);
 
3177
        break;
 
3178
    }
 
3179
}
 
3180
 
 
3181
 
 
3182
/*
 
3183
 * Local variables:
 
3184
 *  c-indent-level: 4
 
3185
 *  c-basic-offset: 4
 
3186
 *  tab-width: 8
 
3187
 * End:
 
3188
 */
 
3189
 
 
3190
void monitor_init(CharDriverState *chr, int flags)
 
3191
{
 
3192
    static int is_first_init = 1;
 
3193
    Monitor *mon;
2876
3194
 
2877
3195
    if (is_first_init) {
2878
3196
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2879
 
        if (!key_timer)
2880
 
            return;
2881
 
        for (i = 0; i < MAX_MON; i++) {
2882
 
            monitor_hd[i] = NULL;
2883
 
        }
2884
3197
        is_first_init = 0;
2885
3198
    }
2886
 
    for (i = 0; i < MAX_MON; i++) {
2887
 
        if (monitor_hd[i] == NULL) {
2888
 
            monitor_hd[i] = hd;
2889
 
            break;
2890
 
        }
2891
 
    }
2892
 
 
2893
 
    hide_banner = !show_banner;
2894
 
 
2895
 
    qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2896
 
 
2897
 
    readline_start("", 0, monitor_handle_command1, NULL);
2898
 
}
2899
 
 
2900
 
/* XXX: use threads ? */
2901
 
/* modal monitor readline */
2902
 
static int monitor_readline_started;
2903
 
static char *monitor_readline_buf;
2904
 
static int monitor_readline_buf_size;
2905
 
 
2906
 
static void monitor_readline_cb(void *opaque, const char *input)
2907
 
{
2908
 
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2909
 
    monitor_readline_started = 0;
2910
 
}
2911
 
 
2912
 
static void monitor_readline(const char *prompt, int is_password,
2913
 
                             char *buf, int buf_size)
2914
 
{
2915
 
    int i;
2916
 
    int old_focus[MAX_MON];
2917
 
 
2918
 
    if (is_password) {
2919
 
        for (i = 0; i < MAX_MON; i++) {
2920
 
            old_focus[i] = 0;
2921
 
            if (monitor_hd[i]) {
2922
 
                old_focus[i] = monitor_hd[i]->focus;
2923
 
                monitor_hd[i]->focus = 0;
2924
 
                qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2925
 
            }
2926
 
        }
2927
 
    }
2928
 
 
2929
 
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2930
 
    monitor_readline_buf = buf;
2931
 
    monitor_readline_buf_size = buf_size;
2932
 
    monitor_readline_started = 1;
2933
 
    while (monitor_readline_started) {
2934
 
        main_loop_wait(10);
2935
 
    }
2936
 
    /* restore original focus */
2937
 
    if (is_password) {
2938
 
        for (i = 0; i < MAX_MON; i++)
2939
 
            if (old_focus[i])
2940
 
                monitor_hd[i]->focus = old_focus[i];
2941
 
    }
2942
 
}
2943
 
 
2944
 
int monitor_read_bdrv_key(BlockDriverState *bs)
2945
 
{
2946
 
    char password[256];
2947
 
    int i;
2948
 
 
2949
 
    if (!bdrv_is_encrypted(bs))
2950
 
        return 0;
2951
 
 
2952
 
    term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2953
 
                bdrv_get_encrypted_filename(bs));
2954
 
    for(i = 0; i < 3; i++) {
2955
 
        monitor_readline("Password: ", 1, password, sizeof(password));
2956
 
        if (bdrv_set_key(bs, password) == 0)
2957
 
            return 0;
2958
 
        term_printf("invalid password\n");
2959
 
    }
2960
 
    return -EPERM;
 
3199
 
 
3200
    mon = qemu_mallocz(sizeof(*mon));
 
3201
 
 
3202
    mon->chr = chr;
 
3203
    mon->flags = flags;
 
3204
    if (mon->chr->focus != 0)
 
3205
        mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
 
3206
    if (flags & MONITOR_USE_READLINE) {
 
3207
        mon->rs = readline_init(mon, monitor_find_completion);
 
3208
        monitor_read_command(mon, 0);
 
3209
    }
 
3210
 
 
3211
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
 
3212
                          mon);
 
3213
 
 
3214
    LIST_INSERT_HEAD(&mon_list, mon, entry);
 
3215
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
 
3216
        cur_mon = mon;
 
3217
}
 
3218
 
 
3219
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
 
3220
{
 
3221
    BlockDriverState *bs = opaque;
 
3222
    int ret = 0;
 
3223
 
 
3224
    if (bdrv_set_key(bs, password) != 0) {
 
3225
        monitor_printf(mon, "invalid password\n");
 
3226
        ret = -EPERM;
 
3227
    }
 
3228
    if (mon->password_completion_cb)
 
3229
        mon->password_completion_cb(mon->password_opaque, ret);
 
3230
 
 
3231
    monitor_read_command(mon, 1);
 
3232
}
 
3233
 
 
3234
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
 
3235
                                 BlockDriverCompletionFunc *completion_cb,
 
3236
                                 void *opaque)
 
3237
{
 
3238
    int err;
 
3239
 
 
3240
    if (!bdrv_key_required(bs)) {
 
3241
        if (completion_cb)
 
3242
            completion_cb(opaque, 0);
 
3243
        return;
 
3244
    }
 
3245
 
 
3246
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
 
3247
                   bdrv_get_encrypted_filename(bs));
 
3248
 
 
3249
    mon->password_completion_cb = completion_cb;
 
3250
    mon->password_opaque = opaque;
 
3251
 
 
3252
    err = monitor_read_password(mon, bdrv_password_cb, bs);
 
3253
 
 
3254
    if (err && completion_cb)
 
3255
        completion_cb(opaque, err);
2961
3256
}