1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
/* Interface to bare machine for GDB running as kernel debugger.
Copyright (C) 1986 Free Software Foundation, Inc.
GDB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY. No author or distributor accepts responsibility to anyone
for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.
Refer to the GDB General Public License for full details.
Everyone is granted permission to copy, modify and redistribute GDB,
but only under the conditions described in the GDB General Public
License. A copy of this license is supposed to have been given to you
along with GDB so you can know your rights and responsibilities. It
should be in a file named COPYING. Among other things, the copyright
notice and this notice must be preserved on all copies.
In other words, go ahead and share GDB, but don't try to stop
anyone else from sharing it farther. Help stamp out software hoarding!
*/
#include <stdio.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined (SIGTSTP) && defined (SIGIO)
#include <sys/time.h>
#include <sys/resource.h>
#endif /* SIGTSTP and SIGIO defined (must be 4.2) */
#include "defs.h"
#include "param.h"
#include "symtab.h"
#include "frame.h"
#include "inferior.h"
#include "wait.h"
/* Random system calls, mostly no-ops to prevent link problems */
ioctl (desc, code, arg)
{}
int (* signal ()) ()
{}
kill ()
{}
getpid ()
{
return 0;
}
sigsetmask ()
{}
chdir ()
{}
char *
getwd (buf)
char *buf;
{
buf[0] = '/';
buf[1] = 0;
return buf;
}
/* Used to check for existence of .gdbinit. Say no. */
access ()
{
return -1;
}
exit ()
{
error ("Fatal error; restarting.");
}
/* Reading "files". The contents of some files are written into kdb's
data area before it is run. These files are used to contain the
symbol table for kdb to load, and the source files (in case the
kdb user wants to print them). The symbols are stored in a file
named "kdb-symbols" in a.out format (except that all the text and
data have been stripped to save room).
The files are stored in the following format:
int number of bytes of data for this file, including these four.
char[] name of the file, ending with a null.
padding to multiple of 4 boundary.
char[] file contents. The length can be deduced from what was
specified before. There is no terminating null here.
If the int at the front is zero, it means there are no more files.
Opening a file in kdb returns a nonzero value to indicate success,
but the value does not matter. Only one file can be open, and only
for reading. All the primitives for input from the file know
which file is open and ignore what is specified for the descriptor
or for the stdio stream.
Input with fgetc can be done either on the file that is open
or on stdin (which reads from the terminal through tty_input () */
/* Address of data for the files stored in format described above. */
char *files_start;
/* The file stream currently open: */
char *sourcebeg; /* beginning of contents */
int sourcesize; /* size of contents */
char *sourceptr; /* current read pointer */
int sourceleft; /* number of bytes to eof */
/* "descriptor" for the file now open.
Incremented at each close.
If specified descriptor does not match this,
it means the program is trying to use a closed descriptor.
We report an error for that. */
int sourcedesc;
open (filename, modes)
char *filename;
int modes;
{
register char *next;
extern int errno;
if (modes)
{
errno = EROFS;
return -1;
}
if (sourceptr)
{
errno = EMFILE;
return -1;
}
for (next - files_start; * (int *) next;
next += * (int *) next)
{
if (!strcmp (next + 4, filename))
{
sourcebeg = next + 4 + strlen (next + 4) + 1;
sourcebeg = (char *) (((int) sourcebeg + 3) & (-4));
sourceptr = sourcebeg;
sourcesize = next + * (int *) next - sourceptr;
sourceleft = sourcesize;
return sourcedesc;
}
}
return 0;
}
close (desc)
int desc;
{
sourceptr = 0;
sourcedesc++;
/* Don't let sourcedesc get big enough to be confused with stdin. */
if (sourcedesc == 100)
sourcedesc = 5;
}
FILE *
fopen (filename, modes)
char *filename;
char *modes;
{
return (FILE *) open (filename, *modes == 'w');
}
FILE *
fdopen (desc)
int desc;
{
return (FILE *) desc;
}
fclose (desc)
int desc;
{
close (desc);
}
fstat (desc, statbuf)
struct stat *statbuf;
{
extern int errno;
if (desc != sourcedesc)
{
errno = EBADF;
return -1;
}
statbuf->st_size = sourcesize;
}
myread (desc, destptr, size, filename)
int desc;
char *destptr;
int size;
char *filename;
{
int len = min (sourceleft, size);
extern int errno;
if (desc != sourcedesc)
{
errno = EBADF;
return -1;
}
bcopy (sourceptr, destptr, len);
sourceleft -= len;
return len;
}
int
fread (bufp, numelts, eltsize, stream)
{
register int elts = min (numelts, sourceleft / eltsize);
register int len = elts * eltsize;
extern int errno;
if (stream != sourcedesc)
{
errno = EBADF;
return -1;
}
bcopy (sourceptr, bufp, len);
sourceleft -= len;
return elts;
}
int
fgetc (desc)
int desc;
{
extern int errno;
if (desc == (int) stdin)
return tty_input ();
if (desc != sourcedesc)
{
errno = EBADF;
return -1;
}
if (sourceleft-- <= 0)
return EOF;
return *sourceptr++;
}
lseek (desc, pos)
int desc;
int pos;
{
extern int errno;
if (desc != sourcedesc)
{
errno = EBADF;
return -1;
}
if (pos < 0 || pos > sourcesize)
{
errno = EINVAL;
return -1;
}
sourceptr = sourcebeg + pos;
sourceleft = sourcesize - pos;
}
/* Output in kdb can go only to the terminal, so the stream
specified may be ignored. */
printf (a1, a2, a3, a4, a5, a6, a7, a8, a9)
{
char buffer[1024];
sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
display_string (buffer);
}
fprintf (ign, a1, a2, a3, a4, a5, a6, a7, a8, a9)
{
char buffer[1024];
sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
display_string (buffer);
}
fwrite (buf, numelts, size, stream)
register char *buf;
int numelts, size;
{
register int i = numelts * size;
while (i-- > 0)
fputc (*buf++, stream);
}
fputc (c, ign)
{
char buf[2];
buf[0] = c;
buf[1] = 0;
display_string (buf);
}
/* sprintf refers to this, but loading this from the
library would cause fflush to be loaded from it too.
In fact there should be no need to call this (I hope). */
_flsbuf ()
{
error ("_flsbuf was actually called.");
}
fflush (ign)
{
}
/* Entries into core and inflow, needed only to make things link ok. */
exec_file_command ()
{}
core_file_command ()
{}
char *
get_exec_file (err)
int err;
{
/* Makes one printout look reasonable; value does not matter otherwise. */
return "run";
}
have_core_file_p ()
{
return 0;
}
kill_command ()
{
inferior_pid = 0;
}
terminal_inferior ()
{}
terminal_ours ()
{}
terminal_init_inferior ()
{}
write_inferior_register ()
{}
read_inferior_register ()
{}
read_memory (memaddr, myaddr, len)
CORE_ADDR memaddr;
char *myaddr;
int len;
{
bcopy (memaddr, myaddr, len);
}
/* Always return 0 indicating success. */
write_memory (memaddr, myaddr, len)
CORE_ADDR memaddr;
char *myaddr;
int len;
{
bcopy (myaddr, memaddr, len);
return 0;
}
static REGISTER_TYPE saved_regs[NUM_REGS];
REGISTER_TYPE
read_register (regno)
int regno;
{
if (regno < 0 || regno >= NUM_REGS)
error ("Register number %d out of range.", regno);
return saved_regs[regno];
}
void
write_register (regno, value)
int regno;
REGISTER_TYPE value;
{
if (regno < 0 || regno >= NUM_REGS)
error ("Register number %d out of range.", regno);
saved_regs[regno] = value;
}
/* System calls needed in relation to running the "inferior". */
vfork ()
{
/* Just appear to "succeed". Say the inferior's pid is 1. */
return 1;
}
/* These are called by code that normally runs in the inferior
that has just been forked. That code never runs, when standalone,
and these definitions are so it will link without errors. */
ptrace ()
{}
setpgrp ()
{}
execle ()
{}
_exit ()
{}
/* Malloc calls these. */
malloc_warning (str)
char *str;
{
printf ("\n%s.\n\n", str);
}
char *next_free;
char *memory_limit;
char *
sbrk (amount)
int amount;
{
if (next_free + amount > memory_limit)
return (char *) -1;
next_free += amount;
return next_free - amount;
}
/* Various ways malloc might ask where end of memory is. */
char *
ulimit ()
{
return memory_limit;
}
int
vlimit ()
{
return memory_limit - next_free;
}
getrlimit (addr)
struct rlimit *addr;
{
addr->rlim_cur = memory_limit - next_free;
}
/* Context switching to and from program being debugged. */
/* GDB calls here to run the user program.
The frame pointer for this function is saved in
gdb_stack by save_frame_pointer; then we restore
all of the user program's registers, including PC and PS. */
static int fault_code;
static REGISTER_TYPE gdb_stack;
resume ()
{
REGISTER_TYPE restore[NUM_REGS];
PUSH_FRAME_PTR;
save_frame_pointer ();
bcopy (saved_regs, restore, sizeof restore);
POP_REGISTERS;
/* Control does not drop through here! */
}
save_frame_pointer (val)
CORE_ADDR val;
{
gdb_stack = val;
}
/* Fault handlers call here, running in the user program stack.
They must first push a fault code,
old PC, old PS, and any other info about the fault.
The exact format is machine-dependent and is known only
in the definition of PUSH_REGISTERS. */
fault ()
{
/* Transfer all registers and fault code to the stack
in canonical order: registers in order of GDB register number,
followed by fault code. */
PUSH_REGISTERS;
/* Transfer them to saved_regs and fault_code. */
save_registers ();
restore_gdb ();
/* Control does not reach here */
}
restore_gdb ()
{
CORE_ADDR new_fp = gdb_stack;
/* Switch to GDB's stack */
POP_FRAME_PTR;
/* Return from the function `resume'. */
}
/* Assuming register contents and fault code have been pushed on the stack as
arguments to this function, copy them into the standard place
for the program's registers while GDB is running. */
save_registers (firstreg)
int firstreg;
{
bcopy (&firstreg, saved_regs, sizeof saved_regs);
fault_code = (&firstreg)[NUM_REGS];
}
/* Store into the structure such as `wait' would return
the information on why the program faulted,
converted into a machine-independent signal number. */
static int fault_table[] = FAULT_TABLE;
int
wait (w)
WAITTYPE *w;
{
WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]);
return inferior_pid;
}
/* Allocate a big space in which files for kdb to read will be stored.
Whatever is left is where malloc can allocate storage.
Initialize it, so that there will be space in the executable file
for it. Then the files can be put into kdb by writing them into
kdb's executable file. */
/* The default size is as much space as we expect to be available
for kdb to use! */
#ifndef HEAP_SIZE
#define HEAP_SIZE 400000
#endif
char heap[HEAP_SIZE] = {0};
#ifndef STACK_SIZE
#define STACK_SIZE 100000
#endif
int kdb_stack_beg[STACK_SIZE / sizeof (int)];
int kdb_stack_end;
_initialize_standalone ()
{
register char *next;
/* Find start of data on files. */
files_start = heap;
/* Find the end of the data on files. */
for (next - files_start; * (int *) next;
next += * (int *) next)
{}
/* That is where free storage starts for sbrk to give out. */
next_free = next;
memory_limit = heap + sizeof heap;
}
|