Statistics
| Branch: | Revision:

root / monitor.c @ d7f9b689

History | View | Annotate | Download (90.8 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 "hw/loader.h"
33
#include "gdbstub.h"
34
#include "net.h"
35
#include "qemu-char.h"
36
#include "sysemu.h"
37
#include "monitor.h"
38
#include "readline.h"
39
#include "console.h"
40
#include "block.h"
41
#include "audio/audio.h"
42
#include "disas.h"
43
#include "balloon.h"
44
#include "qemu-timer.h"
45
#include "migration.h"
46
#include "kvm.h"
47
#include "acl.h"
48
#include "qint.h"
49
#include "qdict.h"
50
#include "qstring.h"
51

    
52
//#define DEBUG
53
//#define DEBUG_COMPLETION
54

    
55
/*
56
 * Supported types:
57
 *
58
 * 'F'          filename
59
 * 'B'          block device name
60
 * 's'          string (accept optional quote)
61
 * 'i'          32 bit integer
62
 * 'l'          target long (32 or 64 bit)
63
 * '/'          optional gdb-like print format (like "/10x")
64
 *
65
 * '?'          optional type (for all types, except '/')
66
 * '.'          other form of optional type (for 'i' and 'l')
67
 * '-'          optional parameter (eg. '-f')
68
 *
69
 */
70

    
71
typedef struct mon_cmd_t {
72
    const char *name;
73
    const char *args_type;
74
    void *handler;
75
    const char *params;
76
    const char *help;
77
} mon_cmd_t;
78

    
79
/* file descriptors passed via SCM_RIGHTS */
80
typedef struct mon_fd_t mon_fd_t;
81
struct mon_fd_t {
82
    char *name;
83
    int fd;
84
    QLIST_ENTRY(mon_fd_t) next;
85
};
86

    
87
struct Monitor {
88
    CharDriverState *chr;
89
    int mux_out;
90
    int reset_seen;
91
    int flags;
92
    int suspend_cnt;
93
    uint8_t outbuf[1024];
94
    int outbuf_index;
95
    ReadLineState *rs;
96
    CPUState *mon_cpu;
97
    BlockDriverCompletionFunc *password_completion_cb;
98
    void *password_opaque;
99
    QLIST_HEAD(,mon_fd_t) fds;
100
    QLIST_ENTRY(Monitor) entry;
101
};
102

    
103
static QLIST_HEAD(mon_list, Monitor) mon_list;
104

    
105
static const mon_cmd_t mon_cmds[];
106
static const mon_cmd_t info_cmds[];
107

    
108
Monitor *cur_mon = NULL;
109

    
110
static void monitor_command_cb(Monitor *mon, const char *cmdline,
111
                               void *opaque);
112

    
113
static void monitor_read_command(Monitor *mon, int show_prompt)
114
{
115
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
116
    if (show_prompt)
117
        readline_show_prompt(mon->rs);
118
}
119

    
120
static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
121
                                 void *opaque)
122
{
123
    if (mon->rs) {
124
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
125
        /* prompt is printed on return from the command handler */
126
        return 0;
127
    } else {
128
        monitor_printf(mon, "terminal does not support password prompting\n");
129
        return -ENOTTY;
130
    }
131
}
132

    
133
void monitor_flush(Monitor *mon)
134
{
135
    if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
136
        qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
137
        mon->outbuf_index = 0;
138
    }
139
}
140

    
141
/* flush at every end of line or if the buffer is full */
142
static void monitor_puts(Monitor *mon, const char *str)
143
{
144
    char c;
145

    
146
    if (!mon)
147
        return;
148

    
149
    for(;;) {
150
        c = *str++;
151
        if (c == '\0')
152
            break;
153
        if (c == '\n')
154
            mon->outbuf[mon->outbuf_index++] = '\r';
155
        mon->outbuf[mon->outbuf_index++] = c;
156
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
157
            || c == '\n')
158
            monitor_flush(mon);
159
    }
160
}
161

    
162
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
163
{
164
    char buf[4096];
165
    vsnprintf(buf, sizeof(buf), fmt, ap);
166
    monitor_puts(mon, buf);
167
}
168

    
169
void monitor_printf(Monitor *mon, const char *fmt, ...)
170
{
171
    va_list ap;
172
    va_start(ap, fmt);
173
    monitor_vprintf(mon, fmt, ap);
174
    va_end(ap);
175
}
176

    
177
void monitor_print_filename(Monitor *mon, const char *filename)
178
{
179
    int i;
180

    
181
    for (i = 0; filename[i]; i++) {
182
        switch (filename[i]) {
183
        case ' ':
184
        case '"':
185
        case '\\':
186
            monitor_printf(mon, "\\%c", filename[i]);
187
            break;
188
        case '\t':
189
            monitor_printf(mon, "\\t");
190
            break;
191
        case '\r':
192
            monitor_printf(mon, "\\r");
193
            break;
194
        case '\n':
195
            monitor_printf(mon, "\\n");
196
            break;
197
        default:
198
            monitor_printf(mon, "%c", filename[i]);
199
            break;
200
        }
201
    }
202
}
203

    
204
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
205
{
206
    va_list ap;
207
    va_start(ap, fmt);
208
    monitor_vprintf((Monitor *)stream, fmt, ap);
209
    va_end(ap);
210
    return 0;
211
}
212

    
213
static int compare_cmd(const char *name, const char *list)
214
{
215
    const char *p, *pstart;
216
    int len;
217
    len = strlen(name);
218
    p = list;
219
    for(;;) {
220
        pstart = p;
221
        p = strchr(p, '|');
222
        if (!p)
223
            p = pstart + strlen(pstart);
224
        if ((p - pstart) == len && !memcmp(pstart, name, len))
225
            return 1;
226
        if (*p == '\0')
227
            break;
228
        p++;
229
    }
230
    return 0;
231
}
232

    
233
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
234
                          const char *prefix, const char *name)
235
{
236
    const mon_cmd_t *cmd;
237

    
238
    for(cmd = cmds; cmd->name != NULL; cmd++) {
239
        if (!name || !strcmp(name, cmd->name))
240
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
241
                           cmd->params, cmd->help);
242
    }
243
}
244

    
245
static void help_cmd(Monitor *mon, const char *name)
246
{
247
    if (name && !strcmp(name, "info")) {
248
        help_cmd_dump(mon, info_cmds, "info ", NULL);
249
    } else {
250
        help_cmd_dump(mon, mon_cmds, "", name);
251
        if (name && !strcmp(name, "log")) {
252
            const CPULogItem *item;
253
            monitor_printf(mon, "Log items (comma separated):\n");
254
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
255
            for(item = cpu_log_items; item->mask != 0; item++) {
256
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
257
            }
258
        }
259
    }
260
}
261

    
262
static void do_help_cmd(Monitor *mon, const QDict *qdict)
263
{
264
    help_cmd(mon, qdict_get_try_str(qdict, "name"));
265
}
266

    
267
static void do_commit(Monitor *mon, const QDict *qdict)
268
{
269
    int all_devices;
270
    DriveInfo *dinfo;
271
    const char *device = qdict_get_str(qdict, "device");
272

    
273
    all_devices = !strcmp(device, "all");
274
    QTAILQ_FOREACH(dinfo, &drives, next) {
275
        if (!all_devices)
276
            if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
277
                continue;
278
        bdrv_commit(dinfo->bdrv);
279
    }
280
}
281

    
282
static void do_info(Monitor *mon, const QDict *qdict)
283
{
284
    const mon_cmd_t *cmd;
285
    const char *item = qdict_get_try_str(qdict, "item");
286
    void (*handler)(Monitor *);
287

    
288
    if (!item)
289
        goto help;
290
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
291
        if (compare_cmd(item, cmd->name))
292
            goto found;
293
    }
294
 help:
295
    help_cmd(mon, "info");
296
    return;
297
 found:
298
    handler = cmd->handler;
299
    handler(mon);
300
}
301

    
302
static void do_info_version(Monitor *mon)
303
{
304
    monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
305
}
306

    
307
static void do_info_name(Monitor *mon)
308
{
309
    if (qemu_name)
310
        monitor_printf(mon, "%s\n", qemu_name);
311
}
312

    
313
#if defined(TARGET_I386)
314
static void do_info_hpet(Monitor *mon)
315
{
316
    monitor_printf(mon, "HPET is %s by QEMU\n",
317
                   (no_hpet) ? "disabled" : "enabled");
318
}
319
#endif
320

    
321
static void do_info_uuid(Monitor *mon)
322
{
323
    monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
324
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
325
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
326
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
327
                   qemu_uuid[14], qemu_uuid[15]);
328
}
329

    
330
/* get the current CPU defined by the user */
331
static int mon_set_cpu(int cpu_index)
332
{
333
    CPUState *env;
334

    
335
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
336
        if (env->cpu_index == cpu_index) {
337
            cur_mon->mon_cpu = env;
338
            return 0;
339
        }
340
    }
341
    return -1;
342
}
343

    
344
static CPUState *mon_get_cpu(void)
345
{
346
    if (!cur_mon->mon_cpu) {
347
        mon_set_cpu(0);
348
    }
349
    cpu_synchronize_state(cur_mon->mon_cpu);
350
    return cur_mon->mon_cpu;
351
}
352

    
353
static void do_info_registers(Monitor *mon)
354
{
355
    CPUState *env;
356
    env = mon_get_cpu();
357
    if (!env)
358
        return;
359
#ifdef TARGET_I386
360
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
361
                   X86_DUMP_FPU);
362
#else
363
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
364
                   0);
365
#endif
366
}
367

    
368
static void do_info_cpus(Monitor *mon)
369
{
370
    CPUState *env;
371

    
372
    /* just to set the default cpu if not already done */
373
    mon_get_cpu();
374

    
375
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
376
        cpu_synchronize_state(env);
377
        monitor_printf(mon, "%c CPU #%d:",
378
                       (env == mon->mon_cpu) ? '*' : ' ',
379
                       env->cpu_index);
380
#if defined(TARGET_I386)
381
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
382
                       env->eip + env->segs[R_CS].base);
383
#elif defined(TARGET_PPC)
384
        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
385
#elif defined(TARGET_SPARC)
386
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
387
                       env->pc, env->npc);
388
#elif defined(TARGET_MIPS)
389
        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
390
#endif
391
        if (env->halted)
392
            monitor_printf(mon, " (halted)");
393
        monitor_printf(mon, "\n");
394
    }
395
}
396

    
397
static void do_cpu_set(Monitor *mon, const QDict *qdict)
398
{
399
    int index = qdict_get_int(qdict, "index");
400
    if (mon_set_cpu(index) < 0)
401
        monitor_printf(mon, "Invalid CPU index\n");
402
}
403

    
404
static void do_info_jit(Monitor *mon)
405
{
406
    dump_exec_info((FILE *)mon, monitor_fprintf);
407
}
408

    
409
static void do_info_history(Monitor *mon)
410
{
411
    int i;
412
    const char *str;
413

    
414
    if (!mon->rs)
415
        return;
416
    i = 0;
417
    for(;;) {
418
        str = readline_get_history(mon->rs, i);
419
        if (!str)
420
            break;
421
        monitor_printf(mon, "%d: '%s'\n", i, str);
422
        i++;
423
    }
424
}
425

    
426
#if defined(TARGET_PPC)
427
/* XXX: not implemented in other targets */
428
static void do_info_cpu_stats(Monitor *mon)
429
{
430
    CPUState *env;
431

    
432
    env = mon_get_cpu();
433
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
434
}
435
#endif
436

    
437
static void do_quit(Monitor *mon, const QDict *qdict)
438
{
439
    exit(0);
440
}
441

    
442
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
443
{
444
    if (bdrv_is_inserted(bs)) {
445
        if (!force) {
446
            if (!bdrv_is_removable(bs)) {
447
                monitor_printf(mon, "device is not removable\n");
448
                return -1;
449
            }
450
            if (bdrv_is_locked(bs)) {
451
                monitor_printf(mon, "device is locked\n");
452
                return -1;
453
            }
454
        }
455
        bdrv_close(bs);
456
    }
457
    return 0;
458
}
459

    
460
static void do_eject(Monitor *mon, const QDict *qdict)
461
{
462
    BlockDriverState *bs;
463
    int force = qdict_get_int(qdict, "force");
464
    const char *filename = qdict_get_str(qdict, "filename");
465

    
466
    bs = bdrv_find(filename);
467
    if (!bs) {
468
        monitor_printf(mon, "device not found\n");
469
        return;
470
    }
471
    eject_device(mon, bs, force);
472
}
473

    
474
static void do_change_block(Monitor *mon, const char *device,
475
                            const char *filename, const char *fmt)
476
{
477
    BlockDriverState *bs;
478
    BlockDriver *drv = NULL;
479

    
480
    bs = bdrv_find(device);
481
    if (!bs) {
482
        monitor_printf(mon, "device not found\n");
483
        return;
484
    }
485
    if (fmt) {
486
        drv = bdrv_find_format(fmt);
487
        if (!drv) {
488
            monitor_printf(mon, "invalid format %s\n", fmt);
489
            return;
490
        }
491
    }
492
    if (eject_device(mon, bs, 0) < 0)
493
        return;
494
    bdrv_open2(bs, filename, 0, drv);
495
    monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
496
}
497

    
498
static void change_vnc_password_cb(Monitor *mon, const char *password,
499
                                   void *opaque)
500
{
501
    if (vnc_display_password(NULL, password) < 0)
502
        monitor_printf(mon, "could not set VNC server password\n");
503

    
504
    monitor_read_command(mon, 1);
505
}
506

    
507
static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
508
{
509
    if (strcmp(target, "passwd") == 0 ||
510
        strcmp(target, "password") == 0) {
511
        if (arg) {
512
            char password[9];
513
            strncpy(password, arg, sizeof(password));
514
            password[sizeof(password) - 1] = '\0';
515
            change_vnc_password_cb(mon, password, NULL);
516
        } else {
517
            monitor_read_password(mon, change_vnc_password_cb, NULL);
518
        }
519
    } else {
520
        if (vnc_display_open(NULL, target) < 0)
521
            monitor_printf(mon, "could not start VNC server on %s\n", target);
522
    }
523
}
524

    
525
static void do_change(Monitor *mon, const QDict *qdict)
526
{
527
    const char *device = qdict_get_str(qdict, "device");
528
    const char *target = qdict_get_str(qdict, "target");
529
    const char *arg = qdict_get_try_str(qdict, "arg");
530
    if (strcmp(device, "vnc") == 0) {
531
        do_change_vnc(mon, target, arg);
532
    } else {
533
        do_change_block(mon, device, target, arg);
534
    }
535
}
536

    
537
static void do_screen_dump(Monitor *mon, const QDict *qdict)
538
{
539
    vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
540
}
541

    
542
static void do_logfile(Monitor *mon, const QDict *qdict)
543
{
544
    cpu_set_log_filename(qdict_get_str(qdict, "filename"));
545
}
546

    
547
static void do_log(Monitor *mon, const QDict *qdict)
548
{
549
    int mask;
550
    const char *items = qdict_get_str(qdict, "items");
551

    
552
    if (!strcmp(items, "none")) {
553
        mask = 0;
554
    } else {
555
        mask = cpu_str_to_log_mask(items);
556
        if (!mask) {
557
            help_cmd(mon, "log");
558
            return;
559
        }
560
    }
561
    cpu_set_log(mask);
562
}
563

    
564
static void do_singlestep(Monitor *mon, const QDict *qdict)
565
{
566
    const char *option = qdict_get_try_str(qdict, "option");
567
    if (!option || !strcmp(option, "on")) {
568
        singlestep = 1;
569
    } else if (!strcmp(option, "off")) {
570
        singlestep = 0;
571
    } else {
572
        monitor_printf(mon, "unexpected option %s\n", option);
573
    }
574
}
575

    
576
static void do_stop(Monitor *mon, const QDict *qdict)
577
{
578
    vm_stop(EXCP_INTERRUPT);
579
}
580

    
581
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
582

    
583
struct bdrv_iterate_context {
584
    Monitor *mon;
585
    int err;
586
};
587

    
588
static void do_cont(Monitor *mon, const QDict *qdict)
589
{
590
    struct bdrv_iterate_context context = { mon, 0 };
591

    
592
    bdrv_iterate(encrypted_bdrv_it, &context);
593
    /* only resume the vm if all keys are set and valid */
594
    if (!context.err)
595
        vm_start();
596
}
597

    
598
static void bdrv_key_cb(void *opaque, int err)
599
{
600
    Monitor *mon = opaque;
601

    
602
    /* another key was set successfully, retry to continue */
603
    if (!err)
604
        do_cont(mon, NULL);
605
}
606

    
607
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
608
{
609
    struct bdrv_iterate_context *context = opaque;
610

    
611
    if (!context->err && bdrv_key_required(bs)) {
612
        context->err = -EBUSY;
613
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
614
                                    context->mon);
615
    }
616
}
617

    
618
static void do_gdbserver(Monitor *mon, const QDict *qdict)
619
{
620
    const char *device = qdict_get_try_str(qdict, "device");
621
    if (!device)
622
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
623
    if (gdbserver_start(device) < 0) {
624
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
625
                       device);
626
    } else if (strcmp(device, "none") == 0) {
627
        monitor_printf(mon, "Disabled gdbserver\n");
628
    } else {
629
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
630
                       device);
631
    }
632
}
633

    
634
static void do_watchdog_action(Monitor *mon, const QDict *qdict)
635
{
636
    const char *action = qdict_get_str(qdict, "action");
637
    if (select_watchdog_action(action) == -1) {
638
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
639
    }
640
}
641

    
642
static void monitor_printc(Monitor *mon, int c)
643
{
644
    monitor_printf(mon, "'");
645
    switch(c) {
646
    case '\'':
647
        monitor_printf(mon, "\\'");
648
        break;
649
    case '\\':
650
        monitor_printf(mon, "\\\\");
651
        break;
652
    case '\n':
653
        monitor_printf(mon, "\\n");
654
        break;
655
    case '\r':
656
        monitor_printf(mon, "\\r");
657
        break;
658
    default:
659
        if (c >= 32 && c <= 126) {
660
            monitor_printf(mon, "%c", c);
661
        } else {
662
            monitor_printf(mon, "\\x%02x", c);
663
        }
664
        break;
665
    }
666
    monitor_printf(mon, "'");
667
}
668

    
669
static void memory_dump(Monitor *mon, int count, int format, int wsize,
670
                        target_phys_addr_t addr, int is_physical)
671
{
672
    CPUState *env;
673
    int nb_per_line, l, line_size, i, max_digits, len;
674
    uint8_t buf[16];
675
    uint64_t v;
676

    
677
    if (format == 'i') {
678
        int flags;
679
        flags = 0;
680
        env = mon_get_cpu();
681
        if (!env && !is_physical)
682
            return;
683
#ifdef TARGET_I386
684
        if (wsize == 2) {
685
            flags = 1;
686
        } else if (wsize == 4) {
687
            flags = 0;
688
        } else {
689
            /* as default we use the current CS size */
690
            flags = 0;
691
            if (env) {
692
#ifdef TARGET_X86_64
693
                if ((env->efer & MSR_EFER_LMA) &&
694
                    (env->segs[R_CS].flags & DESC_L_MASK))
695
                    flags = 2;
696
                else
697
#endif
698
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
699
                    flags = 1;
700
            }
701
        }
702
#endif
703
        monitor_disas(mon, env, addr, count, is_physical, flags);
704
        return;
705
    }
706

    
707
    len = wsize * count;
708
    if (wsize == 1)
709
        line_size = 8;
710
    else
711
        line_size = 16;
712
    nb_per_line = line_size / wsize;
713
    max_digits = 0;
714

    
715
    switch(format) {
716
    case 'o':
717
        max_digits = (wsize * 8 + 2) / 3;
718
        break;
719
    default:
720
    case 'x':
721
        max_digits = (wsize * 8) / 4;
722
        break;
723
    case 'u':
724
    case 'd':
725
        max_digits = (wsize * 8 * 10 + 32) / 33;
726
        break;
727
    case 'c':
728
        wsize = 1;
729
        break;
730
    }
731

    
732
    while (len > 0) {
733
        if (is_physical)
734
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
735
        else
736
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
737
        l = len;
738
        if (l > line_size)
739
            l = line_size;
740
        if (is_physical) {
741
            cpu_physical_memory_rw(addr, buf, l, 0);
742
        } else {
743
            env = mon_get_cpu();
744
            if (!env)
745
                break;
746
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
747
                monitor_printf(mon, " Cannot access memory\n");
748
                break;
749
            }
750
        }
751
        i = 0;
752
        while (i < l) {
753
            switch(wsize) {
754
            default:
755
            case 1:
756
                v = ldub_raw(buf + i);
757
                break;
758
            case 2:
759
                v = lduw_raw(buf + i);
760
                break;
761
            case 4:
762
                v = (uint32_t)ldl_raw(buf + i);
763
                break;
764
            case 8:
765
                v = ldq_raw(buf + i);
766
                break;
767
            }
768
            monitor_printf(mon, " ");
769
            switch(format) {
770
            case 'o':
771
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
772
                break;
773
            case 'x':
774
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
775
                break;
776
            case 'u':
777
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
778
                break;
779
            case 'd':
780
                monitor_printf(mon, "%*" PRId64, max_digits, v);
781
                break;
782
            case 'c':
783
                monitor_printc(mon, v);
784
                break;
785
            }
786
            i += wsize;
787
        }
788
        monitor_printf(mon, "\n");
789
        addr += l;
790
        len -= l;
791
    }
792
}
793

    
794
static void do_memory_dump(Monitor *mon, const QDict *qdict)
795
{
796
    int count = qdict_get_int(qdict, "count");
797
    int format = qdict_get_int(qdict, "format");
798
    int size = qdict_get_int(qdict, "size");
799
    target_long addr = qdict_get_int(qdict, "addr");
800

    
801
    memory_dump(mon, count, format, size, addr, 0);
802
}
803

    
804
static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
805
{
806
    int count = qdict_get_int(qdict, "count");
807
    int format = qdict_get_int(qdict, "format");
808
    int size = qdict_get_int(qdict, "size");
809
    target_phys_addr_t addr = qdict_get_int(qdict, "addr");
810

    
811
    memory_dump(mon, count, format, size, addr, 1);
812
}
813

    
814
static void do_print(Monitor *mon, const QDict *qdict)
815
{
816
    int format = qdict_get_int(qdict, "format");
817
    target_phys_addr_t val = qdict_get_int(qdict, "val");
818

    
819
#if TARGET_PHYS_ADDR_BITS == 32
820
    switch(format) {
821
    case 'o':
822
        monitor_printf(mon, "%#o", val);
823
        break;
824
    case 'x':
825
        monitor_printf(mon, "%#x", val);
826
        break;
827
    case 'u':
828
        monitor_printf(mon, "%u", val);
829
        break;
830
    default:
831
    case 'd':
832
        monitor_printf(mon, "%d", val);
833
        break;
834
    case 'c':
835
        monitor_printc(mon, val);
836
        break;
837
    }
838
#else
839
    switch(format) {
840
    case 'o':
841
        monitor_printf(mon, "%#" PRIo64, val);
842
        break;
843
    case 'x':
844
        monitor_printf(mon, "%#" PRIx64, val);
845
        break;
846
    case 'u':
847
        monitor_printf(mon, "%" PRIu64, val);
848
        break;
849
    default:
850
    case 'd':
851
        monitor_printf(mon, "%" PRId64, val);
852
        break;
853
    case 'c':
854
        monitor_printc(mon, val);
855
        break;
856
    }
857
#endif
858
    monitor_printf(mon, "\n");
859
}
860

    
861
static void do_memory_save(Monitor *mon, const QDict *qdict)
862
{
863
    FILE *f;
864
    uint32_t size = qdict_get_int(qdict, "size");
865
    const char *filename = qdict_get_str(qdict, "filename");
866
    target_long addr = qdict_get_int(qdict, "val");
867
    uint32_t l;
868
    CPUState *env;
869
    uint8_t buf[1024];
870

    
871
    env = mon_get_cpu();
872
    if (!env)
873
        return;
874

    
875
    f = fopen(filename, "wb");
876
    if (!f) {
877
        monitor_printf(mon, "could not open '%s'\n", filename);
878
        return;
879
    }
880
    while (size != 0) {
881
        l = sizeof(buf);
882
        if (l > size)
883
            l = size;
884
        cpu_memory_rw_debug(env, addr, buf, l, 0);
885
        fwrite(buf, 1, l, f);
886
        addr += l;
887
        size -= l;
888
    }
889
    fclose(f);
890
}
891

    
892
static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
893
{
894
    FILE *f;
895
    uint32_t l;
896
    uint8_t buf[1024];
897
    uint32_t size = qdict_get_int(qdict, "size");
898
    const char *filename = qdict_get_str(qdict, "filename");
899
    target_phys_addr_t addr = qdict_get_int(qdict, "val");
900

    
901
    f = fopen(filename, "wb");
902
    if (!f) {
903
        monitor_printf(mon, "could not open '%s'\n", filename);
904
        return;
905
    }
906
    while (size != 0) {
907
        l = sizeof(buf);
908
        if (l > size)
909
            l = size;
910
        cpu_physical_memory_rw(addr, buf, l, 0);
911
        fwrite(buf, 1, l, f);
912
        fflush(f);
913
        addr += l;
914
        size -= l;
915
    }
916
    fclose(f);
917
}
918

    
919
static void do_sum(Monitor *mon, const QDict *qdict)
920
{
921
    uint32_t addr;
922
    uint8_t buf[1];
923
    uint16_t sum;
924
    uint32_t start = qdict_get_int(qdict, "start");
925
    uint32_t size = qdict_get_int(qdict, "size");
926

    
927
    sum = 0;
928
    for(addr = start; addr < (start + size); addr++) {
929
        cpu_physical_memory_rw(addr, buf, 1, 0);
930
        /* BSD sum algorithm ('sum' Unix command) */
931
        sum = (sum >> 1) | (sum << 15);
932
        sum += buf[0];
933
    }
934
    monitor_printf(mon, "%05d\n", sum);
935
}
936

    
937
typedef struct {
938
    int keycode;
939
    const char *name;
940
} KeyDef;
941

    
942
static const KeyDef key_defs[] = {
943
    { 0x2a, "shift" },
944
    { 0x36, "shift_r" },
945

    
946
    { 0x38, "alt" },
947
    { 0xb8, "alt_r" },
948
    { 0x64, "altgr" },
949
    { 0xe4, "altgr_r" },
950
    { 0x1d, "ctrl" },
951
    { 0x9d, "ctrl_r" },
952

    
953
    { 0xdd, "menu" },
954

    
955
    { 0x01, "esc" },
956

    
957
    { 0x02, "1" },
958
    { 0x03, "2" },
959
    { 0x04, "3" },
960
    { 0x05, "4" },
961
    { 0x06, "5" },
962
    { 0x07, "6" },
963
    { 0x08, "7" },
964
    { 0x09, "8" },
965
    { 0x0a, "9" },
966
    { 0x0b, "0" },
967
    { 0x0c, "minus" },
968
    { 0x0d, "equal" },
969
    { 0x0e, "backspace" },
970

    
971
    { 0x0f, "tab" },
972
    { 0x10, "q" },
973
    { 0x11, "w" },
974
    { 0x12, "e" },
975
    { 0x13, "r" },
976
    { 0x14, "t" },
977
    { 0x15, "y" },
978
    { 0x16, "u" },
979
    { 0x17, "i" },
980
    { 0x18, "o" },
981
    { 0x19, "p" },
982

    
983
    { 0x1c, "ret" },
984

    
985
    { 0x1e, "a" },
986
    { 0x1f, "s" },
987
    { 0x20, "d" },
988
    { 0x21, "f" },
989
    { 0x22, "g" },
990
    { 0x23, "h" },
991
    { 0x24, "j" },
992
    { 0x25, "k" },
993
    { 0x26, "l" },
994

    
995
    { 0x2c, "z" },
996
    { 0x2d, "x" },
997
    { 0x2e, "c" },
998
    { 0x2f, "v" },
999
    { 0x30, "b" },
1000
    { 0x31, "n" },
1001
    { 0x32, "m" },
1002
    { 0x33, "comma" },
1003
    { 0x34, "dot" },
1004
    { 0x35, "slash" },
1005

    
1006
    { 0x37, "asterisk" },
1007

    
1008
    { 0x39, "spc" },
1009
    { 0x3a, "caps_lock" },
1010
    { 0x3b, "f1" },
1011
    { 0x3c, "f2" },
1012
    { 0x3d, "f3" },
1013
    { 0x3e, "f4" },
1014
    { 0x3f, "f5" },
1015
    { 0x40, "f6" },
1016
    { 0x41, "f7" },
1017
    { 0x42, "f8" },
1018
    { 0x43, "f9" },
1019
    { 0x44, "f10" },
1020
    { 0x45, "num_lock" },
1021
    { 0x46, "scroll_lock" },
1022

    
1023
    { 0xb5, "kp_divide" },
1024
    { 0x37, "kp_multiply" },
1025
    { 0x4a, "kp_subtract" },
1026
    { 0x4e, "kp_add" },
1027
    { 0x9c, "kp_enter" },
1028
    { 0x53, "kp_decimal" },
1029
    { 0x54, "sysrq" },
1030

    
1031
    { 0x52, "kp_0" },
1032
    { 0x4f, "kp_1" },
1033
    { 0x50, "kp_2" },
1034
    { 0x51, "kp_3" },
1035
    { 0x4b, "kp_4" },
1036
    { 0x4c, "kp_5" },
1037
    { 0x4d, "kp_6" },
1038
    { 0x47, "kp_7" },
1039
    { 0x48, "kp_8" },
1040
    { 0x49, "kp_9" },
1041

    
1042
    { 0x56, "<" },
1043

    
1044
    { 0x57, "f11" },
1045
    { 0x58, "f12" },
1046

    
1047
    { 0xb7, "print" },
1048

    
1049
    { 0xc7, "home" },
1050
    { 0xc9, "pgup" },
1051
    { 0xd1, "pgdn" },
1052
    { 0xcf, "end" },
1053

    
1054
    { 0xcb, "left" },
1055
    { 0xc8, "up" },
1056
    { 0xd0, "down" },
1057
    { 0xcd, "right" },
1058

    
1059
    { 0xd2, "insert" },
1060
    { 0xd3, "delete" },
1061
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1062
    { 0xf0, "stop" },
1063
    { 0xf1, "again" },
1064
    { 0xf2, "props" },
1065
    { 0xf3, "undo" },
1066
    { 0xf4, "front" },
1067
    { 0xf5, "copy" },
1068
    { 0xf6, "open" },
1069
    { 0xf7, "paste" },
1070
    { 0xf8, "find" },
1071
    { 0xf9, "cut" },
1072
    { 0xfa, "lf" },
1073
    { 0xfb, "help" },
1074
    { 0xfc, "meta_l" },
1075
    { 0xfd, "meta_r" },
1076
    { 0xfe, "compose" },
1077
#endif
1078
    { 0, NULL },
1079
};
1080

    
1081
static int get_keycode(const char *key)
1082
{
1083
    const KeyDef *p;
1084
    char *endp;
1085
    int ret;
1086

    
1087
    for(p = key_defs; p->name != NULL; p++) {
1088
        if (!strcmp(key, p->name))
1089
            return p->keycode;
1090
    }
1091
    if (strstart(key, "0x", NULL)) {
1092
        ret = strtoul(key, &endp, 0);
1093
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1094
            return ret;
1095
    }
1096
    return -1;
1097
}
1098

    
1099
#define MAX_KEYCODES 16
1100
static uint8_t keycodes[MAX_KEYCODES];
1101
static int nb_pending_keycodes;
1102
static QEMUTimer *key_timer;
1103

    
1104
static void release_keys(void *opaque)
1105
{
1106
    int keycode;
1107

    
1108
    while (nb_pending_keycodes > 0) {
1109
        nb_pending_keycodes--;
1110
        keycode = keycodes[nb_pending_keycodes];
1111
        if (keycode & 0x80)
1112
            kbd_put_keycode(0xe0);
1113
        kbd_put_keycode(keycode | 0x80);
1114
    }
1115
}
1116

    
1117
static void do_sendkey(Monitor *mon, const QDict *qdict)
1118
{
1119
    char keyname_buf[16];
1120
    char *separator;
1121
    int keyname_len, keycode, i;
1122
    const char *string = qdict_get_str(qdict, "string");
1123
    int has_hold_time = qdict_haskey(qdict, "hold_time");
1124
    int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1125

    
1126
    if (nb_pending_keycodes > 0) {
1127
        qemu_del_timer(key_timer);
1128
        release_keys(NULL);
1129
    }
1130
    if (!has_hold_time)
1131
        hold_time = 100;
1132
    i = 0;
1133
    while (1) {
1134
        separator = strchr(string, '-');
1135
        keyname_len = separator ? separator - string : strlen(string);
1136
        if (keyname_len > 0) {
1137
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1138
            if (keyname_len > sizeof(keyname_buf) - 1) {
1139
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1140
                return;
1141
            }
1142
            if (i == MAX_KEYCODES) {
1143
                monitor_printf(mon, "too many keys\n");
1144
                return;
1145
            }
1146
            keyname_buf[keyname_len] = 0;
1147
            keycode = get_keycode(keyname_buf);
1148
            if (keycode < 0) {
1149
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1150
                return;
1151
            }
1152
            keycodes[i++] = keycode;
1153
        }
1154
        if (!separator)
1155
            break;
1156
        string = separator + 1;
1157
    }
1158
    nb_pending_keycodes = i;
1159
    /* key down events */
1160
    for (i = 0; i < nb_pending_keycodes; i++) {
1161
        keycode = keycodes[i];
1162
        if (keycode & 0x80)
1163
            kbd_put_keycode(0xe0);
1164
        kbd_put_keycode(keycode & 0x7f);
1165
    }
1166
    /* delayed key up events */
1167
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1168
                   muldiv64(get_ticks_per_sec(), hold_time, 1000));
1169
}
1170

    
1171
static int mouse_button_state;
1172

    
1173
static void do_mouse_move(Monitor *mon, const QDict *qdict)
1174
{
1175
    int dx, dy, dz;
1176
    const char *dx_str = qdict_get_str(qdict, "dx_str");
1177
    const char *dy_str = qdict_get_str(qdict, "dy_str");
1178
    const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1179
    dx = strtol(dx_str, NULL, 0);
1180
    dy = strtol(dy_str, NULL, 0);
1181
    dz = 0;
1182
    if (dz_str)
1183
        dz = strtol(dz_str, NULL, 0);
1184
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1185
}
1186

    
1187
static void do_mouse_button(Monitor *mon, const QDict *qdict)
1188
{
1189
    int button_state = qdict_get_int(qdict, "button_state");
1190
    mouse_button_state = button_state;
1191
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1192
}
1193

    
1194
static void do_ioport_read(Monitor *mon, const QDict *qdict)
1195
{
1196
    int size = qdict_get_int(qdict, "size");
1197
    int addr = qdict_get_int(qdict, "addr");
1198
    int has_index = qdict_haskey(qdict, "index");
1199
    uint32_t val;
1200
    int suffix;
1201

    
1202
    if (has_index) {
1203
        int index = qdict_get_int(qdict, "index");
1204
        cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1205
        addr++;
1206
    }
1207
    addr &= 0xffff;
1208

    
1209
    switch(size) {
1210
    default:
1211
    case 1:
1212
        val = cpu_inb(addr);
1213
        suffix = 'b';
1214
        break;
1215
    case 2:
1216
        val = cpu_inw(addr);
1217
        suffix = 'w';
1218
        break;
1219
    case 4:
1220
        val = cpu_inl(addr);
1221
        suffix = 'l';
1222
        break;
1223
    }
1224
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1225
                   suffix, addr, size * 2, val);
1226
}
1227

    
1228
static void do_ioport_write(Monitor *mon, const QDict *qdict)
1229
{
1230
    int size = qdict_get_int(qdict, "size");
1231
    int addr = qdict_get_int(qdict, "addr");
1232
    int val = qdict_get_int(qdict, "val");
1233

    
1234
    addr &= IOPORTS_MASK;
1235

    
1236
    switch (size) {
1237
    default:
1238
    case 1:
1239
        cpu_outb(addr, val);
1240
        break;
1241
    case 2:
1242
        cpu_outw(addr, val);
1243
        break;
1244
    case 4:
1245
        cpu_outl(addr, val);
1246
        break;
1247
    }
1248
}
1249

    
1250
static void do_boot_set(Monitor *mon, const QDict *qdict)
1251
{
1252
    int res;
1253
    const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1254

    
1255
    res = qemu_boot_set(bootdevice);
1256
    if (res == 0) {
1257
        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1258
    } else if (res > 0) {
1259
        monitor_printf(mon, "setting boot device list failed\n");
1260
    } else {
1261
        monitor_printf(mon, "no function defined to set boot device list for "
1262
                       "this architecture\n");
1263
    }
1264
}
1265

    
1266
static void do_system_reset(Monitor *mon, const QDict *qdict)
1267
{
1268
    qemu_system_reset_request();
1269
}
1270

    
1271
static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1272
{
1273
    qemu_system_powerdown_request();
1274
}
1275

    
1276
#if defined(TARGET_I386)
1277
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1278
{
1279
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1280
                   addr,
1281
                   pte & mask,
1282
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
1283
                   pte & PG_PSE_MASK ? 'P' : '-',
1284
                   pte & PG_DIRTY_MASK ? 'D' : '-',
1285
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
1286
                   pte & PG_PCD_MASK ? 'C' : '-',
1287
                   pte & PG_PWT_MASK ? 'T' : '-',
1288
                   pte & PG_USER_MASK ? 'U' : '-',
1289
                   pte & PG_RW_MASK ? 'W' : '-');
1290
}
1291

    
1292
static void tlb_info(Monitor *mon)
1293
{
1294
    CPUState *env;
1295
    int l1, l2;
1296
    uint32_t pgd, pde, pte;
1297

    
1298
    env = mon_get_cpu();
1299
    if (!env)
1300
        return;
1301

    
1302
    if (!(env->cr[0] & CR0_PG_MASK)) {
1303
        monitor_printf(mon, "PG disabled\n");
1304
        return;
1305
    }
1306
    pgd = env->cr[3] & ~0xfff;
1307
    for(l1 = 0; l1 < 1024; l1++) {
1308
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1309
        pde = le32_to_cpu(pde);
1310
        if (pde & PG_PRESENT_MASK) {
1311
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1312
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1313
            } else {
1314
                for(l2 = 0; l2 < 1024; l2++) {
1315
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1316
                                             (uint8_t *)&pte, 4);
1317
                    pte = le32_to_cpu(pte);
1318
                    if (pte & PG_PRESENT_MASK) {
1319
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1320
                                  pte & ~PG_PSE_MASK,
1321
                                  ~0xfff);
1322
                    }
1323
                }
1324
            }
1325
        }
1326
    }
1327
}
1328

    
1329
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1330
                      uint32_t end, int prot)
1331
{
1332
    int prot1;
1333
    prot1 = *plast_prot;
1334
    if (prot != prot1) {
1335
        if (*pstart != -1) {
1336
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1337
                           *pstart, end, end - *pstart,
1338
                           prot1 & PG_USER_MASK ? 'u' : '-',
1339
                           'r',
1340
                           prot1 & PG_RW_MASK ? 'w' : '-');
1341
        }
1342
        if (prot != 0)
1343
            *pstart = end;
1344
        else
1345
            *pstart = -1;
1346
        *plast_prot = prot;
1347
    }
1348
}
1349

    
1350
static void mem_info(Monitor *mon)
1351
{
1352
    CPUState *env;
1353
    int l1, l2, prot, last_prot;
1354
    uint32_t pgd, pde, pte, start, end;
1355

    
1356
    env = mon_get_cpu();
1357
    if (!env)
1358
        return;
1359

    
1360
    if (!(env->cr[0] & CR0_PG_MASK)) {
1361
        monitor_printf(mon, "PG disabled\n");
1362
        return;
1363
    }
1364
    pgd = env->cr[3] & ~0xfff;
1365
    last_prot = 0;
1366
    start = -1;
1367
    for(l1 = 0; l1 < 1024; l1++) {
1368
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1369
        pde = le32_to_cpu(pde);
1370
        end = l1 << 22;
1371
        if (pde & PG_PRESENT_MASK) {
1372
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1373
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1374
                mem_print(mon, &start, &last_prot, end, prot);
1375
            } else {
1376
                for(l2 = 0; l2 < 1024; l2++) {
1377
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1378
                                             (uint8_t *)&pte, 4);
1379
                    pte = le32_to_cpu(pte);
1380
                    end = (l1 << 22) + (l2 << 12);
1381
                    if (pte & PG_PRESENT_MASK) {
1382
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1383
                    } else {
1384
                        prot = 0;
1385
                    }
1386
                    mem_print(mon, &start, &last_prot, end, prot);
1387
                }
1388
            }
1389
        } else {
1390
            prot = 0;
1391
            mem_print(mon, &start, &last_prot, end, prot);
1392
        }
1393
    }
1394
}
1395
#endif
1396

    
1397
#if defined(TARGET_SH4)
1398

    
1399
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1400
{
1401
    monitor_printf(mon, " tlb%i:\t"
1402
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1403
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1404
                   "dirty=%hhu writethrough=%hhu\n",
1405
                   idx,
1406
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1407
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
1408
                   tlb->d, tlb->wt);
1409
}
1410

    
1411
static void tlb_info(Monitor *mon)
1412
{
1413
    CPUState *env = mon_get_cpu();
1414
    int i;
1415

    
1416
    monitor_printf (mon, "ITLB:\n");
1417
    for (i = 0 ; i < ITLB_SIZE ; i++)
1418
        print_tlb (mon, i, &env->itlb[i]);
1419
    monitor_printf (mon, "UTLB:\n");
1420
    for (i = 0 ; i < UTLB_SIZE ; i++)
1421
        print_tlb (mon, i, &env->utlb[i]);
1422
}
1423

    
1424
#endif
1425

    
1426
static void do_info_kvm(Monitor *mon)
1427
{
1428
#ifdef CONFIG_KVM
1429
    monitor_printf(mon, "kvm support: ");
1430
    if (kvm_enabled())
1431
        monitor_printf(mon, "enabled\n");
1432
    else
1433
        monitor_printf(mon, "disabled\n");
1434
#else
1435
    monitor_printf(mon, "kvm support: not compiled\n");
1436
#endif
1437
}
1438

    
1439
static void do_info_numa(Monitor *mon)
1440
{
1441
    int i;
1442
    CPUState *env;
1443

    
1444
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1445
    for (i = 0; i < nb_numa_nodes; i++) {
1446
        monitor_printf(mon, "node %d cpus:", i);
1447
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1448
            if (env->numa_node == i) {
1449
                monitor_printf(mon, " %d", env->cpu_index);
1450
            }
1451
        }
1452
        monitor_printf(mon, "\n");
1453
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1454
            node_mem[i] >> 20);
1455
    }
1456
}
1457

    
1458
#ifdef CONFIG_PROFILER
1459

    
1460
int64_t qemu_time;
1461
int64_t dev_time;
1462

    
1463
static void do_info_profile(Monitor *mon)
1464
{
1465
    int64_t total;
1466
    total = qemu_time;
1467
    if (total == 0)
1468
        total = 1;
1469
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1470
                   dev_time, dev_time / (double)get_ticks_per_sec());
1471
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1472
                   qemu_time, qemu_time / (double)get_ticks_per_sec());
1473
    qemu_time = 0;
1474
    dev_time = 0;
1475
}
1476
#else
1477
static void do_info_profile(Monitor *mon)
1478
{
1479
    monitor_printf(mon, "Internal profiler not compiled\n");
1480
}
1481
#endif
1482

    
1483
/* Capture support */
1484
static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1485

    
1486
static void do_info_capture(Monitor *mon)
1487
{
1488
    int i;
1489
    CaptureState *s;
1490

    
1491
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1492
        monitor_printf(mon, "[%d]: ", i);
1493
        s->ops.info (s->opaque);
1494
    }
1495
}
1496

    
1497
#ifdef HAS_AUDIO
1498
static void do_stop_capture(Monitor *mon, const QDict *qdict)
1499
{
1500
    int i;
1501
    int n = qdict_get_int(qdict, "n");
1502
    CaptureState *s;
1503

    
1504
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1505
        if (i == n) {
1506
            s->ops.destroy (s->opaque);
1507
            QLIST_REMOVE (s, entries);
1508
            qemu_free (s);
1509
            return;
1510
        }
1511
    }
1512
}
1513

    
1514
static void do_wav_capture(Monitor *mon, const QDict *qdict)
1515
{
1516
    const char *path = qdict_get_str(qdict, "path");
1517
    int has_freq = qdict_haskey(qdict, "freq");
1518
    int freq = qdict_get_try_int(qdict, "freq", -1);
1519
    int has_bits = qdict_haskey(qdict, "bits");
1520
    int bits = qdict_get_try_int(qdict, "bits", -1);
1521
    int has_channels = qdict_haskey(qdict, "nchannels");
1522
    int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1523
    CaptureState *s;
1524

    
1525
    s = qemu_mallocz (sizeof (*s));
1526

    
1527
    freq = has_freq ? freq : 44100;
1528
    bits = has_bits ? bits : 16;
1529
    nchannels = has_channels ? nchannels : 2;
1530

    
1531
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1532
        monitor_printf(mon, "Faied to add wave capture\n");
1533
        qemu_free (s);
1534
    }
1535
    QLIST_INSERT_HEAD (&capture_head, s, entries);
1536
}
1537
#endif
1538

    
1539
#if defined(TARGET_I386)
1540
static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1541
{
1542
    CPUState *env;
1543
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1544

    
1545
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1546
        if (env->cpu_index == cpu_index) {
1547
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1548
            break;
1549
        }
1550
}
1551
#endif
1552

    
1553
static void do_info_status(Monitor *mon)
1554
{
1555
    if (vm_running) {
1556
        if (singlestep) {
1557
            monitor_printf(mon, "VM status: running (single step mode)\n");
1558
        } else {
1559
            monitor_printf(mon, "VM status: running\n");
1560
        }
1561
    } else
1562
       monitor_printf(mon, "VM status: paused\n");
1563
}
1564

    
1565

    
1566
static void do_balloon(Monitor *mon, const QDict *qdict)
1567
{
1568
    int value = qdict_get_int(qdict, "value");
1569
    ram_addr_t target = value;
1570
    qemu_balloon(target << 20);
1571
}
1572

    
1573
static void do_info_balloon(Monitor *mon)
1574
{
1575
    ram_addr_t actual;
1576

    
1577
    actual = qemu_balloon_status();
1578
    if (kvm_enabled() && !kvm_has_sync_mmu())
1579
        monitor_printf(mon, "Using KVM without synchronous MMU, "
1580
                       "ballooning disabled\n");
1581
    else if (actual == 0)
1582
        monitor_printf(mon, "Ballooning not activated in VM\n");
1583
    else
1584
        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1585
}
1586

    
1587
static qemu_acl *find_acl(Monitor *mon, const char *name)
1588
{
1589
    qemu_acl *acl = qemu_acl_find(name);
1590

    
1591
    if (!acl) {
1592
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
1593
    }
1594
    return acl;
1595
}
1596

    
1597
static void do_acl_show(Monitor *mon, const QDict *qdict)
1598
{
1599
    const char *aclname = qdict_get_str(qdict, "aclname");
1600
    qemu_acl *acl = find_acl(mon, aclname);
1601
    qemu_acl_entry *entry;
1602
    int i = 0;
1603

    
1604
    if (acl) {
1605
        monitor_printf(mon, "policy: %s\n",
1606
                       acl->defaultDeny ? "deny" : "allow");
1607
        QTAILQ_FOREACH(entry, &acl->entries, next) {
1608
            i++;
1609
            monitor_printf(mon, "%d: %s %s\n", i,
1610
                           entry->deny ? "deny" : "allow", entry->match);
1611
        }
1612
    }
1613
}
1614

    
1615
static void do_acl_reset(Monitor *mon, const QDict *qdict)
1616
{
1617
    const char *aclname = qdict_get_str(qdict, "aclname");
1618
    qemu_acl *acl = find_acl(mon, aclname);
1619

    
1620
    if (acl) {
1621
        qemu_acl_reset(acl);
1622
        monitor_printf(mon, "acl: removed all rules\n");
1623
    }
1624
}
1625

    
1626
static void do_acl_policy(Monitor *mon, const QDict *qdict)
1627
{
1628
    const char *aclname = qdict_get_str(qdict, "aclname");
1629
    const char *policy = qdict_get_str(qdict, "policy");
1630
    qemu_acl *acl = find_acl(mon, aclname);
1631

    
1632
    if (acl) {
1633
        if (strcmp(policy, "allow") == 0) {
1634
            acl->defaultDeny = 0;
1635
            monitor_printf(mon, "acl: policy set to 'allow'\n");
1636
        } else if (strcmp(policy, "deny") == 0) {
1637
            acl->defaultDeny = 1;
1638
            monitor_printf(mon, "acl: policy set to 'deny'\n");
1639
        } else {
1640
            monitor_printf(mon, "acl: unknown policy '%s', "
1641
                           "expected 'deny' or 'allow'\n", policy);
1642
        }
1643
    }
1644
}
1645

    
1646
static void do_acl_add(Monitor *mon, const QDict *qdict)
1647
{
1648
    const char *aclname = qdict_get_str(qdict, "aclname");
1649
    const char *match = qdict_get_str(qdict, "match");
1650
    const char *policy = qdict_get_str(qdict, "policy");
1651
    int has_index = qdict_haskey(qdict, "index");
1652
    int index = qdict_get_try_int(qdict, "index", -1);
1653
    qemu_acl *acl = find_acl(mon, aclname);
1654
    int deny, ret;
1655

    
1656
    if (acl) {
1657
        if (strcmp(policy, "allow") == 0) {
1658
            deny = 0;
1659
        } else if (strcmp(policy, "deny") == 0) {
1660
            deny = 1;
1661
        } else {
1662
            monitor_printf(mon, "acl: unknown policy '%s', "
1663
                           "expected 'deny' or 'allow'\n", policy);
1664
            return;
1665
        }
1666
        if (has_index)
1667
            ret = qemu_acl_insert(acl, deny, match, index);
1668
        else
1669
            ret = qemu_acl_append(acl, deny, match);
1670
        if (ret < 0)
1671
            monitor_printf(mon, "acl: unable to add acl entry\n");
1672
        else
1673
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
1674
    }
1675
}
1676

    
1677
static void do_acl_remove(Monitor *mon, const QDict *qdict)
1678
{
1679
    const char *aclname = qdict_get_str(qdict, "aclname");
1680
    const char *match = qdict_get_str(qdict, "match");
1681
    qemu_acl *acl = find_acl(mon, aclname);
1682
    int ret;
1683

    
1684
    if (acl) {
1685
        ret = qemu_acl_remove(acl, match);
1686
        if (ret < 0)
1687
            monitor_printf(mon, "acl: no matching acl entry\n");
1688
        else
1689
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1690
    }
1691
}
1692

    
1693
#if defined(TARGET_I386)
1694
static void do_inject_mce(Monitor *mon, const QDict *qdict)
1695
{
1696
    CPUState *cenv;
1697
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1698
    int bank = qdict_get_int(qdict, "bank");
1699
    uint64_t status = qdict_get_int(qdict, "status");
1700
    uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1701
    uint64_t addr = qdict_get_int(qdict, "addr");
1702
    uint64_t misc = qdict_get_int(qdict, "misc");
1703

    
1704
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1705
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1706
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1707
            break;
1708
        }
1709
}
1710
#endif
1711

    
1712
static void do_getfd(Monitor *mon, const QDict *qdict)
1713
{
1714
    const char *fdname = qdict_get_str(qdict, "fdname");
1715
    mon_fd_t *monfd;
1716
    int fd;
1717

    
1718
    fd = qemu_chr_get_msgfd(mon->chr);
1719
    if (fd == -1) {
1720
        monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1721
        return;
1722
    }
1723

    
1724
    if (qemu_isdigit(fdname[0])) {
1725
        monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1726
        return;
1727
    }
1728

    
1729
    fd = dup(fd);
1730
    if (fd == -1) {
1731
        monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1732
                       strerror(errno));
1733
        return;
1734
    }
1735

    
1736
    QLIST_FOREACH(monfd, &mon->fds, next) {
1737
        if (strcmp(monfd->name, fdname) != 0) {
1738
            continue;
1739
        }
1740

    
1741
        close(monfd->fd);
1742
        monfd->fd = fd;
1743
        return;
1744
    }
1745

    
1746
    monfd = qemu_mallocz(sizeof(mon_fd_t));
1747
    monfd->name = qemu_strdup(fdname);
1748
    monfd->fd = fd;
1749

    
1750
    QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1751
}
1752

    
1753
static void do_closefd(Monitor *mon, const QDict *qdict)
1754
{
1755
    const char *fdname = qdict_get_str(qdict, "fdname");
1756
    mon_fd_t *monfd;
1757

    
1758
    QLIST_FOREACH(monfd, &mon->fds, next) {
1759
        if (strcmp(monfd->name, fdname) != 0) {
1760
            continue;
1761
        }
1762

    
1763
        QLIST_REMOVE(monfd, next);
1764
        close(monfd->fd);
1765
        qemu_free(monfd->name);
1766
        qemu_free(monfd);
1767
        return;
1768
    }
1769

    
1770
    monitor_printf(mon, "Failed to find file descriptor named %s\n",
1771
                   fdname);
1772
}
1773

    
1774
static void do_loadvm(Monitor *mon, const QDict *qdict)
1775
{
1776
    int saved_vm_running  = vm_running;
1777
    const char *name = qdict_get_str(qdict, "name");
1778

    
1779
    vm_stop(0);
1780

    
1781
    if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1782
        vm_start();
1783
}
1784

    
1785
int monitor_get_fd(Monitor *mon, const char *fdname)
1786
{
1787
    mon_fd_t *monfd;
1788

    
1789
    QLIST_FOREACH(monfd, &mon->fds, next) {
1790
        int fd;
1791

    
1792
        if (strcmp(monfd->name, fdname) != 0) {
1793
            continue;
1794
        }
1795

    
1796
        fd = monfd->fd;
1797

    
1798
        /* caller takes ownership of fd */
1799
        QLIST_REMOVE(monfd, next);
1800
        qemu_free(monfd->name);
1801
        qemu_free(monfd);
1802

    
1803
        return fd;
1804
    }
1805

    
1806
    return -1;
1807
}
1808

    
1809
static const mon_cmd_t mon_cmds[] = {
1810
#include "qemu-monitor.h"
1811
    { NULL, NULL, },
1812
};
1813

    
1814
/* Please update qemu-monitor.hx when adding or changing commands */
1815
static const mon_cmd_t info_cmds[] = {
1816
    {
1817
        .name       = "version",
1818
        .args_type  = "",
1819
        .handler    = do_info_version,
1820
        .params     = "",
1821
        .help       = "show the version of QEMU",
1822
    },
1823
    {
1824
        .name       = "network",
1825
        .args_type  = "",
1826
        .handler    = do_info_network,
1827
        .params     = "",
1828
        .help       = "show the network state",
1829
    },
1830
    {
1831
        .name       = "chardev",
1832
        .args_type  = "",
1833
        .handler    = qemu_chr_info,
1834
        .params     = "",
1835
        .help       = "show the character devices",
1836
    },
1837
    {
1838
        .name       = "block",
1839
        .args_type  = "",
1840
        .handler    = bdrv_info,
1841
        .params     = "",
1842
        .help       = "show the block devices",
1843
    },
1844
    {
1845
        .name       = "blockstats",
1846
        .args_type  = "",
1847
        .handler    = bdrv_info_stats,
1848
        .params     = "",
1849
        .help       = "show block device statistics",
1850
    },
1851
    {
1852
        .name       = "registers",
1853
        .args_type  = "",
1854
        .handler    = do_info_registers,
1855
        .params     = "",
1856
        .help       = "show the cpu registers",
1857
    },
1858
    {
1859
        .name       = "cpus",
1860
        .args_type  = "",
1861
        .handler    = do_info_cpus,
1862
        .params     = "",
1863
        .help       = "show infos for each CPU",
1864
    },
1865
    {
1866
        .name       = "history",
1867
        .args_type  = "",
1868
        .handler    = do_info_history,
1869
        .params     = "",
1870
        .help       = "show the command line history",
1871
    },
1872
    {
1873
        .name       = "irq",
1874
        .args_type  = "",
1875
        .handler    = irq_info,
1876
        .params     = "",
1877
        .help       = "show the interrupts statistics (if available)",
1878
    },
1879
    {
1880
        .name       = "pic",
1881
        .args_type  = "",
1882
        .handler    = pic_info,
1883
        .params     = "",
1884
        .help       = "show i8259 (PIC) state",
1885
    },
1886
    {
1887
        .name       = "pci",
1888
        .args_type  = "",
1889
        .handler    = pci_info,
1890
        .params     = "",
1891
        .help       = "show PCI info",
1892
    },
1893
#if defined(TARGET_I386) || defined(TARGET_SH4)
1894
    {
1895
        .name       = "tlb",
1896
        .args_type  = "",
1897
        .handler    = tlb_info,
1898
        .params     = "",
1899
        .help       = "show virtual to physical memory mappings",
1900
    },
1901
#endif
1902
#if defined(TARGET_I386)
1903
    {
1904
        .name       = "mem",
1905
        .args_type  = "",
1906
        .handler    = mem_info,
1907
        .params     = "",
1908
        .help       = "show the active virtual memory mappings",
1909
    },
1910
    {
1911
        .name       = "hpet",
1912
        .args_type  = "",
1913
        .handler    = do_info_hpet,
1914
        .params     = "",
1915
        .help       = "show state of HPET",
1916
    },
1917
#endif
1918
    {
1919
        .name       = "jit",
1920
        .args_type  = "",
1921
        .handler    = do_info_jit,
1922
        .params     = "",
1923
        .help       = "show dynamic compiler info",
1924
    },
1925
    {
1926
        .name       = "kvm",
1927
        .args_type  = "",
1928
        .handler    = do_info_kvm,
1929
        .params     = "",
1930
        .help       = "show KVM information",
1931
    },
1932
    {
1933
        .name       = "numa",
1934
        .args_type  = "",
1935
        .handler    = do_info_numa,
1936
        .params     = "",
1937
        .help       = "show NUMA information",
1938
    },
1939
    {
1940
        .name       = "usb",
1941
        .args_type  = "",
1942
        .handler    = usb_info,
1943
        .params     = "",
1944
        .help       = "show guest USB devices",
1945
    },
1946
    {
1947
        .name       = "usbhost",
1948
        .args_type  = "",
1949
        .handler    = usb_host_info,
1950
        .params     = "",
1951
        .help       = "show host USB devices",
1952
    },
1953
    {
1954
        .name       = "profile",
1955
        .args_type  = "",
1956
        .handler    = do_info_profile,
1957
        .params     = "",
1958
        .help       = "show profiling information",
1959
    },
1960
    {
1961
        .name       = "capture",
1962
        .args_type  = "",
1963
        .handler    = do_info_capture,
1964
        .params     = "",
1965
        .help       = "show capture information",
1966
    },
1967
    {
1968
        .name       = "snapshots",
1969
        .args_type  = "",
1970
        .handler    = do_info_snapshots,
1971
        .params     = "",
1972
        .help       = "show the currently saved VM snapshots",
1973
    },
1974
    {
1975
        .name       = "status",
1976
        .args_type  = "",
1977
        .handler    = do_info_status,
1978
        .params     = "",
1979
        .help       = "show the current VM status (running|paused)",
1980
    },
1981
    {
1982
        .name       = "pcmcia",
1983
        .args_type  = "",
1984
        .handler    = pcmcia_info,
1985
        .params     = "",
1986
        .help       = "show guest PCMCIA status",
1987
    },
1988
    {
1989
        .name       = "mice",
1990
        .args_type  = "",
1991
        .handler    = do_info_mice,
1992
        .params     = "",
1993
        .help       = "show which guest mouse is receiving events",
1994
    },
1995
    {
1996
        .name       = "vnc",
1997
        .args_type  = "",
1998
        .handler    = do_info_vnc,
1999
        .params     = "",
2000
        .help       = "show the vnc server status",
2001
    },
2002
    {
2003
        .name       = "name",
2004
        .args_type  = "",
2005
        .handler    = do_info_name,
2006
        .params     = "",
2007
        .help       = "show the current VM name",
2008
    },
2009
    {
2010
        .name       = "uuid",
2011
        .args_type  = "",
2012
        .handler    = do_info_uuid,
2013
        .params     = "",
2014
        .help       = "show the current VM UUID",
2015
    },
2016
#if defined(TARGET_PPC)
2017
    {
2018
        .name       = "cpustats",
2019
        .args_type  = "",
2020
        .handler    = do_info_cpu_stats,
2021
        .params     = "",
2022
        .help       = "show CPU statistics",
2023
    },
2024
#endif
2025
#if defined(CONFIG_SLIRP)
2026
    {
2027
        .name       = "usernet",
2028
        .args_type  = "",
2029
        .handler    = do_info_usernet,
2030
        .params     = "",
2031
        .help       = "show user network stack connection states",
2032
    },
2033
#endif
2034
    {
2035
        .name       = "migrate",
2036
        .args_type  = "",
2037
        .handler    = do_info_migrate,
2038
        .params     = "",
2039
        .help       = "show migration status",
2040
    },
2041
    {
2042
        .name       = "balloon",
2043
        .args_type  = "",
2044
        .handler    = do_info_balloon,
2045
        .params     = "",
2046
        .help       = "show balloon information",
2047
    },
2048
    {
2049
        .name       = "qtree",
2050
        .args_type  = "",
2051
        .handler    = do_info_qtree,
2052
        .params     = "",
2053
        .help       = "show device tree",
2054
    },
2055
    {
2056
        .name       = "qdm",
2057
        .args_type  = "",
2058
        .handler    = do_info_qdm,
2059
        .params     = "",
2060
        .help       = "show qdev device model list",
2061
    },
2062
    {
2063
        .name       = "roms",
2064
        .args_type  = "",
2065
        .handler    = do_info_roms,
2066
        .params     = "",
2067
        .help       = "show roms",
2068
    },
2069
    {
2070
        .name       = NULL,
2071
    },
2072
};
2073

    
2074
/*******************************************************************/
2075

    
2076
static const char *pch;
2077
static jmp_buf expr_env;
2078

    
2079
#define MD_TLONG 0
2080
#define MD_I32   1
2081

    
2082
typedef struct MonitorDef {
2083
    const char *name;
2084
    int offset;
2085
    target_long (*get_value)(const struct MonitorDef *md, int val);
2086
    int type;
2087
} MonitorDef;
2088

    
2089
#if defined(TARGET_I386)
2090
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2091
{
2092
    CPUState *env = mon_get_cpu();
2093
    if (!env)
2094
        return 0;
2095
    return env->eip + env->segs[R_CS].base;
2096
}
2097
#endif
2098

    
2099
#if defined(TARGET_PPC)
2100
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2101
{
2102
    CPUState *env = mon_get_cpu();
2103
    unsigned int u;
2104
    int i;
2105

    
2106
    if (!env)
2107
        return 0;
2108

    
2109
    u = 0;
2110
    for (i = 0; i < 8; i++)
2111
        u |= env->crf[i] << (32 - (4 * i));
2112

    
2113
    return u;
2114
}
2115

    
2116
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2117
{
2118
    CPUState *env = mon_get_cpu();
2119
    if (!env)
2120
        return 0;
2121
    return env->msr;
2122
}
2123

    
2124
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2125
{
2126
    CPUState *env = mon_get_cpu();
2127
    if (!env)
2128
        return 0;
2129
    return env->xer;
2130
}
2131

    
2132
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2133
{
2134
    CPUState *env = mon_get_cpu();
2135
    if (!env)
2136
        return 0;
2137
    return cpu_ppc_load_decr(env);
2138
}
2139

    
2140
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2141
{
2142
    CPUState *env = mon_get_cpu();
2143
    if (!env)
2144
        return 0;
2145
    return cpu_ppc_load_tbu(env);
2146
}
2147

    
2148
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2149
{
2150
    CPUState *env = mon_get_cpu();
2151
    if (!env)
2152
        return 0;
2153
    return cpu_ppc_load_tbl(env);
2154
}
2155
#endif
2156

    
2157
#if defined(TARGET_SPARC)
2158
#ifndef TARGET_SPARC64
2159
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2160
{
2161
    CPUState *env = mon_get_cpu();
2162
    if (!env)
2163
        return 0;
2164
    return GET_PSR(env);
2165
}
2166
#endif
2167

    
2168
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2169
{
2170
    CPUState *env = mon_get_cpu();
2171
    if (!env)
2172
        return 0;
2173
    return env->regwptr[val];
2174
}
2175
#endif
2176

    
2177
static const MonitorDef monitor_defs[] = {
2178
#ifdef TARGET_I386
2179

    
2180
#define SEG(name, seg) \
2181
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2182
    { name ".base", offsetof(CPUState, segs[seg].base) },\
2183
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2184

    
2185
    { "eax", offsetof(CPUState, regs[0]) },
2186
    { "ecx", offsetof(CPUState, regs[1]) },
2187
    { "edx", offsetof(CPUState, regs[2]) },
2188
    { "ebx", offsetof(CPUState, regs[3]) },
2189
    { "esp|sp", offsetof(CPUState, regs[4]) },
2190
    { "ebp|fp", offsetof(CPUState, regs[5]) },
2191
    { "esi", offsetof(CPUState, regs[6]) },
2192
    { "edi", offsetof(CPUState, regs[7]) },
2193
#ifdef TARGET_X86_64
2194
    { "r8", offsetof(CPUState, regs[8]) },
2195
    { "r9", offsetof(CPUState, regs[9]) },
2196
    { "r10", offsetof(CPUState, regs[10]) },
2197
    { "r11", offsetof(CPUState, regs[11]) },
2198
    { "r12", offsetof(CPUState, regs[12]) },
2199
    { "r13", offsetof(CPUState, regs[13]) },
2200
    { "r14", offsetof(CPUState, regs[14]) },
2201
    { "r15", offsetof(CPUState, regs[15]) },
2202
#endif
2203
    { "eflags", offsetof(CPUState, eflags) },
2204
    { "eip", offsetof(CPUState, eip) },
2205
    SEG("cs", R_CS)
2206
    SEG("ds", R_DS)
2207
    SEG("es", R_ES)
2208
    SEG("ss", R_SS)
2209
    SEG("fs", R_FS)
2210
    SEG("gs", R_GS)
2211
    { "pc", 0, monitor_get_pc, },
2212
#elif defined(TARGET_PPC)
2213
    /* General purpose registers */
2214
    { "r0", offsetof(CPUState, gpr[0]) },
2215
    { "r1", offsetof(CPUState, gpr[1]) },
2216
    { "r2", offsetof(CPUState, gpr[2]) },
2217
    { "r3", offsetof(CPUState, gpr[3]) },
2218
    { "r4", offsetof(CPUState, gpr[4]) },
2219
    { "r5", offsetof(CPUState, gpr[5]) },
2220
    { "r6", offsetof(CPUState, gpr[6]) },
2221
    { "r7", offsetof(CPUState, gpr[7]) },
2222
    { "r8", offsetof(CPUState, gpr[8]) },
2223
    { "r9", offsetof(CPUState, gpr[9]) },
2224
    { "r10", offsetof(CPUState, gpr[10]) },
2225
    { "r11", offsetof(CPUState, gpr[11]) },
2226
    { "r12", offsetof(CPUState, gpr[12]) },
2227
    { "r13", offsetof(CPUState, gpr[13]) },
2228
    { "r14", offsetof(CPUState, gpr[14]) },
2229
    { "r15", offsetof(CPUState, gpr[15]) },
2230
    { "r16", offsetof(CPUState, gpr[16]) },
2231
    { "r17", offsetof(CPUState, gpr[17]) },
2232
    { "r18", offsetof(CPUState, gpr[18]) },
2233
    { "r19", offsetof(CPUState, gpr[19]) },
2234
    { "r20", offsetof(CPUState, gpr[20]) },
2235
    { "r21", offsetof(CPUState, gpr[21]) },
2236
    { "r22", offsetof(CPUState, gpr[22]) },
2237
    { "r23", offsetof(CPUState, gpr[23]) },
2238
    { "r24", offsetof(CPUState, gpr[24]) },
2239
    { "r25", offsetof(CPUState, gpr[25]) },
2240
    { "r26", offsetof(CPUState, gpr[26]) },
2241
    { "r27", offsetof(CPUState, gpr[27]) },
2242
    { "r28", offsetof(CPUState, gpr[28]) },
2243
    { "r29", offsetof(CPUState, gpr[29]) },
2244
    { "r30", offsetof(CPUState, gpr[30]) },
2245
    { "r31", offsetof(CPUState, gpr[31]) },
2246
    /* Floating point registers */
2247
    { "f0", offsetof(CPUState, fpr[0]) },
2248
    { "f1", offsetof(CPUState, fpr[1]) },
2249
    { "f2", offsetof(CPUState, fpr[2]) },
2250
    { "f3", offsetof(CPUState, fpr[3]) },
2251
    { "f4", offsetof(CPUState, fpr[4]) },
2252
    { "f5", offsetof(CPUState, fpr[5]) },
2253
    { "f6", offsetof(CPUState, fpr[6]) },
2254
    { "f7", offsetof(CPUState, fpr[7]) },
2255
    { "f8", offsetof(CPUState, fpr[8]) },
2256
    { "f9", offsetof(CPUState, fpr[9]) },
2257
    { "f10", offsetof(CPUState, fpr[10]) },
2258
    { "f11", offsetof(CPUState, fpr[11]) },
2259
    { "f12", offsetof(CPUState, fpr[12]) },
2260
    { "f13", offsetof(CPUState, fpr[13]) },
2261
    { "f14", offsetof(CPUState, fpr[14]) },
2262
    { "f15", offsetof(CPUState, fpr[15]) },
2263
    { "f16", offsetof(CPUState, fpr[16]) },
2264
    { "f17", offsetof(CPUState, fpr[17]) },
2265
    { "f18", offsetof(CPUState, fpr[18]) },
2266
    { "f19", offsetof(CPUState, fpr[19]) },
2267
    { "f20", offsetof(CPUState, fpr[20]) },
2268
    { "f21", offsetof(CPUState, fpr[21]) },
2269
    { "f22", offsetof(CPUState, fpr[22]) },
2270
    { "f23", offsetof(CPUState, fpr[23]) },
2271
    { "f24", offsetof(CPUState, fpr[24]) },
2272
    { "f25", offsetof(CPUState, fpr[25]) },
2273
    { "f26", offsetof(CPUState, fpr[26]) },
2274
    { "f27", offsetof(CPUState, fpr[27]) },
2275
    { "f28", offsetof(CPUState, fpr[28]) },
2276
    { "f29", offsetof(CPUState, fpr[29]) },
2277
    { "f30", offsetof(CPUState, fpr[30]) },
2278
    { "f31", offsetof(CPUState, fpr[31]) },
2279
    { "fpscr", offsetof(CPUState, fpscr) },
2280
    /* Next instruction pointer */
2281
    { "nip|pc", offsetof(CPUState, nip) },
2282
    { "lr", offsetof(CPUState, lr) },
2283
    { "ctr", offsetof(CPUState, ctr) },
2284
    { "decr", 0, &monitor_get_decr, },
2285
    { "ccr", 0, &monitor_get_ccr, },
2286
    /* Machine state register */
2287
    { "msr", 0, &monitor_get_msr, },
2288
    { "xer", 0, &monitor_get_xer, },
2289
    { "tbu", 0, &monitor_get_tbu, },
2290
    { "tbl", 0, &monitor_get_tbl, },
2291
#if defined(TARGET_PPC64)
2292
    /* Address space register */
2293
    { "asr", offsetof(CPUState, asr) },
2294
#endif
2295
    /* Segment registers */
2296
    { "sdr1", offsetof(CPUState, sdr1) },
2297
    { "sr0", offsetof(CPUState, sr[0]) },
2298
    { "sr1", offsetof(CPUState, sr[1]) },
2299
    { "sr2", offsetof(CPUState, sr[2]) },
2300
    { "sr3", offsetof(CPUState, sr[3]) },
2301
    { "sr4", offsetof(CPUState, sr[4]) },
2302
    { "sr5", offsetof(CPUState, sr[5]) },
2303
    { "sr6", offsetof(CPUState, sr[6]) },
2304
    { "sr7", offsetof(CPUState, sr[7]) },
2305
    { "sr8", offsetof(CPUState, sr[8]) },
2306
    { "sr9", offsetof(CPUState, sr[9]) },
2307
    { "sr10", offsetof(CPUState, sr[10]) },
2308
    { "sr11", offsetof(CPUState, sr[11]) },
2309
    { "sr12", offsetof(CPUState, sr[12]) },
2310
    { "sr13", offsetof(CPUState, sr[13]) },
2311
    { "sr14", offsetof(CPUState, sr[14]) },
2312
    { "sr15", offsetof(CPUState, sr[15]) },
2313
    /* Too lazy to put BATs and SPRs ... */
2314
#elif defined(TARGET_SPARC)
2315
    { "g0", offsetof(CPUState, gregs[0]) },
2316
    { "g1", offsetof(CPUState, gregs[1]) },
2317
    { "g2", offsetof(CPUState, gregs[2]) },
2318
    { "g3", offsetof(CPUState, gregs[3]) },
2319
    { "g4", offsetof(CPUState, gregs[4]) },
2320
    { "g5", offsetof(CPUState, gregs[5]) },
2321
    { "g6", offsetof(CPUState, gregs[6]) },
2322
    { "g7", offsetof(CPUState, gregs[7]) },
2323
    { "o0", 0, monitor_get_reg },
2324
    { "o1", 1, monitor_get_reg },
2325
    { "o2", 2, monitor_get_reg },
2326
    { "o3", 3, monitor_get_reg },
2327
    { "o4", 4, monitor_get_reg },
2328
    { "o5", 5, monitor_get_reg },
2329
    { "o6", 6, monitor_get_reg },
2330
    { "o7", 7, monitor_get_reg },
2331
    { "l0", 8, monitor_get_reg },
2332
    { "l1", 9, monitor_get_reg },
2333
    { "l2", 10, monitor_get_reg },
2334
    { "l3", 11, monitor_get_reg },
2335
    { "l4", 12, monitor_get_reg },
2336
    { "l5", 13, monitor_get_reg },
2337
    { "l6", 14, monitor_get_reg },
2338
    { "l7", 15, monitor_get_reg },
2339
    { "i0", 16, monitor_get_reg },
2340
    { "i1", 17, monitor_get_reg },
2341
    { "i2", 18, monitor_get_reg },
2342
    { "i3", 19, monitor_get_reg },
2343
    { "i4", 20, monitor_get_reg },
2344
    { "i5", 21, monitor_get_reg },
2345
    { "i6", 22, monitor_get_reg },
2346
    { "i7", 23, monitor_get_reg },
2347
    { "pc", offsetof(CPUState, pc) },
2348
    { "npc", offsetof(CPUState, npc) },
2349
    { "y", offsetof(CPUState, y) },
2350
#ifndef TARGET_SPARC64
2351
    { "psr", 0, &monitor_get_psr, },
2352
    { "wim", offsetof(CPUState, wim) },
2353
#endif
2354
    { "tbr", offsetof(CPUState, tbr) },
2355
    { "fsr", offsetof(CPUState, fsr) },
2356
    { "f0", offsetof(CPUState, fpr[0]) },
2357
    { "f1", offsetof(CPUState, fpr[1]) },
2358
    { "f2", offsetof(CPUState, fpr[2]) },
2359
    { "f3", offsetof(CPUState, fpr[3]) },
2360
    { "f4", offsetof(CPUState, fpr[4]) },
2361
    { "f5", offsetof(CPUState, fpr[5]) },
2362
    { "f6", offsetof(CPUState, fpr[6]) },
2363
    { "f7", offsetof(CPUState, fpr[7]) },
2364
    { "f8", offsetof(CPUState, fpr[8]) },
2365
    { "f9", offsetof(CPUState, fpr[9]) },
2366
    { "f10", offsetof(CPUState, fpr[10]) },
2367
    { "f11", offsetof(CPUState, fpr[11]) },
2368
    { "f12", offsetof(CPUState, fpr[12]) },
2369
    { "f13", offsetof(CPUState, fpr[13]) },
2370
    { "f14", offsetof(CPUState, fpr[14]) },
2371
    { "f15", offsetof(CPUState, fpr[15]) },
2372
    { "f16", offsetof(CPUState, fpr[16]) },
2373
    { "f17", offsetof(CPUState, fpr[17]) },
2374
    { "f18", offsetof(CPUState, fpr[18]) },
2375
    { "f19", offsetof(CPUState, fpr[19]) },
2376
    { "f20", offsetof(CPUState, fpr[20]) },
2377
    { "f21", offsetof(CPUState, fpr[21]) },
2378
    { "f22", offsetof(CPUState, fpr[22]) },
2379
    { "f23", offsetof(CPUState, fpr[23]) },
2380
    { "f24", offsetof(CPUState, fpr[24]) },
2381
    { "f25", offsetof(CPUState, fpr[25]) },
2382
    { "f26", offsetof(CPUState, fpr[26]) },
2383
    { "f27", offsetof(CPUState, fpr[27]) },
2384
    { "f28", offsetof(CPUState, fpr[28]) },
2385
    { "f29", offsetof(CPUState, fpr[29]) },
2386
    { "f30", offsetof(CPUState, fpr[30]) },
2387
    { "f31", offsetof(CPUState, fpr[31]) },
2388
#ifdef TARGET_SPARC64
2389
    { "f32", offsetof(CPUState, fpr[32]) },
2390
    { "f34", offsetof(CPUState, fpr[34]) },
2391
    { "f36", offsetof(CPUState, fpr[36]) },
2392
    { "f38", offsetof(CPUState, fpr[38]) },
2393
    { "f40", offsetof(CPUState, fpr[40]) },
2394
    { "f42", offsetof(CPUState, fpr[42]) },
2395
    { "f44", offsetof(CPUState, fpr[44]) },
2396
    { "f46", offsetof(CPUState, fpr[46]) },
2397
    { "f48", offsetof(CPUState, fpr[48]) },
2398
    { "f50", offsetof(CPUState, fpr[50]) },
2399
    { "f52", offsetof(CPUState, fpr[52]) },
2400
    { "f54", offsetof(CPUState, fpr[54]) },
2401
    { "f56", offsetof(CPUState, fpr[56]) },
2402
    { "f58", offsetof(CPUState, fpr[58]) },
2403
    { "f60", offsetof(CPUState, fpr[60]) },
2404
    { "f62", offsetof(CPUState, fpr[62]) },
2405
    { "asi", offsetof(CPUState, asi) },
2406
    { "pstate", offsetof(CPUState, pstate) },
2407
    { "cansave", offsetof(CPUState, cansave) },
2408
    { "canrestore", offsetof(CPUState, canrestore) },
2409
    { "otherwin", offsetof(CPUState, otherwin) },
2410
    { "wstate", offsetof(CPUState, wstate) },
2411
    { "cleanwin", offsetof(CPUState, cleanwin) },
2412
    { "fprs", offsetof(CPUState, fprs) },
2413
#endif
2414
#endif
2415
    { NULL },
2416
};
2417

    
2418
static void expr_error(Monitor *mon, const char *msg)
2419
{
2420
    monitor_printf(mon, "%s\n", msg);
2421
    longjmp(expr_env, 1);
2422
}
2423

    
2424
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2425
static int get_monitor_def(target_long *pval, const char *name)
2426
{
2427
    const MonitorDef *md;
2428
    void *ptr;
2429

    
2430
    for(md = monitor_defs; md->name != NULL; md++) {
2431
        if (compare_cmd(name, md->name)) {
2432
            if (md->get_value) {
2433
                *pval = md->get_value(md, md->offset);
2434
            } else {
2435
                CPUState *env = mon_get_cpu();
2436
                if (!env)
2437
                    return -2;
2438
                ptr = (uint8_t *)env + md->offset;
2439
                switch(md->type) {
2440
                case MD_I32:
2441
                    *pval = *(int32_t *)ptr;
2442
                    break;
2443
                case MD_TLONG:
2444
                    *pval = *(target_long *)ptr;
2445
                    break;
2446
                default:
2447
                    *pval = 0;
2448
                    break;
2449
                }
2450
            }
2451
            return 0;
2452
        }
2453
    }
2454
    return -1;
2455
}
2456

    
2457
static void next(void)
2458
{
2459
    if (*pch != '\0') {
2460
        pch++;
2461
        while (qemu_isspace(*pch))
2462
            pch++;
2463
    }
2464
}
2465

    
2466
static int64_t expr_sum(Monitor *mon);
2467

    
2468
static int64_t expr_unary(Monitor *mon)
2469
{
2470
    int64_t n;
2471
    char *p;
2472
    int ret;
2473

    
2474
    switch(*pch) {
2475
    case '+':
2476
        next();
2477
        n = expr_unary(mon);
2478
        break;
2479
    case '-':
2480
        next();
2481
        n = -expr_unary(mon);
2482
        break;
2483
    case '~':
2484
        next();
2485
        n = ~expr_unary(mon);
2486
        break;
2487
    case '(':
2488
        next();
2489
        n = expr_sum(mon);
2490
        if (*pch != ')') {
2491
            expr_error(mon, "')' expected");
2492
        }
2493
        next();
2494
        break;
2495
    case '\'':
2496
        pch++;
2497
        if (*pch == '\0')
2498
            expr_error(mon, "character constant expected");
2499
        n = *pch;
2500
        pch++;
2501
        if (*pch != '\'')
2502
            expr_error(mon, "missing terminating \' character");
2503
        next();
2504
        break;
2505
    case '$':
2506
        {
2507
            char buf[128], *q;
2508
            target_long reg=0;
2509

    
2510
            pch++;
2511
            q = buf;
2512
            while ((*pch >= 'a' && *pch <= 'z') ||
2513
                   (*pch >= 'A' && *pch <= 'Z') ||
2514
                   (*pch >= '0' && *pch <= '9') ||
2515
                   *pch == '_' || *pch == '.') {
2516
                if ((q - buf) < sizeof(buf) - 1)
2517
                    *q++ = *pch;
2518
                pch++;
2519
            }
2520
            while (qemu_isspace(*pch))
2521
                pch++;
2522
            *q = 0;
2523
            ret = get_monitor_def(&reg, buf);
2524
            if (ret == -1)
2525
                expr_error(mon, "unknown register");
2526
            else if (ret == -2)
2527
                expr_error(mon, "no cpu defined");
2528
            n = reg;
2529
        }
2530
        break;
2531
    case '\0':
2532
        expr_error(mon, "unexpected end of expression");
2533
        n = 0;
2534
        break;
2535
    default:
2536
#if TARGET_PHYS_ADDR_BITS > 32
2537
        n = strtoull(pch, &p, 0);
2538
#else
2539
        n = strtoul(pch, &p, 0);
2540
#endif
2541
        if (pch == p) {
2542
            expr_error(mon, "invalid char in expression");
2543
        }
2544
        pch = p;
2545
        while (qemu_isspace(*pch))
2546
            pch++;
2547
        break;
2548
    }
2549
    return n;
2550
}
2551

    
2552

    
2553
static int64_t expr_prod(Monitor *mon)
2554
{
2555
    int64_t val, val2;
2556
    int op;
2557

    
2558
    val = expr_unary(mon);
2559
    for(;;) {
2560
        op = *pch;
2561
        if (op != '*' && op != '/' && op != '%')
2562
            break;
2563
        next();
2564
        val2 = expr_unary(mon);
2565
        switch(op) {
2566
        default:
2567
        case '*':
2568
            val *= val2;
2569
            break;
2570
        case '/':
2571
        case '%':
2572
            if (val2 == 0)
2573
                expr_error(mon, "division by zero");
2574
            if (op == '/')
2575
                val /= val2;
2576
            else
2577
                val %= val2;
2578
            break;
2579
        }
2580
    }
2581
    return val;
2582
}
2583

    
2584
static int64_t expr_logic(Monitor *mon)
2585
{
2586
    int64_t val, val2;
2587
    int op;
2588

    
2589
    val = expr_prod(mon);
2590
    for(;;) {
2591
        op = *pch;
2592
        if (op != '&' && op != '|' && op != '^')
2593
            break;
2594
        next();
2595
        val2 = expr_prod(mon);
2596
        switch(op) {
2597
        default:
2598
        case '&':
2599
            val &= val2;
2600
            break;
2601
        case '|':
2602
            val |= val2;
2603
            break;
2604
        case '^':
2605
            val ^= val2;
2606
            break;
2607
        }
2608
    }
2609
    return val;
2610
}
2611

    
2612
static int64_t expr_sum(Monitor *mon)
2613
{
2614
    int64_t val, val2;
2615
    int op;
2616

    
2617
    val = expr_logic(mon);
2618
    for(;;) {
2619
        op = *pch;
2620
        if (op != '+' && op != '-')
2621
            break;
2622
        next();
2623
        val2 = expr_logic(mon);
2624
        if (op == '+')
2625
            val += val2;
2626
        else
2627
            val -= val2;
2628
    }
2629
    return val;
2630
}
2631

    
2632
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2633
{
2634
    pch = *pp;
2635
    if (setjmp(expr_env)) {
2636
        *pp = pch;
2637
        return -1;
2638
    }
2639
    while (qemu_isspace(*pch))
2640
        pch++;
2641
    *pval = expr_sum(mon);
2642
    *pp = pch;
2643
    return 0;
2644
}
2645

    
2646
static int get_str(char *buf, int buf_size, const char **pp)
2647
{
2648
    const char *p;
2649
    char *q;
2650
    int c;
2651

    
2652
    q = buf;
2653
    p = *pp;
2654
    while (qemu_isspace(*p))
2655
        p++;
2656
    if (*p == '\0') {
2657
    fail:
2658
        *q = '\0';
2659
        *pp = p;
2660
        return -1;
2661
    }
2662
    if (*p == '\"') {
2663
        p++;
2664
        while (*p != '\0' && *p != '\"') {
2665
            if (*p == '\\') {
2666
                p++;
2667
                c = *p++;
2668
                switch(c) {
2669
                case 'n':
2670
                    c = '\n';
2671
                    break;
2672
                case 'r':
2673
                    c = '\r';
2674
                    break;
2675
                case '\\':
2676
                case '\'':
2677
                case '\"':
2678
                    break;
2679
                default:
2680
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2681
                    goto fail;
2682
                }
2683
                if ((q - buf) < buf_size - 1) {
2684
                    *q++ = c;
2685
                }
2686
            } else {
2687
                if ((q - buf) < buf_size - 1) {
2688
                    *q++ = *p;
2689
                }
2690
                p++;
2691
            }
2692
        }
2693
        if (*p != '\"') {
2694
            qemu_printf("unterminated string\n");
2695
            goto fail;
2696
        }
2697
        p++;
2698
    } else {
2699
        while (*p != '\0' && !qemu_isspace(*p)) {
2700
            if ((q - buf) < buf_size - 1) {
2701
                *q++ = *p;
2702
            }
2703
            p++;
2704
        }
2705
    }
2706
    *q = '\0';
2707
    *pp = p;
2708
    return 0;
2709
}
2710

    
2711
/*
2712
 * Store the command-name in cmdname, and return a pointer to
2713
 * the remaining of the command string.
2714
 */
2715
static const char *get_command_name(const char *cmdline,
2716
                                    char *cmdname, size_t nlen)
2717
{
2718
    size_t len;
2719
    const char *p, *pstart;
2720

    
2721
    p = cmdline;
2722
    while (qemu_isspace(*p))
2723
        p++;
2724
    if (*p == '\0')
2725
        return NULL;
2726
    pstart = p;
2727
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2728
        p++;
2729
    len = p - pstart;
2730
    if (len > nlen - 1)
2731
        len = nlen - 1;
2732
    memcpy(cmdname, pstart, len);
2733
    cmdname[len] = '\0';
2734
    return p;
2735
}
2736

    
2737
/**
2738
 * Read key of 'type' into 'key' and return the current
2739
 * 'type' pointer.
2740
 */
2741
static char *key_get_info(const char *type, char **key)
2742
{
2743
    size_t len;
2744
    char *p, *str;
2745

    
2746
    if (*type == ',')
2747
        type++;
2748

    
2749
    p = strchr(type, ':');
2750
    if (!p) {
2751
        *key = NULL;
2752
        return NULL;
2753
    }
2754
    len = p - type;
2755

    
2756
    str = qemu_malloc(len + 1);
2757
    memcpy(str, type, len);
2758
    str[len] = '\0';
2759

    
2760
    *key = str;
2761
    return ++p;
2762
}
2763

    
2764
static int default_fmt_format = 'x';
2765
static int default_fmt_size = 4;
2766

    
2767
#define MAX_ARGS 16
2768

    
2769
static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2770
                                              const char *cmdline,
2771
                                              QDict *qdict)
2772
{
2773
    const char *p, *typestr;
2774
    int c;
2775
    const mon_cmd_t *cmd;
2776
    char cmdname[256];
2777
    char buf[1024];
2778
    char *key;
2779

    
2780
#ifdef DEBUG
2781
    monitor_printf(mon, "command='%s'\n", cmdline);
2782
#endif
2783

    
2784
    /* extract the command name */
2785
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2786
    if (!p)
2787
        return NULL;
2788

    
2789
    /* find the command */
2790
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2791
        if (compare_cmd(cmdname, cmd->name))
2792
            break;
2793
    }
2794

    
2795
    if (cmd->name == NULL) {
2796
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2797
        return NULL;
2798
    }
2799

    
2800
    /* parse the parameters */
2801
    typestr = cmd->args_type;
2802
    for(;;) {
2803
        typestr = key_get_info(typestr, &key);
2804
        if (!typestr)
2805
            break;
2806
        c = *typestr;
2807
        typestr++;
2808
        switch(c) {
2809
        case 'F':
2810
        case 'B':
2811
        case 's':
2812
            {
2813
                int ret;
2814

    
2815
                while (qemu_isspace(*p))
2816
                    p++;
2817
                if (*typestr == '?') {
2818
                    typestr++;
2819
                    if (*p == '\0') {
2820
                        /* no optional string: NULL argument */
2821
                        break;
2822
                    }
2823
                }
2824
                ret = get_str(buf, sizeof(buf), &p);
2825
                if (ret < 0) {
2826
                    switch(c) {
2827
                    case 'F':
2828
                        monitor_printf(mon, "%s: filename expected\n",
2829
                                       cmdname);
2830
                        break;
2831
                    case 'B':
2832
                        monitor_printf(mon, "%s: block device name expected\n",
2833
                                       cmdname);
2834
                        break;
2835
                    default:
2836
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2837
                        break;
2838
                    }
2839
                    goto fail;
2840
                }
2841
                qdict_put(qdict, key, qstring_from_str(buf));
2842
            }
2843
            break;
2844
        case '/':
2845
            {
2846
                int count, format, size;
2847

    
2848
                while (qemu_isspace(*p))
2849
                    p++;
2850
                if (*p == '/') {
2851
                    /* format found */
2852
                    p++;
2853
                    count = 1;
2854
                    if (qemu_isdigit(*p)) {
2855
                        count = 0;
2856
                        while (qemu_isdigit(*p)) {
2857
                            count = count * 10 + (*p - '0');
2858
                            p++;
2859
                        }
2860
                    }
2861
                    size = -1;
2862
                    format = -1;
2863
                    for(;;) {
2864
                        switch(*p) {
2865
                        case 'o':
2866
                        case 'd':
2867
                        case 'u':
2868
                        case 'x':
2869
                        case 'i':
2870
                        case 'c':
2871
                            format = *p++;
2872
                            break;
2873
                        case 'b':
2874
                            size = 1;
2875
                            p++;
2876
                            break;
2877
                        case 'h':
2878
                            size = 2;
2879
                            p++;
2880
                            break;
2881
                        case 'w':
2882
                            size = 4;
2883
                            p++;
2884
                            break;
2885
                        case 'g':
2886
                        case 'L':
2887
                            size = 8;
2888
                            p++;
2889
                            break;
2890
                        default:
2891
                            goto next;
2892
                        }
2893
                    }
2894
                next:
2895
                    if (*p != '\0' && !qemu_isspace(*p)) {
2896
                        monitor_printf(mon, "invalid char in format: '%c'\n",
2897
                                       *p);
2898
                        goto fail;
2899
                    }
2900
                    if (format < 0)
2901
                        format = default_fmt_format;
2902
                    if (format != 'i') {
2903
                        /* for 'i', not specifying a size gives -1 as size */
2904
                        if (size < 0)
2905
                            size = default_fmt_size;
2906
                        default_fmt_size = size;
2907
                    }
2908
                    default_fmt_format = format;
2909
                } else {
2910
                    count = 1;
2911
                    format = default_fmt_format;
2912
                    if (format != 'i') {
2913
                        size = default_fmt_size;
2914
                    } else {
2915
                        size = -1;
2916
                    }
2917
                }
2918
                qdict_put(qdict, "count", qint_from_int(count));
2919
                qdict_put(qdict, "format", qint_from_int(format));
2920
                qdict_put(qdict, "size", qint_from_int(size));
2921
            }
2922
            break;
2923
        case 'i':
2924
        case 'l':
2925
            {
2926
                int64_t val;
2927

    
2928
                while (qemu_isspace(*p))
2929
                    p++;
2930
                if (*typestr == '?' || *typestr == '.') {
2931
                    if (*typestr == '?') {
2932
                        if (*p == '\0') {
2933
                            typestr++;
2934
                            break;
2935
                        }
2936
                    } else {
2937
                        if (*p == '.') {
2938
                            p++;
2939
                            while (qemu_isspace(*p))
2940
                                p++;
2941
                        } else {
2942
                            typestr++;
2943
                            break;
2944
                        }
2945
                    }
2946
                    typestr++;
2947
                }
2948
                if (get_expr(mon, &val, &p))
2949
                    goto fail;
2950
                /* Check if 'i' is greater than 32-bit */
2951
                if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2952
                    monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2953
                    monitor_printf(mon, "integer is for 32-bit values\n");
2954
                    goto fail;
2955
                }
2956
                qdict_put(qdict, key, qint_from_int(val));
2957
            }
2958
            break;
2959
        case '-':
2960
            {
2961
                int has_option;
2962
                /* option */
2963

    
2964
                c = *typestr++;
2965
                if (c == '\0')
2966
                    goto bad_type;
2967
                while (qemu_isspace(*p))
2968
                    p++;
2969
                has_option = 0;
2970
                if (*p == '-') {
2971
                    p++;
2972
                    if (*p != c) {
2973
                        monitor_printf(mon, "%s: unsupported option -%c\n",
2974
                                       cmdname, *p);
2975
                        goto fail;
2976
                    }
2977
                    p++;
2978
                    has_option = 1;
2979
                }
2980
                qdict_put(qdict, key, qint_from_int(has_option));
2981
            }
2982
            break;
2983
        default:
2984
        bad_type:
2985
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2986
            goto fail;
2987
        }
2988
        qemu_free(key);
2989
        key = NULL;
2990
    }
2991
    /* check that all arguments were parsed */
2992
    while (qemu_isspace(*p))
2993
        p++;
2994
    if (*p != '\0') {
2995
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2996
                       cmdname);
2997
        goto fail;
2998
    }
2999

    
3000
    return cmd;
3001

    
3002
fail:
3003
    qemu_free(key);
3004
    return NULL;
3005
}
3006

    
3007
static void monitor_handle_command(Monitor *mon, const char *cmdline)
3008
{
3009
    QDict *qdict;
3010
    const mon_cmd_t *cmd;
3011

    
3012
    qdict = qdict_new();
3013

    
3014
    cmd = monitor_parse_command(mon, cmdline, qdict);
3015
    if (cmd) {
3016
        void (*handler)(Monitor *mon, const QDict *qdict);
3017

    
3018
        qemu_errors_to_mon(mon);
3019

    
3020
        handler = cmd->handler;
3021
        handler(mon, qdict);
3022

    
3023
        qemu_errors_to_previous();
3024
    }
3025

    
3026
    QDECREF(qdict);
3027
}
3028

    
3029
static void cmd_completion(const char *name, const char *list)
3030
{
3031
    const char *p, *pstart;
3032
    char cmd[128];
3033
    int len;
3034

    
3035
    p = list;
3036
    for(;;) {
3037
        pstart = p;
3038
        p = strchr(p, '|');
3039
        if (!p)
3040
            p = pstart + strlen(pstart);
3041
        len = p - pstart;
3042
        if (len > sizeof(cmd) - 2)
3043
            len = sizeof(cmd) - 2;
3044
        memcpy(cmd, pstart, len);
3045
        cmd[len] = '\0';
3046
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3047
            readline_add_completion(cur_mon->rs, cmd);
3048
        }
3049
        if (*p == '\0')
3050
            break;
3051
        p++;
3052
    }
3053
}
3054

    
3055
static void file_completion(const char *input)
3056
{
3057
    DIR *ffs;
3058
    struct dirent *d;
3059
    char path[1024];
3060
    char file[1024], file_prefix[1024];
3061
    int input_path_len;
3062
    const char *p;
3063

    
3064
    p = strrchr(input, '/');
3065
    if (!p) {
3066
        input_path_len = 0;
3067
        pstrcpy(file_prefix, sizeof(file_prefix), input);
3068
        pstrcpy(path, sizeof(path), ".");
3069
    } else {
3070
        input_path_len = p - input + 1;
3071
        memcpy(path, input, input_path_len);
3072
        if (input_path_len > sizeof(path) - 1)
3073
            input_path_len = sizeof(path) - 1;
3074
        path[input_path_len] = '\0';
3075
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3076
    }
3077
#ifdef DEBUG_COMPLETION
3078
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3079
                   input, path, file_prefix);
3080
#endif
3081
    ffs = opendir(path);
3082
    if (!ffs)
3083
        return;
3084
    for(;;) {
3085
        struct stat sb;
3086
        d = readdir(ffs);
3087
        if (!d)
3088
            break;
3089
        if (strstart(d->d_name, file_prefix, NULL)) {
3090
            memcpy(file, input, input_path_len);
3091
            if (input_path_len < sizeof(file))
3092
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3093
                        d->d_name);
3094
            /* stat the file to find out if it's a directory.
3095
             * In that case add a slash to speed up typing long paths
3096
             */
3097
            stat(file, &sb);
3098
            if(S_ISDIR(sb.st_mode))
3099
                pstrcat(file, sizeof(file), "/");
3100
            readline_add_completion(cur_mon->rs, file);
3101
        }
3102
    }
3103
    closedir(ffs);
3104
}
3105

    
3106
static void block_completion_it(void *opaque, BlockDriverState *bs)
3107
{
3108
    const char *name = bdrv_get_device_name(bs);
3109
    const char *input = opaque;
3110

    
3111
    if (input[0] == '\0' ||
3112
        !strncmp(name, (char *)input, strlen(input))) {
3113
        readline_add_completion(cur_mon->rs, name);
3114
    }
3115
}
3116

    
3117
/* NOTE: this parser is an approximate form of the real command parser */
3118
static void parse_cmdline(const char *cmdline,
3119
                         int *pnb_args, char **args)
3120
{
3121
    const char *p;
3122
    int nb_args, ret;
3123
    char buf[1024];
3124

    
3125
    p = cmdline;
3126
    nb_args = 0;
3127
    for(;;) {
3128
        while (qemu_isspace(*p))
3129
            p++;
3130
        if (*p == '\0')
3131
            break;
3132
        if (nb_args >= MAX_ARGS)
3133
            break;
3134
        ret = get_str(buf, sizeof(buf), &p);
3135
        args[nb_args] = qemu_strdup(buf);
3136
        nb_args++;
3137
        if (ret < 0)
3138
            break;
3139
    }
3140
    *pnb_args = nb_args;
3141
}
3142

    
3143
static const char *next_arg_type(const char *typestr)
3144
{
3145
    const char *p = strchr(typestr, ':');
3146
    return (p != NULL ? ++p : typestr);
3147
}
3148

    
3149
static void monitor_find_completion(const char *cmdline)
3150
{
3151
    const char *cmdname;
3152
    char *args[MAX_ARGS];
3153
    int nb_args, i, len;
3154
    const char *ptype, *str;
3155
    const mon_cmd_t *cmd;
3156
    const KeyDef *key;
3157

    
3158
    parse_cmdline(cmdline, &nb_args, args);
3159
#ifdef DEBUG_COMPLETION
3160
    for(i = 0; i < nb_args; i++) {
3161
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3162
    }
3163
#endif
3164

    
3165
    /* if the line ends with a space, it means we want to complete the
3166
       next arg */
3167
    len = strlen(cmdline);
3168
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3169
        if (nb_args >= MAX_ARGS)
3170
            return;
3171
        args[nb_args++] = qemu_strdup("");
3172
    }
3173
    if (nb_args <= 1) {
3174
        /* command completion */
3175
        if (nb_args == 0)
3176
            cmdname = "";
3177
        else
3178
            cmdname = args[0];
3179
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3180
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3181
            cmd_completion(cmdname, cmd->name);
3182
        }
3183
    } else {
3184
        /* find the command */
3185
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3186
            if (compare_cmd(args[0], cmd->name))
3187
                goto found;
3188
        }
3189
        return;
3190
    found:
3191
        ptype = next_arg_type(cmd->args_type);
3192
        for(i = 0; i < nb_args - 2; i++) {
3193
            if (*ptype != '\0') {
3194
                ptype = next_arg_type(ptype);
3195
                while (*ptype == '?')
3196
                    ptype = next_arg_type(ptype);
3197
            }
3198
        }
3199
        str = args[nb_args - 1];
3200
        if (*ptype == '-' && ptype[1] != '\0') {
3201
            ptype += 2;
3202
        }
3203
        switch(*ptype) {
3204
        case 'F':
3205
            /* file completion */
3206
            readline_set_completion_index(cur_mon->rs, strlen(str));
3207
            file_completion(str);
3208
            break;
3209
        case 'B':
3210
            /* block device name completion */
3211
            readline_set_completion_index(cur_mon->rs, strlen(str));
3212
            bdrv_iterate(block_completion_it, (void *)str);
3213
            break;
3214
        case 's':
3215
            /* XXX: more generic ? */
3216
            if (!strcmp(cmd->name, "info")) {
3217
                readline_set_completion_index(cur_mon->rs, strlen(str));
3218
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3219
                    cmd_completion(str, cmd->name);
3220
                }
3221
            } else if (!strcmp(cmd->name, "sendkey")) {
3222
                char *sep = strrchr(str, '-');
3223
                if (sep)
3224
                    str = sep + 1;
3225
                readline_set_completion_index(cur_mon->rs, strlen(str));
3226
                for(key = key_defs; key->name != NULL; key++) {
3227
                    cmd_completion(str, key->name);
3228
                }
3229
            } else if (!strcmp(cmd->name, "help|?")) {
3230
                readline_set_completion_index(cur_mon->rs, strlen(str));
3231
                for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3232
                    cmd_completion(str, cmd->name);
3233
                }
3234
            }
3235
            break;
3236
        default:
3237
            break;
3238
        }
3239
    }
3240
    for(i = 0; i < nb_args; i++)
3241
        qemu_free(args[i]);
3242
}
3243

    
3244
static int monitor_can_read(void *opaque)
3245
{
3246
    Monitor *mon = opaque;
3247

    
3248
    return (mon->suspend_cnt == 0) ? 128 : 0;
3249
}
3250

    
3251
static void monitor_read(void *opaque, const uint8_t *buf, int size)
3252
{
3253
    Monitor *old_mon = cur_mon;
3254
    int i;
3255

    
3256
    cur_mon = opaque;
3257

    
3258
    if (cur_mon->rs) {
3259
        for (i = 0; i < size; i++)
3260
            readline_handle_byte(cur_mon->rs, buf[i]);
3261
    } else {
3262
        if (size == 0 || buf[size - 1] != 0)
3263
            monitor_printf(cur_mon, "corrupted command\n");
3264
        else
3265
            monitor_handle_command(cur_mon, (char *)buf);
3266
    }
3267

    
3268
    cur_mon = old_mon;
3269
}
3270

    
3271
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3272
{
3273
    monitor_suspend(mon);
3274
    monitor_handle_command(mon, cmdline);
3275
    monitor_resume(mon);
3276
}
3277

    
3278
int monitor_suspend(Monitor *mon)
3279
{
3280
    if (!mon->rs)
3281
        return -ENOTTY;
3282
    mon->suspend_cnt++;
3283
    return 0;
3284
}
3285

    
3286
void monitor_resume(Monitor *mon)
3287
{
3288
    if (!mon->rs)
3289
        return;
3290
    if (--mon->suspend_cnt == 0)
3291
        readline_show_prompt(mon->rs);
3292
}
3293

    
3294
static void monitor_event(void *opaque, int event)
3295
{
3296
    Monitor *mon = opaque;
3297

    
3298
    switch (event) {
3299
    case CHR_EVENT_MUX_IN:
3300
        mon->mux_out = 0;
3301
        if (mon->reset_seen) {
3302
            readline_restart(mon->rs);
3303
            monitor_resume(mon);
3304
            monitor_flush(mon);
3305
        } else {
3306
            mon->suspend_cnt = 0;
3307
        }
3308
        break;
3309

    
3310
    case CHR_EVENT_MUX_OUT:
3311
        if (mon->reset_seen) {
3312
            if (mon->suspend_cnt == 0) {
3313
                monitor_printf(mon, "\n");
3314
            }
3315
            monitor_flush(mon);
3316
            monitor_suspend(mon);
3317
        } else {
3318
            mon->suspend_cnt++;
3319
        }
3320
        mon->mux_out = 1;
3321
        break;
3322

    
3323
    case CHR_EVENT_RESET:
3324
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3325
                       "information\n", QEMU_VERSION);
3326
        if (!mon->mux_out) {
3327
            readline_show_prompt(mon->rs);
3328
        }
3329
        mon->reset_seen = 1;
3330
        break;
3331
    }
3332
}
3333

    
3334

    
3335
/*
3336
 * Local variables:
3337
 *  c-indent-level: 4
3338
 *  c-basic-offset: 4
3339
 *  tab-width: 8
3340
 * End:
3341
 */
3342

    
3343
void monitor_init(CharDriverState *chr, int flags)
3344
{
3345
    static int is_first_init = 1;
3346
    Monitor *mon;
3347

    
3348
    if (is_first_init) {
3349
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3350
        is_first_init = 0;
3351
    }
3352

    
3353
    mon = qemu_mallocz(sizeof(*mon));
3354

    
3355
    mon->chr = chr;
3356
    mon->flags = flags;
3357
    if (flags & MONITOR_USE_READLINE) {
3358
        mon->rs = readline_init(mon, monitor_find_completion);
3359
        monitor_read_command(mon, 0);
3360
    }
3361

    
3362
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3363
                          mon);
3364

    
3365
    QLIST_INSERT_HEAD(&mon_list, mon, entry);
3366
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3367
        cur_mon = mon;
3368
}
3369

    
3370
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3371
{
3372
    BlockDriverState *bs = opaque;
3373
    int ret = 0;
3374

    
3375
    if (bdrv_set_key(bs, password) != 0) {
3376
        monitor_printf(mon, "invalid password\n");
3377
        ret = -EPERM;
3378
    }
3379
    if (mon->password_completion_cb)
3380
        mon->password_completion_cb(mon->password_opaque, ret);
3381

    
3382
    monitor_read_command(mon, 1);
3383
}
3384

    
3385
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3386
                                 BlockDriverCompletionFunc *completion_cb,
3387
                                 void *opaque)
3388
{
3389
    int err;
3390

    
3391
    if (!bdrv_key_required(bs)) {
3392
        if (completion_cb)
3393
            completion_cb(opaque, 0);
3394
        return;
3395
    }
3396

    
3397
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3398
                   bdrv_get_encrypted_filename(bs));
3399

    
3400
    mon->password_completion_cb = completion_cb;
3401
    mon->password_opaque = opaque;
3402

    
3403
    err = monitor_read_password(mon, bdrv_password_cb, bs);
3404

    
3405
    if (err && completion_cb)
3406
        completion_cb(opaque, err);
3407
}
3408

    
3409
typedef struct QemuErrorSink QemuErrorSink;
3410
struct QemuErrorSink {
3411
    enum {
3412
        ERR_SINK_FILE,
3413
        ERR_SINK_MONITOR,
3414
    } dest;
3415
    union {
3416
        FILE    *fp;
3417
        Monitor *mon;
3418
    };
3419
    QemuErrorSink *previous;
3420
};
3421

    
3422
static QemuErrorSink *qemu_error_sink;
3423

    
3424
void qemu_errors_to_file(FILE *fp)
3425
{
3426
    QemuErrorSink *sink;
3427

    
3428
    sink = qemu_mallocz(sizeof(*sink));
3429
    sink->dest = ERR_SINK_FILE;
3430
    sink->fp = fp;
3431
    sink->previous = qemu_error_sink;
3432
    qemu_error_sink = sink;
3433
}
3434

    
3435
void qemu_errors_to_mon(Monitor *mon)
3436
{
3437
    QemuErrorSink *sink;
3438

    
3439
    sink = qemu_mallocz(sizeof(*sink));
3440
    sink->dest = ERR_SINK_MONITOR;
3441
    sink->mon = mon;
3442
    sink->previous = qemu_error_sink;
3443
    qemu_error_sink = sink;
3444
}
3445

    
3446
void qemu_errors_to_previous(void)
3447
{
3448
    QemuErrorSink *sink;
3449

    
3450
    assert(qemu_error_sink != NULL);
3451
    sink = qemu_error_sink;
3452
    qemu_error_sink = sink->previous;
3453
    qemu_free(sink);
3454
}
3455

    
3456
void qemu_error(const char *fmt, ...)
3457
{
3458
    va_list args;
3459

    
3460
    assert(qemu_error_sink != NULL);
3461
    switch (qemu_error_sink->dest) {
3462
    case ERR_SINK_FILE:
3463
        va_start(args, fmt);
3464
        vfprintf(qemu_error_sink->fp, fmt, args);
3465
        va_end(args);
3466
        break;
3467
    case ERR_SINK_MONITOR:
3468
        va_start(args, fmt);
3469
        monitor_vprintf(qemu_error_sink->mon, fmt, args);
3470
        va_end(args);
3471
        break;
3472
    }
3473
}