summaryrefslogtreecommitdiff
path: root/other/wrez/debug
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/wrez/debug
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/wrez/debug')
-rw-r--r--other/wrez/debug/pcdebug.c250
-rwxr-xr-xother/wrez/debug/pcdumpbin0 -> 7678 bytes
-rw-r--r--other/wrez/debug/pcdump.c148
3 files changed, 398 insertions, 0 deletions
diff --git a/other/wrez/debug/pcdebug.c b/other/wrez/debug/pcdebug.c
new file mode 100644
index 0000000..86ee12b
--- /dev/null
+++ b/other/wrez/debug/pcdebug.c
@@ -0,0 +1,250 @@
1/* memory dump utility
2 * -scut
3 */
4
5#include <sys/types.h>
6#include <sys/ptrace.h>
7#include <sys/wait.h>
8#include <sys/user.h>
9#include <errno.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <stdio.h>
13#include <libgen.h> /* basename */
14
15
16void
17hexdump (unsigned char *data, unsigned int amount);
18
19
20int
21main (int argc, char *argv[])
22{
23 pid_t fpid; /* child pid, gets ptraced */
24 char * argv0;
25 struct user regs; /* PTRACE pulled registers */
26 unsigned long int addr, /* segment start address */
27 addr_end, /* segment end address */
28 len; /* length of segment */
29 unsigned long int addr_walker, /* walker to dump memory */
30 eip; /* current childs eip */
31
32 /* array to temporarily store data into */
33 unsigned char data_saved[sizeof (unsigned long int)];
34
35 /* file to read mapping information */
36 FILE * map_f; /* /proc/<pid>/maps stream */
37 unsigned char map_line[256]; /* one line each from map */
38
39 /* data for the dump files */
40 FILE * dump_f; /* stream */
41 char dump_name[64]; /* filename buffer */
42
43
44 if (argc < 3 || sscanf (argv[1], "0x%lx", &eip) != 1) {
45 printf ("usage: %s <eip> <argv0 [argv1 [...]]>\n\n", argv[0]);
46 printf ("will run 'argv0' as program with given arguments, "
47 "until 'eip' is reached, then\n"
48 "dumping 'len' bytes from 'addr'.\n\n"
49 "example: %s 0x08049014 0x08048000 0x100 /bin/ls "
50 "-l\n\n", argv[0]);
51
52 exit (EXIT_FAILURE);
53 }
54
55 argv0 = argv[2];
56
57 fpid = fork ();
58 if (fpid < 0) {
59 perror ("fork");
60 exit (EXIT_FAILURE);
61 }
62 if (fpid == 0) { /* child */
63 if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) != 0) {
64 perror ("ptrace PTRACE_TRACEME");
65 exit (EXIT_FAILURE);
66 }
67 fprintf (stderr, " child: TRACEME set\n");
68
69 fprintf (stderr, " child: executing: %s\n", argv[2]);
70 close (1);
71 dup2 (2, 1);
72 execve (argv[2], &argv[2], NULL);
73
74 /* failed ? */
75 perror ("execve");
76 exit (EXIT_FAILURE);
77 }
78
79 wait (NULL);
80
81 memset (&regs, 0, sizeof (regs));
82
83 if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
84 perror ("ptrace PTRACE_GETREGS");
85 exit (EXIT_FAILURE);
86 }
87 fprintf (stderr, "[0x%08lx] first stop\n", regs.regs.eip);
88
89 /* now single step until given eip is reached */
90 do {
91 if (ptrace (PTRACE_SINGLESTEP, fpid, NULL, NULL) < 0) {
92 perror ("ptrace PTRACE_SINGLESTEP");
93 exit (EXIT_FAILURE);
94 }
95 wait (NULL);
96
97 memset (&regs, 0, sizeof (regs));
98 if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
99 perror ("ptrace PTRACE_GETREGS");
100 exit (EXIT_FAILURE);
101 }
102 fprintf (stderr, "0x%08lx\n", regs.regs.eip);
103 } while (regs.regs.eip != eip);
104
105 fprintf (stderr, "MEMDUMP: eip @ 0x%08lx, dumping...\n", eip);
106
107 snprintf (dump_name, sizeof (dump_name), "%s.regs",
108 basename (argv0));
109 dump_name[sizeof (dump_name) - 1] = '\0';
110 dump_f = fopen (dump_name, "w");
111 if (dump_f == NULL) {
112 perror ("fopen dumpfile regs");
113 exit (EXIT_FAILURE);
114 }
115 fprintf (dump_f, "eax = 0x%08lx\n", regs.regs.eax);
116 fprintf (dump_f, "ebx = 0x%08lx\n", regs.regs.ebx);
117 fprintf (dump_f, "ecx = 0x%08lx\n", regs.regs.ecx);
118 fprintf (dump_f, "edx = 0x%08lx\n", regs.regs.edx);
119 fprintf (dump_f, "esi = 0x%08lx\n", regs.regs.esi);
120 fprintf (dump_f, "edi = 0x%08lx\n", regs.regs.edi);
121 fprintf (dump_f, "ebp = 0x%08lx\n", regs.regs.ebp);
122 fprintf (dump_f, "esp = 0x%08lx\n", regs.regs.esp);
123 fprintf (dump_f, "eflags = 0x%08lx\n", regs.regs.eflags);
124 fprintf (dump_f, "xcs = 0x%08lx\n", regs.regs.xcs);
125 fprintf (dump_f, "xds = 0x%08lx\n", regs.regs.xds);
126 fprintf (dump_f, "xes = 0x%08lx\n", regs.regs.xes);
127 fprintf (dump_f, "xss = 0x%08lx\n", regs.regs.xss);
128 fclose (dump_f);
129
130 snprintf (map_line, sizeof (map_line), "/proc/%d/maps", fpid);
131 map_line[sizeof (map_line) - 1] = '\0';
132 map_f = fopen (map_line, "r");
133 if (map_f == NULL) {
134 perror ("fopen map-file");
135
136 exit (EXIT_FAILURE);
137 }
138
139 while (fgets (map_line, sizeof (map_line), map_f) != NULL) {
140 char map_perm[8];
141
142 if (sscanf (map_line, "%08lx-%08lx %7[rwxp-] ",
143 &addr, &addr_end, map_perm) != 3)
144 {
145 perror ("invalid map-line");
146
147 exit (EXIT_FAILURE);
148 }
149 if (addr_end < addr) {
150 fprintf (stderr, "sanity required, not so: "
151 "addr = 0x%08lx, addr_end = 0x%08lx",
152 addr, addr_end);
153
154 exit (EXIT_FAILURE);
155 }
156 len = addr_end - addr;
157 map_perm[sizeof (map_perm) - 1] = '\0'; /* ;-) */
158
159 fprintf (stderr, "MEMDUMP: -> 0x%08lx (0x%08lx bytes, "
160 "perm %s)\n", addr, len, map_perm);
161
162 snprintf (dump_name, sizeof (dump_name),
163 "%s.0x%08lx.0x%08lx.%s",
164 basename (argv0), addr, len, map_perm);
165 dump_name[sizeof (dump_name) - 1] = '\0';
166 dump_f = fopen (dump_name, "wb");
167 if (dump_f == NULL) {
168 perror ("fopen dumpfile");
169
170 exit (EXIT_FAILURE);
171 }
172
173 /* save data, assuming addr is page aligned */
174 for (addr_walker = 0 ; addr_walker < len ;
175 addr_walker += sizeof (data_saved))
176 {
177 errno = 0;
178
179 *((unsigned long int *) &data_saved[0]) =
180 ptrace (PTRACE_PEEKDATA, fpid,
181 addr + addr_walker, NULL);
182
183 if (errno == 0 && fwrite (&data_saved[0], 1, 4,
184 dump_f) != 4)
185 {
186 perror ("fwrite dumpfile");
187
188 exit (EXIT_FAILURE);
189 } else if (errno != 0) {
190 fprintf (stderr,
191 "[0x%08lx] invalid PTRACE_PEEKDATA\n",
192 addr + addr_walker);
193
194 exit (EXIT_FAILURE);
195 }
196 }
197
198 fclose (dump_f);
199 }
200 fclose (map_f);
201
202 if (ptrace (PTRACE_DETACH, fpid, NULL, NULL) < 0) {
203 perror ("ptrace PTRACE_DETACH");
204 exit (EXIT_FAILURE);
205 }
206
207 fprintf (stderr, "MEMDUMP: success. terminating.\n");
208 exit (EXIT_SUCCESS);
209}
210
211
212
213void
214hexdump (unsigned char *data, unsigned int amount)
215{
216 unsigned int dp, p; /* data pointer */
217 const char trans[] =
218 "................................ !\"#$%&'()*+,-./0123456789"
219 ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
220 "nopqrstuvwxyz{|}~...................................."
221 "....................................................."
222 "........................................";
223
224 for (dp = 1; dp <= amount; dp++) {
225 printf ("%02x ", data[dp-1]);
226 if ((dp % 8) == 0)
227 printf (" ");
228 if ((dp % 16) == 0) {
229 printf ("| ");
230 p = dp;
231 for (dp -= 16; dp < p; dp++)
232 printf ("%c", trans[data[dp]]);
233 printf ("\n");
234 }
235 }
236 if ((amount % 16) != 0) {
237 p = dp = 16 - (amount % 16);
238 for (dp = p; dp > 0; dp--) {
239 printf (" ");
240 if (((dp % 8) == 0) && (p != 8))
241 printf (" ");
242 }
243 printf (" | ");
244 for (dp = (amount - (16 - p)); dp < amount; dp++)
245 printf ("%c", trans[data[dp]]);
246 }
247 printf ("\n");
248
249 return;
250}
diff --git a/other/wrez/debug/pcdump b/other/wrez/debug/pcdump
new file mode 100755
index 0000000..3c77057
--- /dev/null
+++ b/other/wrez/debug/pcdump
Binary files differ
diff --git a/other/wrez/debug/pcdump.c b/other/wrez/debug/pcdump.c
new file mode 100644
index 0000000..95b96b7
--- /dev/null
+++ b/other/wrez/debug/pcdump.c
@@ -0,0 +1,148 @@
1/* memory dump utility
2 * -scut
3 */
4
5#include <sys/types.h>
6#include <sys/ptrace.h>
7#include <sys/wait.h>
8#include <sys/user.h>
9#include <errno.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <stdio.h>
13#include <libgen.h> /* basename */
14
15
16void
17hexdump (unsigned char *data, unsigned int amount);
18
19
20int
21main (int argc, char *argv[], char *envp[])
22{
23 pid_t fpid; /* child pid, gets ptraced */
24 char * argv0;
25 struct user regs; /* PTRACE pulled registers */
26 unsigned long int addr, /* segment start address */
27 addr_end, /* segment end address */
28 len; /* length of segment */
29 unsigned long int addr_walker, /* walker to dump memory */
30 eip; /* current childs eip */
31
32 /* array to temporarily store data into */
33 unsigned char data_saved[sizeof (unsigned long int)];
34
35 /* file to read mapping information */
36 FILE * map_f; /* /proc/<pid>/maps stream */
37 unsigned char map_line[256]; /* one line each from map */
38
39 /* data for the dump files */
40 FILE * dump_f; /* stream */
41 char dump_name[64]; /* filename buffer */
42
43
44 if (argc < 2) {
45 printf ("usage: %s <argv0 [argv1 [...]]>\n\n", argv[0]);
46 printf ("will run 'argv0' as program with given arguments, "
47 "dumping 'eip'\n\n", argv[0]);
48
49 exit (EXIT_FAILURE);
50 }
51
52 fpid = fork ();
53 if (fpid < 0) {
54 perror ("fork");
55 exit (EXIT_FAILURE);
56 }
57 if (fpid == 0) { /* child */
58 if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) != 0) {
59 perror ("ptrace PTRACE_TRACEME");
60 exit (EXIT_FAILURE);
61 }
62 fprintf (stderr, " child: TRACEME set\n");
63
64 fprintf (stderr, " child: executing: %s\n", argv[2]);
65 close (1);
66 dup2 (2, 1);
67 execve (argv[1], &argv[1], envp);
68
69 /* failed ? */
70 perror ("execve");
71 exit (EXIT_FAILURE);
72 }
73
74 wait (NULL);
75
76 memset (&regs, 0, sizeof (regs));
77
78 if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
79 perror ("ptrace PTRACE_GETREGS");
80 exit (EXIT_FAILURE);
81 }
82 fprintf (stderr, "[0x%08lx] first stop\n", regs.regs.eip);
83
84 /* now single step until given eip is reached */
85 do {
86 if (ptrace (PTRACE_SINGLESTEP, fpid, NULL, NULL) < 0) {
87 perror ("ptrace PTRACE_SINGLESTEP");
88 exit (EXIT_FAILURE);
89 }
90 wait (NULL);
91
92 memset (&regs, 0, sizeof (regs));
93 if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
94 perror ("ptrace PTRACE_GETREGS");
95 exit (EXIT_FAILURE);
96 }
97 fprintf (stderr, "0x%08lx\n", regs.regs.eip);
98 } while (1);
99
100 if (ptrace (PTRACE_DETACH, fpid, NULL, NULL) < 0) {
101 perror ("ptrace PTRACE_DETACH");
102 exit (EXIT_FAILURE);
103 }
104
105 fprintf (stderr, "MEMDUMP: success. terminating.\n");
106 exit (EXIT_SUCCESS);
107}
108
109
110
111void
112hexdump (unsigned char *data, unsigned int amount)
113{
114 unsigned int dp, p; /* data pointer */
115 const char trans[] =
116 "................................ !\"#$%&'()*+,-./0123456789"
117 ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
118 "nopqrstuvwxyz{|}~...................................."
119 "....................................................."
120 "........................................";
121
122 for (dp = 1; dp <= amount; dp++) {
123 printf ("%02x ", data[dp-1]);
124 if ((dp % 8) == 0)
125 printf (" ");
126 if ((dp % 16) == 0) {
127 printf ("| ");
128 p = dp;
129 for (dp -= 16; dp < p; dp++)
130 printf ("%c", trans[data[dp]]);
131 printf ("\n");
132 }
133 }
134 if ((amount % 16) != 0) {
135 p = dp = 16 - (amount % 16);
136 for (dp = p; dp > 0; dp--) {
137 printf (" ");
138 if (((dp % 8) == 0) && (p != 8))
139 printf (" ");
140 }
141 printf (" | ");
142 for (dp = (amount - (16 - p)); dp < amount; dp++)
143 printf ("%c", trans[data[dp]]);
144 }
145 printf ("\n");
146
147 return;
148}