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