debuggers.hg

changeset 21939:754877be695b

tools/misc: xen-hvmcrash: New tool to attempt to crash HVM guests

This tool reads the CPU save records, overwrites RIP with a bogus
value, and then restores them. This is, of course, not guaranteed
to crash the guest (since the CPUs may not be executing in kernel
at the time) but it's good for breaking into some tight loops that
would be hard to debug otherwise.

Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Paul Durrant <paul.durrant@citrix.com>
date Thu Jul 29 18:58:19 2010 +0100 (2010-07-29)
parents 9ced33b68bd8
children c9e7850ec9a1
files .hgignore tools/misc/Makefile tools/misc/xen-hvmcrash.c
line diff
     1.1 --- a/.hgignore	Thu Jul 29 18:52:33 2010 +0100
     1.2 +++ b/.hgignore	Thu Jul 29 18:58:19 2010 +0100
     1.3 @@ -199,6 +199,7 @@
     1.4  ^tools/misc/xen_cpuperf$
     1.5  ^tools/misc/xen-detect$
     1.6  ^tools/misc/xen-hptool$
     1.7 +^tools/misc/xen-hvmcrash$
     1.8  ^tools/misc/xen-tmem-list-parse$
     1.9  ^tools/misc/xenperf$
    1.10  ^tools/misc/xenpm$
     2.1 --- a/tools/misc/Makefile	Thu Jul 29 18:52:33 2010 +0100
     2.2 +++ b/tools/misc/Makefile	Thu Jul 29 18:58:19 2010 +0100
     2.3 @@ -11,7 +11,7 @@ CFLAGS   += $(INCLUDES)
     2.4  HDRS     = $(wildcard *.h)
     2.5  
     2.6  TARGETS-y := xenperf xenpm xen-tmem-list-parse gtraceview gtracestat xenlockprof xenwatchdogd
     2.7 -TARGETS-$(CONFIG_X86) += xen-detect xen-hvmctx
     2.8 +TARGETS-$(CONFIG_X86) += xen-detect xen-hvmctx xen-hvmcrash
     2.9  TARGETS-$(CONFIG_MIGRATE) += xen-hptool
    2.10  TARGETS := $(TARGETS-y)
    2.11  
    2.12 @@ -24,7 +24,7 @@ INSTALL_BIN-$(CONFIG_X86) += xen-detect
    2.13  INSTALL_BIN := $(INSTALL_BIN-y)
    2.14  
    2.15  INSTALL_SBIN-y := xm xen-bugtool xen-python-path xend xenperf xsview xenpm xen-tmem-list-parse gtraceview gtracestat xenlockprof xenwatchdogd
    2.16 -INSTALL_SBIN-$(CONFIG_X86) += xen-hvmctx
    2.17 +INSTALL_SBIN-$(CONFIG_X86) += xen-hvmctx xen-hvmcrash
    2.18  INSTALL_SBIN-$(CONFIG_MIGRATE) += xen-hptool
    2.19  INSTALL_SBIN := $(INSTALL_SBIN-y)
    2.20  
    2.21 @@ -51,7 +51,7 @@ clean:
    2.22  %.o: %.c $(HDRS) Makefile
    2.23  	$(CC) -c $(CFLAGS) -o $@ $<
    2.24  
    2.25 -xen-hvmctx xenperf xenpm gtracestat xenlockprof xen-hptool xenwatchdogd: %: %.o Makefile
    2.26 +xen-hvmctx xen-hvmcrash xenperf xenpm gtracestat xenlockprof xen-hptool xenwatchdogd: %: %.o Makefile
    2.27  	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LDFLAGS_libxenctrl) $(LDFLAGS_libxenguest) $(LDFLAGS_libxenstore)
    2.28  
    2.29  gtraceview: %: %.o Makefile
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/misc/xen-hvmcrash.c	Thu Jul 29 18:58:19 2010 +0100
     3.3 @@ -0,0 +1,143 @@
     3.4 +/* 
     3.5 + * xen-hvmcrash.c
     3.6 + *
     3.7 + * Attempt to crash an HVM guest by overwriting RIP/EIP with a bogus value
     3.8 + * 
     3.9 + * Copyright (c) 2010 Citrix Systems, Inc.
    3.10 + *
    3.11 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    3.12 + * of this software and associated documentation files (the "Software"), to
    3.13 + * deal in the Software without restriction, including without limitation the
    3.14 + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    3.15 + * sell copies of the Software, and to permit persons to whom the Software is
    3.16 + * furnished to do so, subject to the following conditions:
    3.17 + *
    3.18 + * The above copyright notice and this permission notice shall be included in
    3.19 + * all copies or substantial portions of the Software.
    3.20 + *
    3.21 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    3.22 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    3.23 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    3.24 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    3.25 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    3.26 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    3.27 + * DEALINGS IN THE SOFTWARE.
    3.28 + */
    3.29 +
    3.30 +#include <inttypes.h>
    3.31 +#include <stdio.h>
    3.32 +#include <stdlib.h>
    3.33 +#include <stddef.h>
    3.34 +#include <stdint.h>
    3.35 +#include <unistd.h>
    3.36 +#include <string.h>
    3.37 +#include <errno.h>
    3.38 +#include <limits.h>
    3.39 +
    3.40 +#include <sys/types.h>
    3.41 +#include <sys/stat.h>
    3.42 +#include <arpa/inet.h>
    3.43 +
    3.44 +#include <xenctrl.h>
    3.45 +#include <xen/xen.h>
    3.46 +#include <xen/domctl.h>
    3.47 +#include <xen/hvm/save.h>
    3.48 +
    3.49 +int
    3.50 +main(int argc, char **argv)
    3.51 +{
    3.52 +    int domid;
    3.53 +    xc_interface *xch;
    3.54 +    xc_dominfo_t dominfo;
    3.55 +    int ret;
    3.56 +    uint32_t len;
    3.57 +    uint8_t *buf;
    3.58 +    uint32_t off;
    3.59 +    struct hvm_save_descriptor *descriptor;
    3.60 +
    3.61 +    if (argc != 2 || !argv[1] || (domid = atoi(argv[1])) < 0) {
    3.62 +        fprintf(stderr, "usage: %s <domid>\n", argv[0]);
    3.63 +        exit(1);
    3.64 +    }
    3.65 +
    3.66 +    xch = xc_interface_open(0, 0, 0);
    3.67 +    if (!xch) {
    3.68 +        fprintf(stderr, "error: can't open libxc handle\n");
    3.69 +        exit(1);
    3.70 +    }
    3.71 +
    3.72 +    ret = xc_domain_getinfo(xch, domid, 1, &dominfo);
    3.73 +    if (ret < 0) {
    3.74 +        perror("xc_domain_getinfo");
    3.75 +        exit(1);
    3.76 +    }
    3.77 +
    3.78 +    if (!dominfo.hvm) {
    3.79 +        fprintf(stderr, "domain %d is not HVM\n", domid);
    3.80 +        exit(1);
    3.81 +    }
    3.82 +
    3.83 +    ret = xc_domain_pause(xch, domid);
    3.84 +    if (ret < 0) {
    3.85 +        perror("xc_domain_pause");
    3.86 +        exit(-1);
    3.87 +    }
    3.88 +
    3.89 +    /*
    3.90 +     * Calling with zero buffer length should return the buffer length
    3.91 +     * required.
    3.92 +     */
    3.93 +    ret = xc_domain_hvm_getcontext(xch, domid, 0, 0);
    3.94 +    if (ret < 0) {
    3.95 +        perror("xc_domain_hvm_getcontext");
    3.96 +        exit(1);
    3.97 +    }
    3.98 +    
    3.99 +    len = ret;
   3.100 +    buf = malloc(len);
   3.101 +    if (buf == NULL) {
   3.102 +        perror("malloc");
   3.103 +        exit(1);
   3.104 +    }
   3.105 +
   3.106 +    ret = xc_domain_hvm_getcontext(xch, domid, buf, len);
   3.107 +    if (ret < 0) {
   3.108 +        perror("xc_domain_hvm_getcontext");
   3.109 +        exit(1);
   3.110 +    }
   3.111 +
   3.112 +    off = 0;
   3.113 +
   3.114 +    while (off < len) {
   3.115 +        descriptor = (struct hvm_save_descriptor *)(buf + off);
   3.116 +
   3.117 +        off += sizeof (struct hvm_save_descriptor);
   3.118 +
   3.119 +        if (descriptor->typecode == HVM_SAVE_CODE(CPU)) {
   3.120 +            HVM_SAVE_TYPE(CPU) *cpu;
   3.121 +
   3.122 +            /* Overwrite EIP/RIP with some recognisable but bogus value */
   3.123 +            cpu = (HVM_SAVE_TYPE(CPU) *)(buf + off);
   3.124 +            printf("CPU[%d]: RIP = %" PRIx64 "\n", descriptor->instance, cpu->rip);
   3.125 +            cpu->rip = 0xf001;
   3.126 +        } else if (descriptor->typecode == HVM_SAVE_CODE(END)) {
   3.127 +            break;
   3.128 +        }
   3.129 +
   3.130 +        off += descriptor->length;
   3.131 +    }
   3.132 +
   3.133 +    ret = xc_domain_hvm_setcontext(xch, domid, buf, len);
   3.134 +    if (ret < 0) {
   3.135 +        perror("xc_domain_hvm_setcontext");
   3.136 +        exit(1);
   3.137 +    }
   3.138 +
   3.139 +    ret = xc_domain_unpause(xch, domid);
   3.140 +    if (ret < 0) {
   3.141 +        perror("xc_domain_unpause");
   3.142 +        exit(1);
   3.143 +    }
   3.144 +
   3.145 +    return 0;
   3.146 +}