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",
68 (int)sizeof(addr
) * 2, addr
+ i
, b
);
72 errx(1, "too few values for width %ld", width
);
74 for (i
= 0; i
< width
; i
++) {
79 v
= strtol(argv
[2 + i
], &endptr
, 0);
80 if (endptr
== argv
[2 + i
] || *endptr
!= 0)
81 errx(1, "invalid value '%s'", argv
[2 + i
]);
82 if (errno
!= 0 || v
< 0 || v
> 256) {
83 if (errno
== 0) errno
= ERANGE
;
84 errx(1, "value '%s'", argv
[2 + i
]);
88 if (kvm_read(kvm
, addr
+ i
, &bo
, 1) != 1)
89 errx(1, "%s", kvm_geterr(kvm
));
90 printf("%#0*lx %02x -> %02x\n",
91 (int)sizeof(addr
) * 2, addr
+ i
, bo
, b
);
92 if (kvm_write(kvm
, addr
+ i
, &b
, 1) != 1)
93 errx(1, "%s", kvm_geterr(kvm
));