Statistics
| Branch: | Revision:

root / monitor.c @ f8bfb1dc

History | View | Annotate | Download (87.1 kB)

1
/*
2
 * QEMU monitor
3
 *
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include <dirent.h>
25
#include "hw/hw.h"
26
#include "hw/qdev.h"
27
#include "hw/usb.h"
28
#include "hw/pcmcia.h"
29
#include "hw/pc.h"
30
#include "hw/pci.h"
31
#include "hw/watchdog.h"
32
#include "gdbstub.h"
33
#include "net.h"
34
#include "qemu-char.h"
35
#include "sysemu.h"
36
#include "monitor.h"
37
#include "readline.h"
38
#include "console.h"
39
#include "block.h"
40
#include "audio/audio.h"
41
#include "disas.h"
42
#include "balloon.h"
43
#include "qemu-timer.h"
44
#include "migration.h"
45
#include "kvm.h"
46
#include "acl.h"
47

    
48
//#define DEBUG
49
//#define DEBUG_COMPLETION
50

    
51
/*
52
 * Supported types:
53
 *
54
 * 'F'          filename
55
 * 'B'          block device name
56
 * 's'          string (accept optional quote)
57
 * 'i'          32 bit integer
58
 * 'l'          target long (32 or 64 bit)
59
 * '/'          optional gdb-like print format (like "/10x")
60
 *
61
 * '?'          optional type (for 'F', 's' and 'i')
62
 *
63
 */
64

    
65
typedef struct mon_cmd_t {
66
    const char *name;
67
    const char *args_type;
68
    void *handler;
69
    const char *params;
70
    const char *help;
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;
130
    }
131
}
132

    
133
/* flush at every end of line or if the buffer is full */
134
static void monitor_puts(Monitor *mon, const char *str)
135
{
136
    char c;
137

    
138
    if (!mon)
139
        return;
140

    
141
    for(;;) {
142
        c = *str++;
143
        if (c == '\0')
144
            break;
145
        if (c == '\n')
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);
151
    }
152
}
153

    
154
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
155
{
156
    char buf[4096];
157
    vsnprintf(buf, sizeof(buf), fmt, ap);
158
    monitor_puts(mon, buf);
159
}
160

    
161
void monitor_printf(Monitor *mon, const char *fmt, ...)
162
{
163
    va_list ap;
164
    va_start(ap, fmt);
165
    monitor_vprintf(mon, fmt, ap);
166
    va_end(ap);
167
}
168

    
169
void monitor_print_filename(Monitor *mon, const char *filename)
170
{
171
    int i;
172

    
173
    for (i = 0; filename[i]; i++) {
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
        }
193
    }
194
}
195

    
196
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
197
{
198
    va_list ap;
199
    va_start(ap, fmt);
200
    monitor_vprintf((Monitor *)stream, fmt, ap);
201
    va_end(ap);
202
    return 0;
203
}
204

    
205
static int compare_cmd(const char *name, const char *list)
206
{
207
    const char *p, *pstart;
208
    int len;
209
    len = strlen(name);
210
    p = list;
211
    for(;;) {
212
        pstart = p;
213
        p = strchr(p, '|');
214
        if (!p)
215
            p = pstart + strlen(pstart);
216
        if ((p - pstart) == len && !memcmp(pstart, name, len))
217
            return 1;
218
        if (*p == '\0')
219
            break;
220
        p++;
221
    }
222
    return 0;
223
}
224

    
225
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
226
                          const char *prefix, const char *name)
227
{
228
    const mon_cmd_t *cmd;
229

    
230
    for(cmd = cmds; cmd->name != NULL; cmd++) {
231
        if (!name || !strcmp(name, cmd->name))
232
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
233
                           cmd->params, cmd->help);
234
    }
235
}
236

    
237
static void help_cmd(Monitor *mon, const char *name)
238
{
239
    if (name && !strcmp(name, "info")) {
240
        help_cmd_dump(mon, info_cmds, "info ", NULL);
241
    } else {
242
        help_cmd_dump(mon, mon_cmds, "", name);
243
        if (name && !strcmp(name, "log")) {
244
            const CPULogItem *item;
245
            monitor_printf(mon, "Log items (comma separated):\n");
246
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
247
            for(item = cpu_log_items; item->mask != 0; item++) {
248
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
249
            }
250
        }
251
    }
252
}
253

    
254
static void do_commit(Monitor *mon, const char *device)
255
{
256
    int all_devices;
257
    DriveInfo *dinfo;
258

    
259
    all_devices = !strcmp(device, "all");
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);
265
    }
266
}
267

    
268
static void do_info(Monitor *mon, const char *item)
269
{
270
    const mon_cmd_t *cmd;
271
    void (*handler)(Monitor *);
272

    
273
    if (!item)
274
        goto help;
275
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
276
        if (compare_cmd(item, cmd->name))
277
            goto found;
278
    }
279
 help:
280
    help_cmd(mon, "info");
281
    return;
282
 found:
283
    handler = cmd->handler;
284
    handler(mon);
285
}
286

    
287
static void do_info_version(Monitor *mon)
288
{
289
    monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
290
}
291

    
292
static void do_info_name(Monitor *mon)
293
{
294
    if (qemu_name)
295
        monitor_printf(mon, "%s\n", qemu_name);
296
}
297

    
298
#if defined(TARGET_I386)
299
static void do_info_hpet(Monitor *mon)
300
{
301
    monitor_printf(mon, "HPET is %s by QEMU\n",
302
                   (no_hpet) ? "disabled" : "enabled");
303
}
304
#endif
305

    
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]);
313
}
314

    
315
/* get the current CPU defined by the user */
316
static int mon_set_cpu(int cpu_index)
317
{
318
    CPUState *env;
319

    
320
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
321
        if (env->cpu_index == cpu_index) {
322
            cur_mon->mon_cpu = env;
323
            return 0;
324
        }
325
    }
326
    return -1;
327
}
328

    
329
static CPUState *mon_get_cpu(void)
330
{
331
    if (!cur_mon->mon_cpu) {
332
        mon_set_cpu(0);
333
    }
334
    cpu_synchronize_state(cur_mon->mon_cpu, 0);
335
    return cur_mon->mon_cpu;
336
}
337

    
338
static void do_info_registers(Monitor *mon)
339
{
340
    CPUState *env;
341
    env = mon_get_cpu();
342
    if (!env)
343
        return;
344
#ifdef TARGET_I386
345
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
346
                   X86_DUMP_FPU);
347
#else
348
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
349
                   0);
350
#endif
351
}
352

    
353
static void do_info_cpus(Monitor *mon)
354
{
355
    CPUState *env;
356

    
357
    /* just to set the default cpu if not already done */
358
    mon_get_cpu();
359

    
360
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
361
        cpu_synchronize_state(env, 0);
362
        monitor_printf(mon, "%c CPU #%d:",
363
                       (env == mon->mon_cpu) ? '*' : ' ',
364
                       env->cpu_index);
365
#if defined(TARGET_I386)
366
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
367
                       env->eip + env->segs[R_CS].base);
368
#elif defined(TARGET_PPC)
369
        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
370
#elif defined(TARGET_SPARC)
371
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
372
                       env->pc, env->npc);
373
#elif defined(TARGET_MIPS)
374
        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
375
#endif
376
        if (env->halted)
377
            monitor_printf(mon, " (halted)");
378
        monitor_printf(mon, "\n");
379
    }
380
}
381

    
382
static void do_cpu_set(Monitor *mon, int index)
383
{
384
    if (mon_set_cpu(index) < 0)
385
        monitor_printf(mon, "Invalid CPU index\n");
386
}
387

    
388
static void do_info_jit(Monitor *mon)
389
{
390
    dump_exec_info((FILE *)mon, monitor_fprintf);
391
}
392

    
393
static void do_info_history(Monitor *mon)
394
{
395
    int i;
396
    const char *str;
397

    
398
    if (!mon->rs)
399
        return;
400
    i = 0;
401
    for(;;) {
402
        str = readline_get_history(mon->rs, i);
403
        if (!str)
404
            break;
405
        monitor_printf(mon, "%d: '%s'\n", i, str);
406
        i++;
407
    }
408
}
409

    
410
#if defined(TARGET_PPC)
411
/* XXX: not implemented in other targets */
412
static void do_info_cpu_stats(Monitor *mon)
413
{
414
    CPUState *env;
415

    
416
    env = mon_get_cpu();
417
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
418
}
419
#endif
420

    
421
static void do_quit(Monitor *mon)
422
{
423
    exit(0);
424
}
425

    
426
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
427
{
428
    if (bdrv_is_inserted(bs)) {
429
        if (!force) {
430
            if (!bdrv_is_removable(bs)) {
431
                monitor_printf(mon, "device is not removable\n");
432
                return -1;
433
            }
434
            if (bdrv_is_locked(bs)) {
435
                monitor_printf(mon, "device is locked\n");
436
                return -1;
437
            }
438
        }
439
        bdrv_close(bs);
440
    }
441
    return 0;
442
}
443

    
444
static void do_eject(Monitor *mon, int force, const char *filename)
445
{
446
    BlockDriverState *bs;
447

    
448
    bs = bdrv_find(filename);
449
    if (!bs) {
450
        monitor_printf(mon, "device not found\n");
451
        return;
452
    }
453
    eject_device(mon, bs, force);
454
}
455

    
456
static void do_change_block(Monitor *mon, const char *device,
457
                            const char *filename, const char *fmt)
458
{
459
    BlockDriverState *bs;
460
    BlockDriver *drv = NULL;
461

    
462
    bs = bdrv_find(device);
463
    if (!bs) {
464
        monitor_printf(mon, "device not found\n");
465
        return;
466
    }
467
    if (fmt) {
468
        drv = bdrv_find_format(fmt);
469
        if (!drv) {
470
            monitor_printf(mon, "invalid format %s\n", fmt);
471
            return;
472
        }
473
    }
474
    if (eject_device(mon, bs, 0) < 0)
475
        return;
476
    bdrv_open2(bs, filename, 0, drv);
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)
490
{
491
    if (strcmp(target, "passwd") == 0 ||
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
        }
501
    } else {
502
        if (vnc_display_open(NULL, target) < 0)
503
            monitor_printf(mon, "could not start VNC server on %s\n", target);
504
    }
505
}
506

    
507
static void do_change(Monitor *mon, const char *device, const char *target,
508
                      const char *arg)
509
{
510
    if (strcmp(device, "vnc") == 0) {
511
        do_change_vnc(mon, target, arg);
512
    } else {
513
        do_change_block(mon, device, target, arg);
514
    }
515
}
516

    
517
static void do_screen_dump(Monitor *mon, const char *filename)
518
{
519
    vga_hw_screen_dump(filename);
520
}
521

    
522
static void do_logfile(Monitor *mon, const char *filename)
523
{
524
    cpu_set_log_filename(filename);
525
}
526

    
527
static void do_log(Monitor *mon, const char *items)
528
{
529
    int mask;
530

    
531
    if (!strcmp(items, "none")) {
532
        mask = 0;
533
    } else {
534
        mask = cpu_str_to_log_mask(items);
535
        if (!mask) {
536
            help_cmd(mon, "log");
537
            return;
538
        }
539
    }
540
    cpu_set_log(mask);
541
}
542

    
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)
555
{
556
    vm_stop(EXCP_INTERRUPT);
557
}
558

    
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);
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 */
581
    if (!err)
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");
605
    } else {
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, "'");
621
    switch(c) {
622
    case '\'':
623
        monitor_printf(mon, "\\'");
624
        break;
625
    case '\\':
626
        monitor_printf(mon, "\\\\");
627
        break;
628
    case '\n':
629
        monitor_printf(mon, "\\n");
630
        break;
631
    case '\r':
632
        monitor_printf(mon, "\\r");
633
        break;
634
    default:
635
        if (c >= 32 && c <= 126) {
636
            monitor_printf(mon, "%c", c);
637
        } else {
638
            monitor_printf(mon, "\\x%02x", c);
639
        }
640
        break;
641
    }
642
    monitor_printf(mon, "'");
643
}
644

    
645
static void memory_dump(Monitor *mon, int count, int format, int wsize,
646
                        target_phys_addr_t addr, int is_physical)
647
{
648
    CPUState *env;
649
    int nb_per_line, l, line_size, i, max_digits, len;
650
    uint8_t buf[16];
651
    uint64_t v;
652

    
653
    if (format == 'i') {
654
        int flags;
655
        flags = 0;
656
        env = mon_get_cpu();
657
        if (!env && !is_physical)
658
            return;
659
#ifdef TARGET_I386
660
        if (wsize == 2) {
661
            flags = 1;
662
        } else if (wsize == 4) {
663
            flags = 0;
664
        } else {
665
            /* as default we use the current CS size */
666
            flags = 0;
667
            if (env) {
668
#ifdef TARGET_X86_64
669
                if ((env->efer & MSR_EFER_LMA) &&
670
                    (env->segs[R_CS].flags & DESC_L_MASK))
671
                    flags = 2;
672
                else
673
#endif
674
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
675
                    flags = 1;
676
            }
677
        }
678
#endif
679
        monitor_disas(mon, env, addr, count, is_physical, flags);
680
        return;
681
    }
682

    
683
    len = wsize * count;
684
    if (wsize == 1)
685
        line_size = 8;
686
    else
687
        line_size = 16;
688
    nb_per_line = line_size / wsize;
689
    max_digits = 0;
690

    
691
    switch(format) {
692
    case 'o':
693
        max_digits = (wsize * 8 + 2) / 3;
694
        break;
695
    default:
696
    case 'x':
697
        max_digits = (wsize * 8) / 4;
698
        break;
699
    case 'u':
700
    case 'd':
701
        max_digits = (wsize * 8 * 10 + 32) / 33;
702
        break;
703
    case 'c':
704
        wsize = 1;
705
        break;
706
    }
707

    
708
    while (len > 0) {
709
        if (is_physical)
710
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
711
        else
712
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
713
        l = len;
714
        if (l > line_size)
715
            l = line_size;
716
        if (is_physical) {
717
            cpu_physical_memory_rw(addr, buf, l, 0);
718
        } else {
719
            env = mon_get_cpu();
720
            if (!env)
721
                break;
722
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
723
                monitor_printf(mon, " Cannot access memory\n");
724
                break;
725
            }
726
        }
727
        i = 0;
728
        while (i < l) {
729
            switch(wsize) {
730
            default:
731
            case 1:
732
                v = ldub_raw(buf + i);
733
                break;
734
            case 2:
735
                v = lduw_raw(buf + i);
736
                break;
737
            case 4:
738
                v = (uint32_t)ldl_raw(buf + i);
739
                break;
740
            case 8:
741
                v = ldq_raw(buf + i);
742
                break;
743
            }
744
            monitor_printf(mon, " ");
745
            switch(format) {
746
            case 'o':
747
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
748
                break;
749
            case 'x':
750
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
751
                break;
752
            case 'u':
753
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
754
                break;
755
            case 'd':
756
                monitor_printf(mon, "%*" PRId64, max_digits, v);
757
                break;
758
            case 'c':
759
                monitor_printc(mon, v);
760
                break;
761
            }
762
            i += wsize;
763
        }
764
        monitor_printf(mon, "\n");
765
        addr += l;
766
        len -= l;
767
    }
768
}
769

    
770
#if TARGET_LONG_BITS == 64
771
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
772
#else
773
#define GET_TLONG(h, l) (l)
774
#endif
775

    
776
static void do_memory_dump(Monitor *mon, int count, int format, int size,
777
                           uint32_t addrh, uint32_t addrl)
778
{
779
    target_long addr = GET_TLONG(addrh, addrl);
780
    memory_dump(mon, count, format, size, addr, 0);
781
}
782

    
783
#if TARGET_PHYS_ADDR_BITS > 32
784
#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
785
#else
786
#define GET_TPHYSADDR(h, l) (l)
787
#endif
788

    
789
static void do_physical_memory_dump(Monitor *mon, int count, int format,
790
                                    int size, uint32_t addrh, uint32_t addrl)
791

    
792
{
793
    target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
794
    memory_dump(mon, count, format, size, addr, 1);
795
}
796

    
797
static void do_print(Monitor *mon, int count, int format, int size,
798
                     unsigned int valh, unsigned int vall)
799
{
800
    target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
801
#if TARGET_PHYS_ADDR_BITS == 32
802
    switch(format) {
803
    case 'o':
804
        monitor_printf(mon, "%#o", val);
805
        break;
806
    case 'x':
807
        monitor_printf(mon, "%#x", val);
808
        break;
809
    case 'u':
810
        monitor_printf(mon, "%u", val);
811
        break;
812
    default:
813
    case 'd':
814
        monitor_printf(mon, "%d", val);
815
        break;
816
    case 'c':
817
        monitor_printc(mon, val);
818
        break;
819
    }
820
#else
821
    switch(format) {
822
    case 'o':
823
        monitor_printf(mon, "%#" PRIo64, val);
824
        break;
825
    case 'x':
826
        monitor_printf(mon, "%#" PRIx64, val);
827
        break;
828
    case 'u':
829
        monitor_printf(mon, "%" PRIu64, val);
830
        break;
831
    default:
832
    case 'd':
833
        monitor_printf(mon, "%" PRId64, val);
834
        break;
835
    case 'c':
836
        monitor_printc(mon, val);
837
        break;
838
    }
839
#endif
840
    monitor_printf(mon, "\n");
841
}
842

    
843
static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
844
                           uint32_t size, const char *filename)
845
{
846
    FILE *f;
847
    target_long addr = GET_TLONG(valh, vall);
848
    uint32_t l;
849
    CPUState *env;
850
    uint8_t buf[1024];
851

    
852
    env = mon_get_cpu();
853
    if (!env)
854
        return;
855

    
856
    f = fopen(filename, "wb");
857
    if (!f) {
858
        monitor_printf(mon, "could not open '%s'\n", filename);
859
        return;
860
    }
861
    while (size != 0) {
862
        l = sizeof(buf);
863
        if (l > size)
864
            l = size;
865
        cpu_memory_rw_debug(env, addr, buf, l, 0);
866
        fwrite(buf, 1, l, f);
867
        addr += l;
868
        size -= l;
869
    }
870
    fclose(f);
871
}
872

    
873
static void do_physical_memory_save(Monitor *mon, unsigned int valh,
874
                                    unsigned int vall, uint32_t size,
875
                                    const char *filename)
876
{
877
    FILE *f;
878
    uint32_t l;
879
    uint8_t buf[1024];
880
    target_phys_addr_t addr = GET_TPHYSADDR(valh, vall); 
881

    
882
    f = fopen(filename, "wb");
883
    if (!f) {
884
        monitor_printf(mon, "could not open '%s'\n", filename);
885
        return;
886
    }
887
    while (size != 0) {
888
        l = sizeof(buf);
889
        if (l > size)
890
            l = size;
891
        cpu_physical_memory_rw(addr, buf, l, 0);
892
        fwrite(buf, 1, l, f);
893
        fflush(f);
894
        addr += l;
895
        size -= l;
896
    }
897
    fclose(f);
898
}
899

    
900
static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
901
{
902
    uint32_t addr;
903
    uint8_t buf[1];
904
    uint16_t sum;
905

    
906
    sum = 0;
907
    for(addr = start; addr < (start + size); addr++) {
908
        cpu_physical_memory_rw(addr, buf, 1, 0);
909
        /* BSD sum algorithm ('sum' Unix command) */
910
        sum = (sum >> 1) | (sum << 15);
911
        sum += buf[0];
912
    }
913
    monitor_printf(mon, "%05d\n", sum);
914
}
915

    
916
typedef struct {
917
    int keycode;
918
    const char *name;
919
} KeyDef;
920

    
921
static const KeyDef key_defs[] = {
922
    { 0x2a, "shift" },
923
    { 0x36, "shift_r" },
924

    
925
    { 0x38, "alt" },
926
    { 0xb8, "alt_r" },
927
    { 0x64, "altgr" },
928
    { 0xe4, "altgr_r" },
929
    { 0x1d, "ctrl" },
930
    { 0x9d, "ctrl_r" },
931

    
932
    { 0xdd, "menu" },
933

    
934
    { 0x01, "esc" },
935

    
936
    { 0x02, "1" },
937
    { 0x03, "2" },
938
    { 0x04, "3" },
939
    { 0x05, "4" },
940
    { 0x06, "5" },
941
    { 0x07, "6" },
942
    { 0x08, "7" },
943
    { 0x09, "8" },
944
    { 0x0a, "9" },
945
    { 0x0b, "0" },
946
    { 0x0c, "minus" },
947
    { 0x0d, "equal" },
948
    { 0x0e, "backspace" },
949

    
950
    { 0x0f, "tab" },
951
    { 0x10, "q" },
952
    { 0x11, "w" },
953
    { 0x12, "e" },
954
    { 0x13, "r" },
955
    { 0x14, "t" },
956
    { 0x15, "y" },
957
    { 0x16, "u" },
958
    { 0x17, "i" },
959
    { 0x18, "o" },
960
    { 0x19, "p" },
961

    
962
    { 0x1c, "ret" },
963

    
964
    { 0x1e, "a" },
965
    { 0x1f, "s" },
966
    { 0x20, "d" },
967
    { 0x21, "f" },
968
    { 0x22, "g" },
969
    { 0x23, "h" },
970
    { 0x24, "j" },
971
    { 0x25, "k" },
972
    { 0x26, "l" },
973

    
974
    { 0x2c, "z" },
975
    { 0x2d, "x" },
976
    { 0x2e, "c" },
977
    { 0x2f, "v" },
978
    { 0x30, "b" },
979
    { 0x31, "n" },
980
    { 0x32, "m" },
981
    { 0x33, "comma" },
982
    { 0x34, "dot" },
983
    { 0x35, "slash" },
984

    
985
    { 0x37, "asterisk" },
986

    
987
    { 0x39, "spc" },
988
    { 0x3a, "caps_lock" },
989
    { 0x3b, "f1" },
990
    { 0x3c, "f2" },
991
    { 0x3d, "f3" },
992
    { 0x3e, "f4" },
993
    { 0x3f, "f5" },
994
    { 0x40, "f6" },
995
    { 0x41, "f7" },
996
    { 0x42, "f8" },
997
    { 0x43, "f9" },
998
    { 0x44, "f10" },
999
    { 0x45, "num_lock" },
1000
    { 0x46, "scroll_lock" },
1001

    
1002
    { 0xb5, "kp_divide" },
1003
    { 0x37, "kp_multiply" },
1004
    { 0x4a, "kp_subtract" },
1005
    { 0x4e, "kp_add" },
1006
    { 0x9c, "kp_enter" },
1007
    { 0x53, "kp_decimal" },
1008
    { 0x54, "sysrq" },
1009

    
1010
    { 0x52, "kp_0" },
1011
    { 0x4f, "kp_1" },
1012
    { 0x50, "kp_2" },
1013
    { 0x51, "kp_3" },
1014
    { 0x4b, "kp_4" },
1015
    { 0x4c, "kp_5" },
1016
    { 0x4d, "kp_6" },
1017
    { 0x47, "kp_7" },
1018
    { 0x48, "kp_8" },
1019
    { 0x49, "kp_9" },
1020

    
1021
    { 0x56, "<" },
1022

    
1023
    { 0x57, "f11" },
1024
    { 0x58, "f12" },
1025

    
1026
    { 0xb7, "print" },
1027

    
1028
    { 0xc7, "home" },
1029
    { 0xc9, "pgup" },
1030
    { 0xd1, "pgdn" },
1031
    { 0xcf, "end" },
1032

    
1033
    { 0xcb, "left" },
1034
    { 0xc8, "up" },
1035
    { 0xd0, "down" },
1036
    { 0xcd, "right" },
1037

    
1038
    { 0xd2, "insert" },
1039
    { 0xd3, "delete" },
1040
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1041
    { 0xf0, "stop" },
1042
    { 0xf1, "again" },
1043
    { 0xf2, "props" },
1044
    { 0xf3, "undo" },
1045
    { 0xf4, "front" },
1046
    { 0xf5, "copy" },
1047
    { 0xf6, "open" },
1048
    { 0xf7, "paste" },
1049
    { 0xf8, "find" },
1050
    { 0xf9, "cut" },
1051
    { 0xfa, "lf" },
1052
    { 0xfb, "help" },
1053
    { 0xfc, "meta_l" },
1054
    { 0xfd, "meta_r" },
1055
    { 0xfe, "compose" },
1056
#endif
1057
    { 0, NULL },
1058
};
1059

    
1060
static int get_keycode(const char *key)
1061
{
1062
    const KeyDef *p;
1063
    char *endp;
1064
    int ret;
1065

    
1066
    for(p = key_defs; p->name != NULL; p++) {
1067
        if (!strcmp(key, p->name))
1068
            return p->keycode;
1069
    }
1070
    if (strstart(key, "0x", NULL)) {
1071
        ret = strtoul(key, &endp, 0);
1072
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1073
            return ret;
1074
    }
1075
    return -1;
1076
}
1077

    
1078
#define MAX_KEYCODES 16
1079
static uint8_t keycodes[MAX_KEYCODES];
1080
static int nb_pending_keycodes;
1081
static QEMUTimer *key_timer;
1082

    
1083
static void release_keys(void *opaque)
1084
{
1085
    int keycode;
1086

    
1087
    while (nb_pending_keycodes > 0) {
1088
        nb_pending_keycodes--;
1089
        keycode = keycodes[nb_pending_keycodes];
1090
        if (keycode & 0x80)
1091
            kbd_put_keycode(0xe0);
1092
        kbd_put_keycode(keycode | 0x80);
1093
    }
1094
}
1095

    
1096
static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1097
                       int hold_time)
1098
{
1099
    char keyname_buf[16];
1100
    char *separator;
1101
    int keyname_len, keycode, i;
1102

    
1103
    if (nb_pending_keycodes > 0) {
1104
        qemu_del_timer(key_timer);
1105
        release_keys(NULL);
1106
    }
1107
    if (!has_hold_time)
1108
        hold_time = 100;
1109
    i = 0;
1110
    while (1) {
1111
        separator = strchr(string, '-');
1112
        keyname_len = separator ? separator - string : strlen(string);
1113
        if (keyname_len > 0) {
1114
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1115
            if (keyname_len > sizeof(keyname_buf) - 1) {
1116
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1117
                return;
1118
            }
1119
            if (i == MAX_KEYCODES) {
1120
                monitor_printf(mon, "too many keys\n");
1121
                return;
1122
            }
1123
            keyname_buf[keyname_len] = 0;
1124
            keycode = get_keycode(keyname_buf);
1125
            if (keycode < 0) {
1126
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1127
                return;
1128
            }
1129
            keycodes[i++] = keycode;
1130
        }
1131
        if (!separator)
1132
            break;
1133
        string = separator + 1;
1134
    }
1135
    nb_pending_keycodes = i;
1136
    /* key down events */
1137
    for (i = 0; i < nb_pending_keycodes; i++) {
1138
        keycode = keycodes[i];
1139
        if (keycode & 0x80)
1140
            kbd_put_keycode(0xe0);
1141
        kbd_put_keycode(keycode & 0x7f);
1142
    }
1143
    /* delayed key up events */
1144
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1145
                    muldiv64(ticks_per_sec, hold_time, 1000));
1146
}
1147

    
1148
static int mouse_button_state;
1149

    
1150
static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1151
                          const char *dz_str)
1152
{
1153
    int dx, dy, dz;
1154
    dx = strtol(dx_str, NULL, 0);
1155
    dy = strtol(dy_str, NULL, 0);
1156
    dz = 0;
1157
    if (dz_str)
1158
        dz = strtol(dz_str, NULL, 0);
1159
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1160
}
1161

    
1162
static void do_mouse_button(Monitor *mon, int button_state)
1163
{
1164
    mouse_button_state = button_state;
1165
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1166
}
1167

    
1168
static void do_ioport_read(Monitor *mon, int count, int format, int size,
1169
                           int addr, int has_index, int index)
1170
{
1171
    uint32_t val;
1172
    int suffix;
1173

    
1174
    if (has_index) {
1175
        cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1176
        addr++;
1177
    }
1178
    addr &= 0xffff;
1179

    
1180
    switch(size) {
1181
    default:
1182
    case 1:
1183
        val = cpu_inb(NULL, addr);
1184
        suffix = 'b';
1185
        break;
1186
    case 2:
1187
        val = cpu_inw(NULL, addr);
1188
        suffix = 'w';
1189
        break;
1190
    case 4:
1191
        val = cpu_inl(NULL, addr);
1192
        suffix = 'l';
1193
        break;
1194
    }
1195
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1196
                   suffix, addr, size * 2, val);
1197
}
1198

    
1199
static void do_ioport_write(Monitor *mon, int count, int format, int size,
1200
                            int addr, int val)
1201
{
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
    }
1216
}
1217

    
1218
static void do_boot_set(Monitor *mon, const char *bootdevice)
1219
{
1220
    int res;
1221

    
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");
1227
    } else {
1228
        monitor_printf(mon, "no function defined to set boot device list for "
1229
                       "this architecture\n");
1230
    }
1231
}
1232

    
1233
static void do_system_reset(Monitor *mon)
1234
{
1235
    qemu_system_reset_request();
1236
}
1237

    
1238
static void do_system_powerdown(Monitor *mon)
1239
{
1240
    qemu_system_powerdown_request();
1241
}
1242

    
1243
#if defined(TARGET_I386)
1244
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1245
{
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' : '-');
1257
}
1258

    
1259
static void tlb_info(Monitor *mon)
1260
{
1261
    CPUState *env;
1262
    int l1, l2;
1263
    uint32_t pgd, pde, pte;
1264

    
1265
    env = mon_get_cpu();
1266
    if (!env)
1267
        return;
1268

    
1269
    if (!(env->cr[0] & CR0_PG_MASK)) {
1270
        monitor_printf(mon, "PG disabled\n");
1271
        return;
1272
    }
1273
    pgd = env->cr[3] & ~0xfff;
1274
    for(l1 = 0; l1 < 1024; l1++) {
1275
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1276
        pde = le32_to_cpu(pde);
1277
        if (pde & PG_PRESENT_MASK) {
1278
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1279
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1280
            } else {
1281
                for(l2 = 0; l2 < 1024; l2++) {
1282
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1283
                                             (uint8_t *)&pte, 4);
1284
                    pte = le32_to_cpu(pte);
1285
                    if (pte & PG_PRESENT_MASK) {
1286
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1287
                                  pte & ~PG_PSE_MASK,
1288
                                  ~0xfff);
1289
                    }
1290
                }
1291
            }
1292
        }
1293
    }
1294
}
1295

    
1296
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1297
                      uint32_t end, int prot)
1298
{
1299
    int prot1;
1300
    prot1 = *plast_prot;
1301
    if (prot != prot1) {
1302
        if (*pstart != -1) {
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' : '-');
1308
        }
1309
        if (prot != 0)
1310
            *pstart = end;
1311
        else
1312
            *pstart = -1;
1313
        *plast_prot = prot;
1314
    }
1315
}
1316

    
1317
static void mem_info(Monitor *mon)
1318
{
1319
    CPUState *env;
1320
    int l1, l2, prot, last_prot;
1321
    uint32_t pgd, pde, pte, start, end;
1322

    
1323
    env = mon_get_cpu();
1324
    if (!env)
1325
        return;
1326

    
1327
    if (!(env->cr[0] & CR0_PG_MASK)) {
1328
        monitor_printf(mon, "PG disabled\n");
1329
        return;
1330
    }
1331
    pgd = env->cr[3] & ~0xfff;
1332
    last_prot = 0;
1333
    start = -1;
1334
    for(l1 = 0; l1 < 1024; l1++) {
1335
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1336
        pde = le32_to_cpu(pde);
1337
        end = l1 << 22;
1338
        if (pde & PG_PRESENT_MASK) {
1339
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1340
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1341
                mem_print(mon, &start, &last_prot, end, prot);
1342
            } else {
1343
                for(l2 = 0; l2 < 1024; l2++) {
1344
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1345
                                             (uint8_t *)&pte, 4);
1346
                    pte = le32_to_cpu(pte);
1347
                    end = (l1 << 22) + (l2 << 12);
1348
                    if (pte & PG_PRESENT_MASK) {
1349
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1350
                    } else {
1351
                        prot = 0;
1352
                    }
1353
                    mem_print(mon, &start, &last_prot, end, prot);
1354
                }
1355
            }
1356
        } else {
1357
            prot = 0;
1358
            mem_print(mon, &start, &last_prot, end, prot);
1359
        }
1360
    }
1361
}
1362
#endif
1363

    
1364
#if defined(TARGET_SH4)
1365

    
1366
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1367
{
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);
1376
}
1377

    
1378
static void tlb_info(Monitor *mon)
1379
{
1380
    CPUState *env = mon_get_cpu();
1381
    int i;
1382

    
1383
    monitor_printf (mon, "ITLB:\n");
1384
    for (i = 0 ; i < ITLB_SIZE ; i++)
1385
        print_tlb (mon, i, &env->itlb[i]);
1386
    monitor_printf (mon, "UTLB:\n");
1387
    for (i = 0 ; i < UTLB_SIZE ; i++)
1388
        print_tlb (mon, i, &env->utlb[i]);
1389
}
1390

    
1391
#endif
1392

    
1393
static void do_info_kqemu(Monitor *mon)
1394
{
1395
#ifdef CONFIG_KQEMU
1396
    CPUState *env;
1397
    int val;
1398
    val = 0;
1399
    env = mon_get_cpu();
1400
    if (!env) {
1401
        monitor_printf(mon, "No cpu initialized yet");
1402
        return;
1403
    }
1404
    val = env->kqemu_enabled;
1405
    monitor_printf(mon, "kqemu support: ");
1406
    switch(val) {
1407
    default:
1408
    case 0:
1409
        monitor_printf(mon, "disabled\n");
1410
        break;
1411
    case 1:
1412
        monitor_printf(mon, "enabled for user code\n");
1413
        break;
1414
    case 2:
1415
        monitor_printf(mon, "enabled for user and kernel code\n");
1416
        break;
1417
    }
1418
#else
1419
    monitor_printf(mon, "kqemu support: not compiled\n");
1420
#endif
1421
}
1422

    
1423
static void do_info_kvm(Monitor *mon)
1424
{
1425
#ifdef CONFIG_KVM
1426
    monitor_printf(mon, "kvm support: ");
1427
    if (kvm_enabled())
1428
        monitor_printf(mon, "enabled\n");
1429
    else
1430
        monitor_printf(mon, "disabled\n");
1431
#else
1432
    monitor_printf(mon, "kvm support: not compiled\n");
1433
#endif
1434
}
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

    
1455
#ifdef CONFIG_PROFILER
1456

    
1457
int64_t kqemu_time;
1458
int64_t qemu_time;
1459
int64_t kqemu_exec_count;
1460
int64_t dev_time;
1461
int64_t kqemu_ret_int_count;
1462
int64_t kqemu_ret_excp_count;
1463
int64_t kqemu_ret_intr_count;
1464

    
1465
static void do_info_profile(Monitor *mon)
1466
{
1467
    int64_t total;
1468
    total = qemu_time;
1469
    if (total == 0)
1470
        total = 1;
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);
1484
    qemu_time = 0;
1485
    kqemu_time = 0;
1486
    kqemu_exec_count = 0;
1487
    dev_time = 0;
1488
    kqemu_ret_int_count = 0;
1489
    kqemu_ret_excp_count = 0;
1490
    kqemu_ret_intr_count = 0;
1491
#ifdef CONFIG_KQEMU
1492
    kqemu_record_dump();
1493
#endif
1494
}
1495
#else
1496
static void do_info_profile(Monitor *mon)
1497
{
1498
    monitor_printf(mon, "Internal profiler not compiled\n");
1499
}
1500
#endif
1501

    
1502
/* Capture support */
1503
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1504

    
1505
static void do_info_capture(Monitor *mon)
1506
{
1507
    int i;
1508
    CaptureState *s;
1509

    
1510
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1511
        monitor_printf(mon, "[%d]: ", i);
1512
        s->ops.info (s->opaque);
1513
    }
1514
}
1515

    
1516
#ifdef HAS_AUDIO
1517
static void do_stop_capture(Monitor *mon, int n)
1518
{
1519
    int i;
1520
    CaptureState *s;
1521

    
1522
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1523
        if (i == n) {
1524
            s->ops.destroy (s->opaque);
1525
            LIST_REMOVE (s, entries);
1526
            qemu_free (s);
1527
            return;
1528
        }
1529
    }
1530
}
1531

    
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)
1536
{
1537
    CaptureState *s;
1538

    
1539
    s = qemu_mallocz (sizeof (*s));
1540

    
1541
    freq = has_freq ? freq : 44100;
1542
    bits = has_bits ? bits : 16;
1543
    nchannels = has_channels ? nchannels : 2;
1544

    
1545
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1546
        monitor_printf(mon, "Faied to add wave capture\n");
1547
        qemu_free (s);
1548
    }
1549
    LIST_INSERT_HEAD (&capture_head, s, entries);
1550
}
1551
#endif
1552

    
1553
#if defined(TARGET_I386)
1554
static void do_inject_nmi(Monitor *mon, int cpu_index)
1555
{
1556
    CPUState *env;
1557

    
1558
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1559
        if (env->cpu_index == cpu_index) {
1560
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1561
            break;
1562
        }
1563
}
1564
#endif
1565

    
1566
static void do_info_status(Monitor *mon)
1567
{
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");
1576
}
1577

    
1578

    
1579
static void do_balloon(Monitor *mon, int value)
1580
{
1581
    ram_addr_t target = value;
1582
    qemu_balloon(target << 20);
1583
}
1584

    
1585
static void do_info_balloon(Monitor *mon)
1586
{
1587
    ram_addr_t actual;
1588

    
1589
    actual = qemu_balloon_status();
1590
    if (kvm_enabled() && !kvm_has_sync_mmu())
1591
        monitor_printf(mon, "Using KVM without synchronous MMU, "
1592
                       "ballooning disabled\n");
1593
    else if (actual == 0)
1594
        monitor_printf(mon, "Ballooning not activated in VM\n");
1595
    else
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"
1805
    { NULL, NULL, },
1806
};
1807

    
1808
/* Please update qemu-monitor.hx when adding or changing commands */
1809
static const mon_cmd_t info_cmds[] = {
1810
    { "version", "", do_info_version,
1811
      "", "show the version of QEMU" },
1812
    { "network", "", do_info_network,
1813
      "", "show the network state" },
1814
    { "chardev", "", qemu_chr_info,
1815
      "", "show the character devices" },
1816
    { "block", "", bdrv_info,
1817
      "", "show the block devices" },
1818
    { "blockstats", "", bdrv_info_stats,
1819
      "", "show block device statistics" },
1820
    { "registers", "", do_info_registers,
1821
      "", "show the cpu registers" },
1822
    { "cpus", "", do_info_cpus,
1823
      "", "show infos for each CPU" },
1824
    { "history", "", do_info_history,
1825
      "", "show the command line history", },
1826
    { "irq", "", irq_info,
1827
      "", "show the interrupts statistics (if available)", },
1828
    { "pic", "", pic_info,
1829
      "", "show i8259 (PIC) state", },
1830
    { "pci", "", pci_info,
1831
      "", "show PCI info", },
1832
#if defined(TARGET_I386) || defined(TARGET_SH4)
1833
    { "tlb", "", tlb_info,
1834
      "", "show virtual to physical memory mappings", },
1835
#endif
1836
#if defined(TARGET_I386)
1837
    { "mem", "", mem_info,
1838
      "", "show the active virtual memory mappings", },
1839
    { "hpet", "", do_info_hpet,
1840
      "", "show state of HPET", },
1841
#endif
1842
    { "jit", "", do_info_jit,
1843
      "", "show dynamic compiler info", },
1844
    { "kqemu", "", do_info_kqemu,
1845
      "", "show KQEMU information", },
1846
    { "kvm", "", do_info_kvm,
1847
      "", "show KVM information", },
1848
    { "numa", "", do_info_numa,
1849
      "", "show NUMA information", },
1850
    { "usb", "", usb_info,
1851
      "", "show guest USB devices", },
1852
    { "usbhost", "", usb_host_info,
1853
      "", "show host USB devices", },
1854
    { "profile", "", do_info_profile,
1855
      "", "show profiling information", },
1856
    { "capture", "", do_info_capture,
1857
      "", "show capture information" },
1858
    { "snapshots", "", do_info_snapshots,
1859
      "", "show the currently saved VM snapshots" },
1860
    { "status", "", do_info_status,
1861
      "", "show the current VM status (running|paused)" },
1862
    { "pcmcia", "", pcmcia_info,
1863
      "", "show guest PCMCIA status" },
1864
    { "mice", "", do_info_mice,
1865
      "", "show which guest mouse is receiving events" },
1866
    { "vnc", "", do_info_vnc,
1867
      "", "show the vnc server status"},
1868
    { "name", "", do_info_name,
1869
      "", "show the current VM name" },
1870
    { "uuid", "", do_info_uuid,
1871
      "", "show the current VM UUID" },
1872
#if defined(TARGET_PPC)
1873
    { "cpustats", "", do_info_cpu_stats,
1874
      "", "show CPU statistics", },
1875
#endif
1876
#if defined(CONFIG_SLIRP)
1877
    { "usernet", "", do_info_usernet,
1878
      "", "show user network stack connection states", },
1879
#endif
1880
    { "migrate", "", do_info_migrate, "", "show migration status" },
1881
    { "balloon", "", do_info_balloon,
1882
      "", "show balloon information" },
1883
    { "qtree", "", do_info_qtree,
1884
      "", "show device tree" },
1885
    { NULL, NULL, },
1886
};
1887

    
1888
/*******************************************************************/
1889

    
1890
static const char *pch;
1891
static jmp_buf expr_env;
1892

    
1893
#define MD_TLONG 0
1894
#define MD_I32   1
1895

    
1896
typedef struct MonitorDef {
1897
    const char *name;
1898
    int offset;
1899
    target_long (*get_value)(const struct MonitorDef *md, int val);
1900
    int type;
1901
} MonitorDef;
1902

    
1903
#if defined(TARGET_I386)
1904
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1905
{
1906
    CPUState *env = mon_get_cpu();
1907
    if (!env)
1908
        return 0;
1909
    return env->eip + env->segs[R_CS].base;
1910
}
1911
#endif
1912

    
1913
#if defined(TARGET_PPC)
1914
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1915
{
1916
    CPUState *env = mon_get_cpu();
1917
    unsigned int u;
1918
    int i;
1919

    
1920
    if (!env)
1921
        return 0;
1922

    
1923
    u = 0;
1924
    for (i = 0; i < 8; i++)
1925
        u |= env->crf[i] << (32 - (4 * i));
1926

    
1927
    return u;
1928
}
1929

    
1930
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1931
{
1932
    CPUState *env = mon_get_cpu();
1933
    if (!env)
1934
        return 0;
1935
    return env->msr;
1936
}
1937

    
1938
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1939
{
1940
    CPUState *env = mon_get_cpu();
1941
    if (!env)
1942
        return 0;
1943
    return env->xer;
1944
}
1945

    
1946
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1947
{
1948
    CPUState *env = mon_get_cpu();
1949
    if (!env)
1950
        return 0;
1951
    return cpu_ppc_load_decr(env);
1952
}
1953

    
1954
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1955
{
1956
    CPUState *env = mon_get_cpu();
1957
    if (!env)
1958
        return 0;
1959
    return cpu_ppc_load_tbu(env);
1960
}
1961

    
1962
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1963
{
1964
    CPUState *env = mon_get_cpu();
1965
    if (!env)
1966
        return 0;
1967
    return cpu_ppc_load_tbl(env);
1968
}
1969
#endif
1970

    
1971
#if defined(TARGET_SPARC)
1972
#ifndef TARGET_SPARC64
1973
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1974
{
1975
    CPUState *env = mon_get_cpu();
1976
    if (!env)
1977
        return 0;
1978
    return GET_PSR(env);
1979
}
1980
#endif
1981

    
1982
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1983
{
1984
    CPUState *env = mon_get_cpu();
1985
    if (!env)
1986
        return 0;
1987
    return env->regwptr[val];
1988
}
1989
#endif
1990

    
1991
static const MonitorDef monitor_defs[] = {
1992
#ifdef TARGET_I386
1993

    
1994
#define SEG(name, seg) \
1995
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1996
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1997
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1998

    
1999
    { "eax", offsetof(CPUState, regs[0]) },
2000
    { "ecx", offsetof(CPUState, regs[1]) },
2001
    { "edx", offsetof(CPUState, regs[2]) },
2002
    { "ebx", offsetof(CPUState, regs[3]) },
2003
    { "esp|sp", offsetof(CPUState, regs[4]) },
2004
    { "ebp|fp", offsetof(CPUState, regs[5]) },
2005
    { "esi", offsetof(CPUState, regs[6]) },
2006
    { "edi", offsetof(CPUState, regs[7]) },
2007
#ifdef TARGET_X86_64
2008
    { "r8", offsetof(CPUState, regs[8]) },
2009
    { "r9", offsetof(CPUState, regs[9]) },
2010
    { "r10", offsetof(CPUState, regs[10]) },
2011
    { "r11", offsetof(CPUState, regs[11]) },
2012
    { "r12", offsetof(CPUState, regs[12]) },
2013
    { "r13", offsetof(CPUState, regs[13]) },
2014
    { "r14", offsetof(CPUState, regs[14]) },
2015
    { "r15", offsetof(CPUState, regs[15]) },
2016
#endif
2017
    { "eflags", offsetof(CPUState, eflags) },
2018
    { "eip", offsetof(CPUState, eip) },
2019
    SEG("cs", R_CS)
2020
    SEG("ds", R_DS)
2021
    SEG("es", R_ES)
2022
    SEG("ss", R_SS)
2023
    SEG("fs", R_FS)
2024
    SEG("gs", R_GS)
2025
    { "pc", 0, monitor_get_pc, },
2026
#elif defined(TARGET_PPC)
2027
    /* General purpose registers */
2028
    { "r0", offsetof(CPUState, gpr[0]) },
2029
    { "r1", offsetof(CPUState, gpr[1]) },
2030
    { "r2", offsetof(CPUState, gpr[2]) },
2031
    { "r3", offsetof(CPUState, gpr[3]) },
2032
    { "r4", offsetof(CPUState, gpr[4]) },
2033
    { "r5", offsetof(CPUState, gpr[5]) },
2034
    { "r6", offsetof(CPUState, gpr[6]) },
2035
    { "r7", offsetof(CPUState, gpr[7]) },
2036
    { "r8", offsetof(CPUState, gpr[8]) },
2037
    { "r9", offsetof(CPUState, gpr[9]) },
2038
    { "r10", offsetof(CPUState, gpr[10]) },
2039
    { "r11", offsetof(CPUState, gpr[11]) },
2040
    { "r12", offsetof(CPUState, gpr[12]) },
2041
    { "r13", offsetof(CPUState, gpr[13]) },
2042
    { "r14", offsetof(CPUState, gpr[14]) },
2043
    { "r15", offsetof(CPUState, gpr[15]) },
2044
    { "r16", offsetof(CPUState, gpr[16]) },
2045
    { "r17", offsetof(CPUState, gpr[17]) },
2046
    { "r18", offsetof(CPUState, gpr[18]) },
2047
    { "r19", offsetof(CPUState, gpr[19]) },
2048
    { "r20", offsetof(CPUState, gpr[20]) },
2049
    { "r21", offsetof(CPUState, gpr[21]) },
2050
    { "r22", offsetof(CPUState, gpr[22]) },
2051
    { "r23", offsetof(CPUState, gpr[23]) },
2052
    { "r24", offsetof(CPUState, gpr[24]) },
2053
    { "r25", offsetof(CPUState, gpr[25]) },
2054
    { "r26", offsetof(CPUState, gpr[26]) },
2055
    { "r27", offsetof(CPUState, gpr[27]) },
2056
    { "r28", offsetof(CPUState, gpr[28]) },
2057
    { "r29", offsetof(CPUState, gpr[29]) },
2058
    { "r30", offsetof(CPUState, gpr[30]) },
2059
    { "r31", offsetof(CPUState, gpr[31]) },
2060
    /* Floating point registers */
2061
    { "f0", offsetof(CPUState, fpr[0]) },
2062
    { "f1", offsetof(CPUState, fpr[1]) },
2063
    { "f2", offsetof(CPUState, fpr[2]) },
2064
    { "f3", offsetof(CPUState, fpr[3]) },
2065
    { "f4", offsetof(CPUState, fpr[4]) },
2066
    { "f5", offsetof(CPUState, fpr[5]) },
2067
    { "f6", offsetof(CPUState, fpr[6]) },
2068
    { "f7", offsetof(CPUState, fpr[7]) },
2069
    { "f8", offsetof(CPUState, fpr[8]) },
2070
    { "f9", offsetof(CPUState, fpr[9]) },
2071
    { "f10", offsetof(CPUState, fpr[10]) },
2072
    { "f11", offsetof(CPUState, fpr[11]) },
2073
    { "f12", offsetof(CPUState, fpr[12]) },
2074
    { "f13", offsetof(CPUState, fpr[13]) },
2075
    { "f14", offsetof(CPUState, fpr[14]) },
2076
    { "f15", offsetof(CPUState, fpr[15]) },
2077
    { "f16", offsetof(CPUState, fpr[16]) },
2078
    { "f17", offsetof(CPUState, fpr[17]) },
2079
    { "f18", offsetof(CPUState, fpr[18]) },
2080
    { "f19", offsetof(CPUState, fpr[19]) },
2081
    { "f20", offsetof(CPUState, fpr[20]) },
2082
    { "f21", offsetof(CPUState, fpr[21]) },
2083
    { "f22", offsetof(CPUState, fpr[22]) },
2084
    { "f23", offsetof(CPUState, fpr[23]) },
2085
    { "f24", offsetof(CPUState, fpr[24]) },
2086
    { "f25", offsetof(CPUState, fpr[25]) },
2087
    { "f26", offsetof(CPUState, fpr[26]) },
2088
    { "f27", offsetof(CPUState, fpr[27]) },
2089
    { "f28", offsetof(CPUState, fpr[28]) },
2090
    { "f29", offsetof(CPUState, fpr[29]) },
2091
    { "f30", offsetof(CPUState, fpr[30]) },
2092
    { "f31", offsetof(CPUState, fpr[31]) },
2093
    { "fpscr", offsetof(CPUState, fpscr) },
2094
    /* Next instruction pointer */
2095
    { "nip|pc", offsetof(CPUState, nip) },
2096
    { "lr", offsetof(CPUState, lr) },
2097
    { "ctr", offsetof(CPUState, ctr) },
2098
    { "decr", 0, &monitor_get_decr, },
2099
    { "ccr", 0, &monitor_get_ccr, },
2100
    /* Machine state register */
2101
    { "msr", 0, &monitor_get_msr, },
2102
    { "xer", 0, &monitor_get_xer, },
2103
    { "tbu", 0, &monitor_get_tbu, },
2104
    { "tbl", 0, &monitor_get_tbl, },
2105
#if defined(TARGET_PPC64)
2106
    /* Address space register */
2107
    { "asr", offsetof(CPUState, asr) },
2108
#endif
2109
    /* Segment registers */
2110
    { "sdr1", offsetof(CPUState, sdr1) },
2111
    { "sr0", offsetof(CPUState, sr[0]) },
2112
    { "sr1", offsetof(CPUState, sr[1]) },
2113
    { "sr2", offsetof(CPUState, sr[2]) },
2114
    { "sr3", offsetof(CPUState, sr[3]) },
2115
    { "sr4", offsetof(CPUState, sr[4]) },
2116
    { "sr5", offsetof(CPUState, sr[5]) },
2117
    { "sr6", offsetof(CPUState, sr[6]) },
2118
    { "sr7", offsetof(CPUState, sr[7]) },
2119
    { "sr8", offsetof(CPUState, sr[8]) },
2120
    { "sr9", offsetof(CPUState, sr[9]) },
2121
    { "sr10", offsetof(CPUState, sr[10]) },
2122
    { "sr11", offsetof(CPUState, sr[11]) },
2123
    { "sr12", offsetof(CPUState, sr[12]) },
2124
    { "sr13", offsetof(CPUState, sr[13]) },
2125
    { "sr14", offsetof(CPUState, sr[14]) },
2126
    { "sr15", offsetof(CPUState, sr[15]) },
2127
    /* Too lazy to put BATs and SPRs ... */
2128
#elif defined(TARGET_SPARC)
2129
    { "g0", offsetof(CPUState, gregs[0]) },
2130
    { "g1", offsetof(CPUState, gregs[1]) },
2131
    { "g2", offsetof(CPUState, gregs[2]) },
2132
    { "g3", offsetof(CPUState, gregs[3]) },
2133
    { "g4", offsetof(CPUState, gregs[4]) },
2134
    { "g5", offsetof(CPUState, gregs[5]) },
2135
    { "g6", offsetof(CPUState, gregs[6]) },
2136
    { "g7", offsetof(CPUState, gregs[7]) },
2137
    { "o0", 0, monitor_get_reg },
2138
    { "o1", 1, monitor_get_reg },
2139
    { "o2", 2, monitor_get_reg },
2140
    { "o3", 3, monitor_get_reg },
2141
    { "o4", 4, monitor_get_reg },
2142
    { "o5", 5, monitor_get_reg },
2143
    { "o6", 6, monitor_get_reg },
2144
    { "o7", 7, monitor_get_reg },
2145
    { "l0", 8, monitor_get_reg },
2146
    { "l1", 9, monitor_get_reg },
2147
    { "l2", 10, monitor_get_reg },
2148
    { "l3", 11, monitor_get_reg },
2149
    { "l4", 12, monitor_get_reg },
2150
    { "l5", 13, monitor_get_reg },
2151
    { "l6", 14, monitor_get_reg },
2152
    { "l7", 15, monitor_get_reg },
2153
    { "i0", 16, monitor_get_reg },
2154
    { "i1", 17, monitor_get_reg },
2155
    { "i2", 18, monitor_get_reg },
2156
    { "i3", 19, monitor_get_reg },
2157
    { "i4", 20, monitor_get_reg },
2158
    { "i5", 21, monitor_get_reg },
2159
    { "i6", 22, monitor_get_reg },
2160
    { "i7", 23, monitor_get_reg },
2161
    { "pc", offsetof(CPUState, pc) },
2162
    { "npc", offsetof(CPUState, npc) },
2163
    { "y", offsetof(CPUState, y) },
2164
#ifndef TARGET_SPARC64
2165
    { "psr", 0, &monitor_get_psr, },
2166
    { "wim", offsetof(CPUState, wim) },
2167
#endif
2168
    { "tbr", offsetof(CPUState, tbr) },
2169
    { "fsr", offsetof(CPUState, fsr) },
2170
    { "f0", offsetof(CPUState, fpr[0]) },
2171
    { "f1", offsetof(CPUState, fpr[1]) },
2172
    { "f2", offsetof(CPUState, fpr[2]) },
2173
    { "f3", offsetof(CPUState, fpr[3]) },
2174
    { "f4", offsetof(CPUState, fpr[4]) },
2175
    { "f5", offsetof(CPUState, fpr[5]) },
2176
    { "f6", offsetof(CPUState, fpr[6]) },
2177
    { "f7", offsetof(CPUState, fpr[7]) },
2178
    { "f8", offsetof(CPUState, fpr[8]) },
2179
    { "f9", offsetof(CPUState, fpr[9]) },
2180
    { "f10", offsetof(CPUState, fpr[10]) },
2181
    { "f11", offsetof(CPUState, fpr[11]) },
2182
    { "f12", offsetof(CPUState, fpr[12]) },
2183
    { "f13", offsetof(CPUState, fpr[13]) },
2184
    { "f14", offsetof(CPUState, fpr[14]) },
2185
    { "f15", offsetof(CPUState, fpr[15]) },
2186
    { "f16", offsetof(CPUState, fpr[16]) },
2187
    { "f17", offsetof(CPUState, fpr[17]) },
2188
    { "f18", offsetof(CPUState, fpr[18]) },
2189
    { "f19", offsetof(CPUState, fpr[19]) },
2190
    { "f20", offsetof(CPUState, fpr[20]) },
2191
    { "f21", offsetof(CPUState, fpr[21]) },
2192
    { "f22", offsetof(CPUState, fpr[22]) },
2193
    { "f23", offsetof(CPUState, fpr[23]) },
2194
    { "f24", offsetof(CPUState, fpr[24]) },
2195
    { "f25", offsetof(CPUState, fpr[25]) },
2196
    { "f26", offsetof(CPUState, fpr[26]) },
2197
    { "f27", offsetof(CPUState, fpr[27]) },
2198
    { "f28", offsetof(CPUState, fpr[28]) },
2199
    { "f29", offsetof(CPUState, fpr[29]) },
2200
    { "f30", offsetof(CPUState, fpr[30]) },
2201
    { "f31", offsetof(CPUState, fpr[31]) },
2202
#ifdef TARGET_SPARC64
2203
    { "f32", offsetof(CPUState, fpr[32]) },
2204
    { "f34", offsetof(CPUState, fpr[34]) },
2205
    { "f36", offsetof(CPUState, fpr[36]) },
2206
    { "f38", offsetof(CPUState, fpr[38]) },
2207
    { "f40", offsetof(CPUState, fpr[40]) },
2208
    { "f42", offsetof(CPUState, fpr[42]) },
2209
    { "f44", offsetof(CPUState, fpr[44]) },
2210
    { "f46", offsetof(CPUState, fpr[46]) },
2211
    { "f48", offsetof(CPUState, fpr[48]) },
2212
    { "f50", offsetof(CPUState, fpr[50]) },
2213
    { "f52", offsetof(CPUState, fpr[52]) },
2214
    { "f54", offsetof(CPUState, fpr[54]) },
2215
    { "f56", offsetof(CPUState, fpr[56]) },
2216
    { "f58", offsetof(CPUState, fpr[58]) },
2217
    { "f60", offsetof(CPUState, fpr[60]) },
2218
    { "f62", offsetof(CPUState, fpr[62]) },
2219
    { "asi", offsetof(CPUState, asi) },
2220
    { "pstate", offsetof(CPUState, pstate) },
2221
    { "cansave", offsetof(CPUState, cansave) },
2222
    { "canrestore", offsetof(CPUState, canrestore) },
2223
    { "otherwin", offsetof(CPUState, otherwin) },
2224
    { "wstate", offsetof(CPUState, wstate) },
2225
    { "cleanwin", offsetof(CPUState, cleanwin) },
2226
    { "fprs", offsetof(CPUState, fprs) },
2227
#endif
2228
#endif
2229
    { NULL },
2230
};
2231

    
2232
static void expr_error(Monitor *mon, const char *msg)
2233
{
2234
    monitor_printf(mon, "%s\n", msg);
2235
    longjmp(expr_env, 1);
2236
}
2237

    
2238
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2239
static int get_monitor_def(target_long *pval, const char *name)
2240
{
2241
    const MonitorDef *md;
2242
    void *ptr;
2243

    
2244
    for(md = monitor_defs; md->name != NULL; md++) {
2245
        if (compare_cmd(name, md->name)) {
2246
            if (md->get_value) {
2247
                *pval = md->get_value(md, md->offset);
2248
            } else {
2249
                CPUState *env = mon_get_cpu();
2250
                if (!env)
2251
                    return -2;
2252
                ptr = (uint8_t *)env + md->offset;
2253
                switch(md->type) {
2254
                case MD_I32:
2255
                    *pval = *(int32_t *)ptr;
2256
                    break;
2257
                case MD_TLONG:
2258
                    *pval = *(target_long *)ptr;
2259
                    break;
2260
                default:
2261
                    *pval = 0;
2262
                    break;
2263
                }
2264
            }
2265
            return 0;
2266
        }
2267
    }
2268
    return -1;
2269
}
2270

    
2271
static void next(void)
2272
{
2273
    if (pch != '\0') {
2274
        pch++;
2275
        while (qemu_isspace(*pch))
2276
            pch++;
2277
    }
2278
}
2279

    
2280
static int64_t expr_sum(Monitor *mon);
2281

    
2282
static int64_t expr_unary(Monitor *mon)
2283
{
2284
    int64_t n;
2285
    char *p;
2286
    int ret;
2287

    
2288
    switch(*pch) {
2289
    case '+':
2290
        next();
2291
        n = expr_unary(mon);
2292
        break;
2293
    case '-':
2294
        next();
2295
        n = -expr_unary(mon);
2296
        break;
2297
    case '~':
2298
        next();
2299
        n = ~expr_unary(mon);
2300
        break;
2301
    case '(':
2302
        next();
2303
        n = expr_sum(mon);
2304
        if (*pch != ')') {
2305
            expr_error(mon, "')' expected");
2306
        }
2307
        next();
2308
        break;
2309
    case '\'':
2310
        pch++;
2311
        if (*pch == '\0')
2312
            expr_error(mon, "character constant expected");
2313
        n = *pch;
2314
        pch++;
2315
        if (*pch != '\'')
2316
            expr_error(mon, "missing terminating \' character");
2317
        next();
2318
        break;
2319
    case '$':
2320
        {
2321
            char buf[128], *q;
2322
            target_long reg=0;
2323

    
2324
            pch++;
2325
            q = buf;
2326
            while ((*pch >= 'a' && *pch <= 'z') ||
2327
                   (*pch >= 'A' && *pch <= 'Z') ||
2328
                   (*pch >= '0' && *pch <= '9') ||
2329
                   *pch == '_' || *pch == '.') {
2330
                if ((q - buf) < sizeof(buf) - 1)
2331
                    *q++ = *pch;
2332
                pch++;
2333
            }
2334
            while (qemu_isspace(*pch))
2335
                pch++;
2336
            *q = 0;
2337
            ret = get_monitor_def(&reg, buf);
2338
            if (ret == -1)
2339
                expr_error(mon, "unknown register");
2340
            else if (ret == -2)
2341
                expr_error(mon, "no cpu defined");
2342
            n = reg;
2343
        }
2344
        break;
2345
    case '\0':
2346
        expr_error(mon, "unexpected end of expression");
2347
        n = 0;
2348
        break;
2349
    default:
2350
#if TARGET_PHYS_ADDR_BITS > 32
2351
        n = strtoull(pch, &p, 0);
2352
#else
2353
        n = strtoul(pch, &p, 0);
2354
#endif
2355
        if (pch == p) {
2356
            expr_error(mon, "invalid char in expression");
2357
        }
2358
        pch = p;
2359
        while (qemu_isspace(*pch))
2360
            pch++;
2361
        break;
2362
    }
2363
    return n;
2364
}
2365

    
2366

    
2367
static int64_t expr_prod(Monitor *mon)
2368
{
2369
    int64_t val, val2;
2370
    int op;
2371

    
2372
    val = expr_unary(mon);
2373
    for(;;) {
2374
        op = *pch;
2375
        if (op != '*' && op != '/' && op != '%')
2376
            break;
2377
        next();
2378
        val2 = expr_unary(mon);
2379
        switch(op) {
2380
        default:
2381
        case '*':
2382
            val *= val2;
2383
            break;
2384
        case '/':
2385
        case '%':
2386
            if (val2 == 0)
2387
                expr_error(mon, "division by zero");
2388
            if (op == '/')
2389
                val /= val2;
2390
            else
2391
                val %= val2;
2392
            break;
2393
        }
2394
    }
2395
    return val;
2396
}
2397

    
2398
static int64_t expr_logic(Monitor *mon)
2399
{
2400
    int64_t val, val2;
2401
    int op;
2402

    
2403
    val = expr_prod(mon);
2404
    for(;;) {
2405
        op = *pch;
2406
        if (op != '&' && op != '|' && op != '^')
2407
            break;
2408
        next();
2409
        val2 = expr_prod(mon);
2410
        switch(op) {
2411
        default:
2412
        case '&':
2413
            val &= val2;
2414
            break;
2415
        case '|':
2416
            val |= val2;
2417
            break;
2418
        case '^':
2419
            val ^= val2;
2420
            break;
2421
        }
2422
    }
2423
    return val;
2424
}
2425

    
2426
static int64_t expr_sum(Monitor *mon)
2427
{
2428
    int64_t val, val2;
2429
    int op;
2430

    
2431
    val = expr_logic(mon);
2432
    for(;;) {
2433
        op = *pch;
2434
        if (op != '+' && op != '-')
2435
            break;
2436
        next();
2437
        val2 = expr_logic(mon);
2438
        if (op == '+')
2439
            val += val2;
2440
        else
2441
            val -= val2;
2442
    }
2443
    return val;
2444
}
2445

    
2446
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2447
{
2448
    pch = *pp;
2449
    if (setjmp(expr_env)) {
2450
        *pp = pch;
2451
        return -1;
2452
    }
2453
    while (qemu_isspace(*pch))
2454
        pch++;
2455
    *pval = expr_sum(mon);
2456
    *pp = pch;
2457
    return 0;
2458
}
2459

    
2460
static int get_str(char *buf, int buf_size, const char **pp)
2461
{
2462
    const char *p;
2463
    char *q;
2464
    int c;
2465

    
2466
    q = buf;
2467
    p = *pp;
2468
    while (qemu_isspace(*p))
2469
        p++;
2470
    if (*p == '\0') {
2471
    fail:
2472
        *q = '\0';
2473
        *pp = p;
2474
        return -1;
2475
    }
2476
    if (*p == '\"') {
2477
        p++;
2478
        while (*p != '\0' && *p != '\"') {
2479
            if (*p == '\\') {
2480
                p++;
2481
                c = *p++;
2482
                switch(c) {
2483
                case 'n':
2484
                    c = '\n';
2485
                    break;
2486
                case 'r':
2487
                    c = '\r';
2488
                    break;
2489
                case '\\':
2490
                case '\'':
2491
                case '\"':
2492
                    break;
2493
                default:
2494
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2495
                    goto fail;
2496
                }
2497
                if ((q - buf) < buf_size - 1) {
2498
                    *q++ = c;
2499
                }
2500
            } else {
2501
                if ((q - buf) < buf_size - 1) {
2502
                    *q++ = *p;
2503
                }
2504
                p++;
2505
            }
2506
        }
2507
        if (*p != '\"') {
2508
            qemu_printf("unterminated string\n");
2509
            goto fail;
2510
        }
2511
        p++;
2512
    } else {
2513
        while (*p != '\0' && !qemu_isspace(*p)) {
2514
            if ((q - buf) < buf_size - 1) {
2515
                *q++ = *p;
2516
            }
2517
            p++;
2518
        }
2519
    }
2520
    *q = '\0';
2521
    *pp = p;
2522
    return 0;
2523
}
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

    
2551
static int default_fmt_format = 'x';
2552
static int default_fmt_size = 4;
2553

    
2554
#define MAX_ARGS 16
2555

    
2556
static void monitor_handle_command(Monitor *mon, const char *cmdline)
2557
{
2558
    const char *p, *typestr;
2559
    int c, nb_args, i, has_arg;
2560
    const mon_cmd_t *cmd;
2561
    char cmdname[256];
2562
    char buf[1024];
2563
    void *str_allocated[MAX_ARGS];
2564
    void *args[MAX_ARGS];
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);
2586

    
2587
#ifdef DEBUG
2588
    monitor_printf(mon, "command='%s'\n", cmdline);
2589
#endif
2590

    
2591
    /* extract the command name */
2592
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2593
    if (!p)
2594
        return;
2595

    
2596
    /* find the command */
2597
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2598
        if (compare_cmd(cmdname, cmd->name))
2599
            break;
2600
    }
2601

    
2602
    if (cmd->name == NULL) {
2603
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2604
        return;
2605
    }
2606

    
2607
    for(i = 0; i < MAX_ARGS; i++)
2608
        str_allocated[i] = NULL;
2609

    
2610
    /* parse the parameters */
2611
    typestr = cmd->args_type;
2612
    nb_args = 0;
2613
    for(;;) {
2614
        c = *typestr;
2615
        if (c == '\0')
2616
            break;
2617
        typestr++;
2618
        switch(c) {
2619
        case 'F':
2620
        case 'B':
2621
        case 's':
2622
            {
2623
                int ret;
2624
                char *str;
2625

    
2626
                while (qemu_isspace(*p))
2627
                    p++;
2628
                if (*typestr == '?') {
2629
                    typestr++;
2630
                    if (*p == '\0') {
2631
                        /* no optional string: NULL argument */
2632
                        str = NULL;
2633
                        goto add_str;
2634
                    }
2635
                }
2636
                ret = get_str(buf, sizeof(buf), &p);
2637
                if (ret < 0) {
2638
                    switch(c) {
2639
                    case 'F':
2640
                        monitor_printf(mon, "%s: filename expected\n",
2641
                                       cmdname);
2642
                        break;
2643
                    case 'B':
2644
                        monitor_printf(mon, "%s: block device name expected\n",
2645
                                       cmdname);
2646
                        break;
2647
                    default:
2648
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2649
                        break;
2650
                    }
2651
                    goto fail;
2652
                }
2653
                str = qemu_malloc(strlen(buf) + 1);
2654
                pstrcpy(str, sizeof(buf), buf);
2655
                str_allocated[nb_args] = str;
2656
            add_str:
2657
                if (nb_args >= MAX_ARGS) {
2658
                error_args:
2659
                    monitor_printf(mon, "%s: too many arguments\n", cmdname);
2660
                    goto fail;
2661
                }
2662
                args[nb_args++] = str;
2663
            }
2664
            break;
2665
        case '/':
2666
            {
2667
                int count, format, size;
2668

    
2669
                while (qemu_isspace(*p))
2670
                    p++;
2671
                if (*p == '/') {
2672
                    /* format found */
2673
                    p++;
2674
                    count = 1;
2675
                    if (qemu_isdigit(*p)) {
2676
                        count = 0;
2677
                        while (qemu_isdigit(*p)) {
2678
                            count = count * 10 + (*p - '0');
2679
                            p++;
2680
                        }
2681
                    }
2682
                    size = -1;
2683
                    format = -1;
2684
                    for(;;) {
2685
                        switch(*p) {
2686
                        case 'o':
2687
                        case 'd':
2688
                        case 'u':
2689
                        case 'x':
2690
                        case 'i':
2691
                        case 'c':
2692
                            format = *p++;
2693
                            break;
2694
                        case 'b':
2695
                            size = 1;
2696
                            p++;
2697
                            break;
2698
                        case 'h':
2699
                            size = 2;
2700
                            p++;
2701
                            break;
2702
                        case 'w':
2703
                            size = 4;
2704
                            p++;
2705
                            break;
2706
                        case 'g':
2707
                        case 'L':
2708
                            size = 8;
2709
                            p++;
2710
                            break;
2711
                        default:
2712
                            goto next;
2713
                        }
2714
                    }
2715
                next:
2716
                    if (*p != '\0' && !qemu_isspace(*p)) {
2717
                        monitor_printf(mon, "invalid char in format: '%c'\n",
2718
                                       *p);
2719
                        goto fail;
2720
                    }
2721
                    if (format < 0)
2722
                        format = default_fmt_format;
2723
                    if (format != 'i') {
2724
                        /* for 'i', not specifying a size gives -1 as size */
2725
                        if (size < 0)
2726
                            size = default_fmt_size;
2727
                        default_fmt_size = size;
2728
                    }
2729
                    default_fmt_format = format;
2730
                } else {
2731
                    count = 1;
2732
                    format = default_fmt_format;
2733
                    if (format != 'i') {
2734
                        size = default_fmt_size;
2735
                    } else {
2736
                        size = -1;
2737
                    }
2738
                }
2739
                if (nb_args + 3 > MAX_ARGS)
2740
                    goto error_args;
2741
                args[nb_args++] = (void*)(long)count;
2742
                args[nb_args++] = (void*)(long)format;
2743
                args[nb_args++] = (void*)(long)size;
2744
            }
2745
            break;
2746
        case 'i':
2747
        case 'l':
2748
            {
2749
                int64_t val;
2750

    
2751
                while (qemu_isspace(*p))
2752
                    p++;
2753
                if (*typestr == '?' || *typestr == '.') {
2754
                    if (*typestr == '?') {
2755
                        if (*p == '\0')
2756
                            has_arg = 0;
2757
                        else
2758
                            has_arg = 1;
2759
                    } else {
2760
                        if (*p == '.') {
2761
                            p++;
2762
                            while (qemu_isspace(*p))
2763
                                p++;
2764
                            has_arg = 1;
2765
                        } else {
2766
                            has_arg = 0;
2767
                        }
2768
                    }
2769
                    typestr++;
2770
                    if (nb_args >= MAX_ARGS)
2771
                        goto error_args;
2772
                    args[nb_args++] = (void *)(long)has_arg;
2773
                    if (!has_arg) {
2774
                        if (nb_args >= MAX_ARGS)
2775
                            goto error_args;
2776
                        val = -1;
2777
                        goto add_num;
2778
                    }
2779
                }
2780
                if (get_expr(mon, &val, &p))
2781
                    goto fail;
2782
            add_num:
2783
                if (c == 'i') {
2784
                    if (nb_args >= MAX_ARGS)
2785
                        goto error_args;
2786
                    args[nb_args++] = (void *)(long)val;
2787
                } else {
2788
                    if ((nb_args + 1) >= MAX_ARGS)
2789
                        goto error_args;
2790
#if TARGET_PHYS_ADDR_BITS > 32
2791
                    args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2792
#else
2793
                    args[nb_args++] = (void *)0;
2794
#endif
2795
                    args[nb_args++] = (void *)(long)(val & 0xffffffff);
2796
                }
2797
            }
2798
            break;
2799
        case '-':
2800
            {
2801
                int has_option;
2802
                /* option */
2803

    
2804
                c = *typestr++;
2805
                if (c == '\0')
2806
                    goto bad_type;
2807
                while (qemu_isspace(*p))
2808
                    p++;
2809
                has_option = 0;
2810
                if (*p == '-') {
2811
                    p++;
2812
                    if (*p != c) {
2813
                        monitor_printf(mon, "%s: unsupported option -%c\n",
2814
                                       cmdname, *p);
2815
                        goto fail;
2816
                    }
2817
                    p++;
2818
                    has_option = 1;
2819
                }
2820
                if (nb_args >= MAX_ARGS)
2821
                    goto error_args;
2822
                args[nb_args++] = (void *)(long)has_option;
2823
            }
2824
            break;
2825
        default:
2826
        bad_type:
2827
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2828
            goto fail;
2829
        }
2830
    }
2831
    /* check that all arguments were parsed */
2832
    while (qemu_isspace(*p))
2833
        p++;
2834
    if (*p != '\0') {
2835
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2836
                       cmdname);
2837
        goto fail;
2838
    }
2839

    
2840
    switch(nb_args) {
2841
    case 0:
2842
        handler_0 = cmd->handler;
2843
        handler_0(mon);
2844
        break;
2845
    case 1:
2846
        handler_1 = cmd->handler;
2847
        handler_1(mon, args[0]);
2848
        break;
2849
    case 2:
2850
        handler_2 = cmd->handler;
2851
        handler_2(mon, args[0], args[1]);
2852
        break;
2853
    case 3:
2854
        handler_3 = cmd->handler;
2855
        handler_3(mon, args[0], args[1], args[2]);
2856
        break;
2857
    case 4:
2858
        handler_4 = cmd->handler;
2859
        handler_4(mon, args[0], args[1], args[2], args[3]);
2860
        break;
2861
    case 5:
2862
        handler_5 = cmd->handler;
2863
        handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2864
        break;
2865
    case 6:
2866
        handler_6 = cmd->handler;
2867
        handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2868
        break;
2869
    case 7:
2870
        handler_7 = cmd->handler;
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]);
2888
        break;
2889
    default:
2890
        monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2891
        goto fail;
2892
    }
2893
 fail:
2894
    for(i = 0; i < MAX_ARGS; i++)
2895
        qemu_free(str_allocated[i]);
2896
}
2897

    
2898
static void cmd_completion(const char *name, const char *list)
2899
{
2900
    const char *p, *pstart;
2901
    char cmd[128];
2902
    int len;
2903

    
2904
    p = list;
2905
    for(;;) {
2906
        pstart = p;
2907
        p = strchr(p, '|');
2908
        if (!p)
2909
            p = pstart + strlen(pstart);
2910
        len = p - pstart;
2911
        if (len > sizeof(cmd) - 2)
2912
            len = sizeof(cmd) - 2;
2913
        memcpy(cmd, pstart, len);
2914
        cmd[len] = '\0';
2915
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2916
            readline_add_completion(cur_mon->rs, cmd);
2917
        }
2918
        if (*p == '\0')
2919
            break;
2920
        p++;
2921
    }
2922
}
2923

    
2924
static void file_completion(const char *input)
2925
{
2926
    DIR *ffs;
2927
    struct dirent *d;
2928
    char path[1024];
2929
    char file[1024], file_prefix[1024];
2930
    int input_path_len;
2931
    const char *p;
2932

    
2933
    p = strrchr(input, '/');
2934
    if (!p) {
2935
        input_path_len = 0;
2936
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2937
        pstrcpy(path, sizeof(path), ".");
2938
    } else {
2939
        input_path_len = p - input + 1;
2940
        memcpy(path, input, input_path_len);
2941
        if (input_path_len > sizeof(path) - 1)
2942
            input_path_len = sizeof(path) - 1;
2943
        path[input_path_len] = '\0';
2944
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2945
    }
2946
#ifdef DEBUG_COMPLETION
2947
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2948
                   input, path, file_prefix);
2949
#endif
2950
    ffs = opendir(path);
2951
    if (!ffs)
2952
        return;
2953
    for(;;) {
2954
        struct stat sb;
2955
        d = readdir(ffs);
2956
        if (!d)
2957
            break;
2958
        if (strstart(d->d_name, file_prefix, NULL)) {
2959
            memcpy(file, input, input_path_len);
2960
            if (input_path_len < sizeof(file))
2961
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2962
                        d->d_name);
2963
            /* stat the file to find out if it's a directory.
2964
             * In that case add a slash to speed up typing long paths
2965
             */
2966
            stat(file, &sb);
2967
            if(S_ISDIR(sb.st_mode))
2968
                pstrcat(file, sizeof(file), "/");
2969
            readline_add_completion(cur_mon->rs, file);
2970
        }
2971
    }
2972
    closedir(ffs);
2973
}
2974

    
2975
static void block_completion_it(void *opaque, BlockDriverState *bs)
2976
{
2977
    const char *name = bdrv_get_device_name(bs);
2978
    const char *input = opaque;
2979

    
2980
    if (input[0] == '\0' ||
2981
        !strncmp(name, (char *)input, strlen(input))) {
2982
        readline_add_completion(cur_mon->rs, name);
2983
    }
2984
}
2985

    
2986
/* NOTE: this parser is an approximate form of the real command parser */
2987
static void parse_cmdline(const char *cmdline,
2988
                         int *pnb_args, char **args)
2989
{
2990
    const char *p;
2991
    int nb_args, ret;
2992
    char buf[1024];
2993

    
2994
    p = cmdline;
2995
    nb_args = 0;
2996
    for(;;) {
2997
        while (qemu_isspace(*p))
2998
            p++;
2999
        if (*p == '\0')
3000
            break;
3001
        if (nb_args >= MAX_ARGS)
3002
            break;
3003
        ret = get_str(buf, sizeof(buf), &p);
3004
        args[nb_args] = qemu_strdup(buf);
3005
        nb_args++;
3006
        if (ret < 0)
3007
            break;
3008
    }
3009
    *pnb_args = nb_args;
3010
}
3011

    
3012
static void monitor_find_completion(const char *cmdline)
3013
{
3014
    const char *cmdname;
3015
    char *args[MAX_ARGS];
3016
    int nb_args, i, len;
3017
    const char *ptype, *str;
3018
    const mon_cmd_t *cmd;
3019
    const KeyDef *key;
3020

    
3021
    parse_cmdline(cmdline, &nb_args, args);
3022
#ifdef DEBUG_COMPLETION
3023
    for(i = 0; i < nb_args; i++) {
3024
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3025
    }
3026
#endif
3027

    
3028
    /* if the line ends with a space, it means we want to complete the
3029
       next arg */
3030
    len = strlen(cmdline);
3031
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3032
        if (nb_args >= MAX_ARGS)
3033
            return;
3034
        args[nb_args++] = qemu_strdup("");
3035
    }
3036
    if (nb_args <= 1) {
3037
        /* command completion */
3038
        if (nb_args == 0)
3039
            cmdname = "";
3040
        else
3041
            cmdname = args[0];
3042
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3043
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3044
            cmd_completion(cmdname, cmd->name);
3045
        }
3046
    } else {
3047
        /* find the command */
3048
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3049
            if (compare_cmd(args[0], cmd->name))
3050
                goto found;
3051
        }
3052
        return;
3053
    found:
3054
        ptype = cmd->args_type;
3055
        for(i = 0; i < nb_args - 2; i++) {
3056
            if (*ptype != '\0') {
3057
                ptype++;
3058
                while (*ptype == '?')
3059
                    ptype++;
3060
            }
3061
        }
3062
        str = args[nb_args - 1];
3063
        switch(*ptype) {
3064
        case 'F':
3065
            /* file completion */
3066
            readline_set_completion_index(cur_mon->rs, strlen(str));
3067
            file_completion(str);
3068
            break;
3069
        case 'B':
3070
            /* block device name completion */
3071
            readline_set_completion_index(cur_mon->rs, strlen(str));
3072
            bdrv_iterate(block_completion_it, (void *)str);
3073
            break;
3074
        case 's':
3075
            /* XXX: more generic ? */
3076
            if (!strcmp(cmd->name, "info")) {
3077
                readline_set_completion_index(cur_mon->rs, strlen(str));
3078
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3079
                    cmd_completion(str, cmd->name);
3080
                }
3081
            } else if (!strcmp(cmd->name, "sendkey")) {
3082
                char *sep = strrchr(str, '-');
3083
                if (sep)
3084
                    str = sep + 1;
3085
                readline_set_completion_index(cur_mon->rs, strlen(str));
3086
                for(key = key_defs; key->name != NULL; key++) {
3087
                    cmd_completion(str, key->name);
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
                }
3094
            }
3095
            break;
3096
        default:
3097
            break;
3098
        }
3099
    }
3100
    for(i = 0; i < nb_args; i++)
3101
        qemu_free(args[i]);
3102
}
3103

    
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;
3194

    
3195
    if (is_first_init) {
3196
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3197
        is_first_init = 0;
3198
    }
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);
3256
}