AMD64 - Move fdisk and nextboot up one level and remove src/sbin/i386.
[dragonfly.git] / test / debug / kwrite.c
blobf78f35eac4ec0740784ccc4d8c53d97071cf0627
1 /*
2 * Tool to read/write kernel memory.
4 * Compile with
5 cc -o kwrite kwrite.c -lkvm
7 * $DragonFly: src/test/debug/kwrite.c,v 1.1 2006/01/09 15:05:45 corecode Exp $
8 */
10 #include <fcntl.h>
11 #include <kvm.h>
12 #include <limits.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <err.h>
18 static void
19 usage(void)
21 fprintf(stderr, "usage: kwrite addr[/width] [bval] [bval]\n");
22 exit(1);
25 int
26 main(int argc, char **argv)
28 char errbuf[_POSIX2_LINE_MAX];
29 kvm_t *kvm;
30 unsigned long addr, width = 1, i;
31 char *endptr, *endptr2;
33 if (argc < 2)
34 usage();
36 errno = 0;
37 addr = strtoul(argv[1], &endptr, 0);
38 if (endptr == argv[1])
39 errx(1, "no addr");
40 if (errno != 0)
41 err(1, "addr '%s'", argv[1]);
42 switch (*endptr) {
43 case 0:
44 break;
45 case '/':
46 endptr++;
47 width = strtoul(endptr, &endptr2, 0);
48 if (endptr2 == endptr)
49 errx(1, "no width");
50 if (errno != 0)
51 err(1, "width '%s'", endptr);
52 break;
53 default:
54 errx(1, "invalid addr '%s'", argv[1]);
57 kvm = kvm_openfiles(NULL, NULL, NULL, O_RDWR, errbuf);
58 if (kvm == NULL)
59 errx(1, "%s", errbuf);
61 if (argc < 3) {
62 for (i = 0; i < width; i++) {
63 char b;
65 if (kvm_read(kvm, addr + i, &b, 1) != 1)
66 errx(1, "%s", kvm_geterr(kvm));
67 printf("%#0*lx %02x\n", sizeof(addr) * 2, addr + i, b);
69 } else {
70 if (argc < width + 2)
71 errx(1, "too few values for width %ld", width);
72 width = argc - 2;
73 for (i = 0; i < width; i++) {
74 long v;
75 char b, bo;
77 errno = 0;
78 v = strtol(argv[2 + i], &endptr, 0);
79 if (endptr == argv[2 + i] || *endptr != 0)
80 errx(1, "invalid value '%s'", argv[2 + i]);
81 if (errno != 0 || v < 0 || v > 256) {
82 if (errno == 0) errno = ERANGE;
83 errx(1, "value '%s'", argv[2 + i]);
85 b = v;
87 if (kvm_read(kvm, addr + i, &bo, 1) != 1)
88 errx(1, "%s", kvm_geterr(kvm));
89 printf("%#0*lx %02x -> %02x\n", sizeof(addr) * 2, addr + i, bo, b);
90 if (kvm_write(kvm, addr + i, &b, 1) != 1)
91 errx(1, "%s", kvm_geterr(kvm));
95 return 0;