2 * Tool to read/write kernel memory.
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 $
21 fprintf(stderr
, "usage: kwrite addr[/width] [bval] [bval]\n");
26 main(int argc
, char **argv
)
28 char errbuf
[_POSIX2_LINE_MAX
];
30 unsigned long addr
, width
= 1, i
;
31 char *endptr
, *endptr2
;
37 addr
= strtoul(argv
[1], &endptr
, 0);
38 if (endptr
== argv
[1])
41 err(1, "addr '%s'", argv
[1]);
47 width
= strtoul(endptr
, &endptr2
, 0);
48 if (endptr2
== endptr
)
51 err(1, "width '%s'", endptr
);
54 errx(1, "invalid addr '%s'", argv
[1]);
57 kvm
= kvm_openfiles(NULL
, NULL
, NULL
, O_RDWR
, errbuf
);
59 errx(1, "%s", errbuf
);
62 for (i
= 0; i
< width
; i
++) {
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
);
71 errx(1, "too few values for width %ld", width
);
73 for (i
= 0; i
< width
; i
++) {
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
]);
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
));