OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [infptrace.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 104 markom
/* Low level Unix child interface to ptrace, for GDB when running under Unix.
2
   Copyright 1988, 89, 90, 91, 92, 93, 94, 95, 96, 1998
3
   Free Software Foundation, Inc.
4
 
5
   This file is part of GDB.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 2 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 59 Temple Place - Suite 330,
20
   Boston, MA 02111-1307, USA.  */
21
 
22
#include "defs.h"
23
#include "frame.h"
24
#include "inferior.h"
25
#include "target.h"
26
#include "gdb_string.h"
27
 
28
#include "gdb_wait.h"
29
 
30
#include "command.h"
31
 
32
#ifdef USG
33
#include <sys/types.h>
34
#endif
35
 
36
#include <sys/param.h>
37
#include <sys/dir.h>
38
#include <signal.h>
39
#include <sys/ioctl.h>
40
 
41
#ifdef HAVE_PTRACE_H
42
#include <ptrace.h>
43
#else
44
#ifdef HAVE_SYS_PTRACE_H
45
#include <sys/ptrace.h>
46
#endif
47
#endif
48
 
49
#if !defined (PT_READ_I)
50
#define PT_READ_I       1       /* Read word from text space */
51
#endif
52
#if !defined (PT_READ_D)
53
#define PT_READ_D       2       /* Read word from data space */
54
#endif
55
#if !defined (PT_READ_U)
56
#define PT_READ_U       3       /* Read word from kernel user struct */
57
#endif
58
#if !defined (PT_WRITE_I)
59
#define PT_WRITE_I      4       /* Write word to text space */
60
#endif
61
#if !defined (PT_WRITE_D)
62
#define PT_WRITE_D      5       /* Write word to data space */
63
#endif
64
#if !defined (PT_WRITE_U)
65
#define PT_WRITE_U      6       /* Write word to kernel user struct */
66
#endif
67
#if !defined (PT_CONTINUE)
68
#define PT_CONTINUE     7       /* Continue after signal */
69
#endif
70
#if !defined (PT_STEP)
71
#define PT_STEP         9       /* Set flag for single stepping */
72
#endif
73
#if !defined (PT_KILL)
74
#define PT_KILL         8       /* Send child a SIGKILL signal */
75
#endif
76
 
77
#ifndef PT_ATTACH
78
#define PT_ATTACH PTRACE_ATTACH
79
#endif
80
#ifndef PT_DETACH
81
#define PT_DETACH PTRACE_DETACH
82
#endif
83
 
84
#include "gdbcore.h"
85
#ifndef NO_SYS_FILE
86
#include <sys/file.h>
87
#endif
88
#if 0
89
/* Don't think this is used anymore.  On the sequent (not sure whether it's
90
   dynix or ptx or both), it is included unconditionally by sys/user.h and
91
   not protected against multiple inclusion.  */
92
#include "gdb_stat.h"
93
#endif
94
 
95
#if !defined (FETCH_INFERIOR_REGISTERS)
96
#include <sys/user.h>           /* Probably need to poke the user structure */
97
#if defined (KERNEL_U_ADDR_BSD)
98
#include <a.out.h>              /* For struct nlist */
99
#endif /* KERNEL_U_ADDR_BSD.  */
100
#endif /* !FETCH_INFERIOR_REGISTERS */
101
 
102
#if !defined (CHILD_XFER_MEMORY)
103
static void udot_info PARAMS ((char *, int));
104
#endif
105
 
106
#if !defined (FETCH_INFERIOR_REGISTERS)
107
static void fetch_register PARAMS ((int));
108
static void store_register PARAMS ((int));
109
#endif
110
 
111
/*
112
 * Some systems (Linux) may have threads implemented as pseudo-processes,
113
 * in which case we may be tracing more than one process at a time.
114
 * In that case, inferior_pid will contain the main process ID and the
115
 * individual thread (process) id mashed together.  These macros are
116
 * used to separate them out.  The definitions may be overridden in tm.h
117
 *
118
 * NOTE: default definitions here are for systems with no threads.
119
 * Useful definitions MUST be provided in tm.h
120
 */
121
 
122
#if !defined (PIDGET)   /* Default definition for PIDGET/TIDGET.  */
123
#define PIDGET(PID)     PID
124
#define TIDGET(PID)     0
125
#endif
126
 
127
void _initialize_kernel_u_addr PARAMS ((void));
128
void _initialize_infptrace PARAMS ((void));
129
 
130
 
131
/* This function simply calls ptrace with the given arguments.
132
   It exists so that all calls to ptrace are isolated in this
133
   machine-dependent file. */
134
int
135
call_ptrace (request, pid, addr, data)
136
     int request, pid;
137
     PTRACE_ARG3_TYPE addr;
138
     int data;
139
{
140
  int pt_status = 0;
141
 
142
#if 0
143
  int saved_errno;
144
 
145
  printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
146
          request, pid, addr, data);
147
#endif
148
#if defined(PT_SETTRC)
149
  /* If the parent can be told to attach to us, try to do it.  */
150
  if (request == PT_SETTRC)
151
    {
152
      errno = 0;
153
#if !defined (FIVE_ARG_PTRACE)
154
      pt_status = ptrace (PT_SETTRC, pid, addr, data);
155
#else
156
      /* Deal with HPUX 8.0 braindamage.  We never use the
157
         calls which require the fifth argument.  */
158
      pt_status = ptrace (PT_SETTRC, pid, addr, data, 0);
159
#endif
160
      if (errno)
161
        perror_with_name ("ptrace");
162
#if 0
163
      printf (" = %d\n", pt_status);
164
#endif
165
      if (pt_status < 0)
166
        return pt_status;
167
      else
168
        return parent_attach_all (pid, addr, data);
169
    }
170
#endif
171
 
172
#if defined(PT_CONTIN1)
173
  /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
174
     signals.  If it's available, use it.  */
175
  if (request == PT_CONTINUE)
176
    request = PT_CONTIN1;
177
#endif
178
 
179
#if defined(PT_SINGLE1)
180
  /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
181
     signals.  If it's available, use it.  */
182
  if (request == PT_STEP)
183
    request = PT_SINGLE1;
184
#endif
185
 
186
#if 0
187
  saved_errno = errno;
188
  errno = 0;
189
#endif
190
#if !defined (FIVE_ARG_PTRACE)
191
  pt_status = ptrace (request, pid, addr, data);
192
#else
193
  /* Deal with HPUX 8.0 braindamage.  We never use the
194
     calls which require the fifth argument.  */
195
  pt_status = ptrace (request, pid, addr, data, 0);
196
#endif
197
 
198
#if 0
199
  if (errno)
200
    printf (" [errno = %d]", errno);
201
 
202
  errno = saved_errno;
203
  printf (" = 0x%x\n", pt_status);
204
#endif
205
  return pt_status;
206
}
207
 
208
 
209
#if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
210
/* For the rest of the file, use an extra level of indirection */
211
/* This lets us breakpoint usefully on call_ptrace. */
212
#define ptrace call_ptrace
213
#endif
214
 
215
/* Wait for a process to finish, possibly running a target-specific
216
   hook before returning.  */
217
 
218
int
219
ptrace_wait (pid, status)
220
     int pid;
221
     int *status;
222
{
223
  int wstate;
224
 
225
  wstate = wait (status);
226
  target_post_wait (wstate, *status);
227
  return wstate;
228
}
229
 
230
void
231
kill_inferior ()
232
{
233
  int status;
234
 
235
  if (inferior_pid == 0)
236
    return;
237
 
238
  /* This once used to call "kill" to kill the inferior just in case
239
     the inferior was still running.  As others have noted in the past
240
     (kingdon) there shouldn't be any way to get here if the inferior
241
     is still running -- else there's a major problem elsewere in gdb
242
     and it needs to be fixed.
243
 
244
     The kill call causes problems under hpux10, so it's been removed;
245
     if this causes problems we'll deal with them as they arise.  */
246
  ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
247
  ptrace_wait (0, &status);
248
  target_mourn_inferior ();
249
}
250
 
251
#ifndef CHILD_RESUME
252
 
253
/* Resume execution of the inferior process.
254
   If STEP is nonzero, single-step it.
255
   If SIGNAL is nonzero, give it that signal.  */
256
 
257
void
258
child_resume (pid, step, signal)
259
     int pid;
260
     int step;
261
     enum target_signal signal;
262
{
263
  errno = 0;
264
 
265
  if (pid == -1)
266
    /* Resume all threads.  */
267
    /* I think this only gets used in the non-threaded case, where "resume
268
       all threads" and "resume inferior_pid" are the same.  */
269
    pid = inferior_pid;
270
 
271
  /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
272
     it was.  (If GDB wanted it to start some other way, we have already
273
     written a new PC value to the child.)
274
 
275
     If this system does not support PT_STEP, a higher level function will
276
     have called single_step() to transmute the step request into a
277
     continue request (by setting breakpoints on all possible successor
278
     instructions), so we don't have to worry about that here.  */
279
 
280
  if (step)
281
    {
282
      if (SOFTWARE_SINGLE_STEP_P)
283
        abort ();               /* Make sure this doesn't happen. */
284
      else
285
        ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
286
                target_signal_to_host (signal));
287
    }
288
  else
289
    ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
290
            target_signal_to_host (signal));
291
 
292
  if (errno)
293
    {
294
      perror_with_name ("ptrace");
295
    }
296
}
297
#endif /* CHILD_RESUME */
298
 
299
 
300
#ifdef ATTACH_DETACH
301
/* Start debugging the process whose number is PID.  */
302
int
303
attach (pid)
304
     int pid;
305
{
306
  errno = 0;
307
  ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
308
  if (errno)
309
    perror_with_name ("ptrace");
310
  attach_flag = 1;
311
  return pid;
312
}
313
 
314
/* Stop debugging the process whose number is PID
315
   and continue it with signal number SIGNAL.
316
   SIGNAL = 0 means just continue it.  */
317
 
318
void
319
detach (signal)
320
     int signal;
321
{
322
  errno = 0;
323
  ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
324
  if (errno)
325
    perror_with_name ("ptrace");
326
  attach_flag = 0;
327
}
328
#endif /* ATTACH_DETACH */
329
 
330
/* Default the type of the ptrace transfer to int.  */
331
#ifndef PTRACE_XFER_TYPE
332
#define PTRACE_XFER_TYPE int
333
#endif
334
 
335
/* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
336
   to get the offset in the core file of the register values.  */
337
#if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
338
/* Get kernel_u_addr using BSD-style nlist().  */
339
CORE_ADDR kernel_u_addr;
340
#endif /* KERNEL_U_ADDR_BSD.  */
341
 
342
void
343
_initialize_kernel_u_addr ()
344
{
345
#if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
346
  struct nlist names[2];
347
 
348
  names[0].n_un.n_name = "_u";
349
  names[1].n_un.n_name = NULL;
350
  if (nlist ("/vmunix", names) == 0)
351
    kernel_u_addr = names[0].n_value;
352
  else
353
    internal_error ("Unable to get kernel u area address.");
354
#endif /* KERNEL_U_ADDR_BSD.  */
355
}
356
 
357
#if !defined (FETCH_INFERIOR_REGISTERS)
358
 
359
#if !defined (offsetof)
360
#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
361
#endif
362
 
363
/* U_REGS_OFFSET is the offset of the registers within the u area.  */
364
#if !defined (U_REGS_OFFSET)
365
#define U_REGS_OFFSET \
366
  ptrace (PT_READ_U, inferior_pid, \
367
          (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
368
    - KERNEL_U_ADDR
369
#endif
370
 
371
/* Registers we shouldn't try to fetch.  */
372
#if !defined (CANNOT_FETCH_REGISTER)
373
#define CANNOT_FETCH_REGISTER(regno) 0
374
#endif
375
 
376
/* Fetch one register.  */
377
 
378
static void
379
fetch_register (regno)
380
     int regno;
381
{
382
  /* This isn't really an address.  But ptrace thinks of it as one.  */
383
  CORE_ADDR regaddr;
384
  char mess[128];               /* For messages */
385
  register int i;
386
  unsigned int offset;          /* Offset of registers within the u area.  */
387
  char buf[MAX_REGISTER_RAW_SIZE];
388
  int tid;
389
 
390
  if (CANNOT_FETCH_REGISTER (regno))
391
    {
392
      memset (buf, '\0', REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
393
      supply_register (regno, buf);
394
      return;
395
    }
396
 
397
  /* Overload thread id onto process id */
398
  if ((tid = TIDGET (inferior_pid)) == 0)
399
    tid = inferior_pid;         /* no thread id, just use process id */
400
 
401
  offset = U_REGS_OFFSET;
402
 
403
  regaddr = register_addr (regno, offset);
404
  for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
405
    {
406
      errno = 0;
407
      *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
408
                                               (PTRACE_ARG3_TYPE) regaddr, 0);
409
      regaddr += sizeof (PTRACE_XFER_TYPE);
410
      if (errno != 0)
411
        {
412
          sprintf (mess, "reading register %s (#%d)",
413
                   REGISTER_NAME (regno), regno);
414
          perror_with_name (mess);
415
        }
416
    }
417
  supply_register (regno, buf);
418
}
419
 
420
 
421
/* Fetch register values from the inferior.
422
   If REGNO is negative, do this for all registers.
423
   Otherwise, REGNO specifies which register (so we can save time). */
424
 
425
void
426
fetch_inferior_registers (regno)
427
     int regno;
428
{
429
  if (regno >= 0)
430
    {
431
      fetch_register (regno);
432
    }
433
  else
434
    {
435
      for (regno = 0; regno < ARCH_NUM_REGS; regno++)
436
        {
437
          fetch_register (regno);
438
        }
439
    }
440
}
441
 
442
/* Registers we shouldn't try to store.  */
443
#if !defined (CANNOT_STORE_REGISTER)
444
#define CANNOT_STORE_REGISTER(regno) 0
445
#endif
446
 
447
/* Store one register. */
448
 
449
static void
450
store_register (regno)
451
     int regno;
452
{
453
  /* This isn't really an address.  But ptrace thinks of it as one.  */
454
  CORE_ADDR regaddr;
455
  char mess[128];               /* For messages */
456
  register int i;
457
  unsigned int offset;          /* Offset of registers within the u area.  */
458
  int tid;
459
 
460
  if (CANNOT_STORE_REGISTER (regno))
461
    {
462
      return;
463
    }
464
 
465
  /* Overload thread id onto process id */
466
  if ((tid = TIDGET (inferior_pid)) == 0)
467
    tid = inferior_pid;         /* no thread id, just use process id */
468
 
469
  offset = U_REGS_OFFSET;
470
 
471
  regaddr = register_addr (regno, offset);
472
  for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
473
    {
474
      errno = 0;
475
      ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
476
              *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
477
      regaddr += sizeof (PTRACE_XFER_TYPE);
478
      if (errno != 0)
479
        {
480
          sprintf (mess, "writing register %s (#%d)",
481
                   REGISTER_NAME (regno), regno);
482
          perror_with_name (mess);
483
        }
484
    }
485
}
486
 
487
/* Store our register values back into the inferior.
488
   If REGNO is negative, do this for all registers.
489
   Otherwise, REGNO specifies which register (so we can save time).  */
490
 
491
void
492
store_inferior_registers (regno)
493
     int regno;
494
{
495
  if (regno >= 0)
496
    {
497
      store_register (regno);
498
    }
499
  else
500
    {
501
      for (regno = 0; regno < ARCH_NUM_REGS; regno++)
502
        {
503
          store_register (regno);
504
        }
505
    }
506
}
507
#endif /* !defined (FETCH_INFERIOR_REGISTERS).  */
508
 
509
 
510
#if !defined (CHILD_XFER_MEMORY)
511
/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
512
   in the NEW_SUN_PTRACE case.
513
   It ought to be straightforward.  But it appears that writing did
514
   not write the data that I specified.  I cannot understand where
515
   it got the data that it actually did write.  */
516
 
517
/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
518
   to debugger memory starting at MYADDR.   Copy to inferior if
519
   WRITE is nonzero.
520
 
521
   Returns the length copied, which is either the LEN argument or zero.
522
   This xfer function does not do partial moves, since child_ops
523
   doesn't allow memory operations to cross below us in the target stack
524
   anyway.  */
525
 
526
int
527
child_xfer_memory (memaddr, myaddr, len, write, target)
528
     CORE_ADDR memaddr;
529
     char *myaddr;
530
     int len;
531
     int write;
532
     struct target_ops *target; /* ignored */
533
{
534
  register int i;
535
  /* Round starting address down to longword boundary.  */
536
  register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE);
537
  /* Round ending address up; get number of longwords that makes.  */
538
  register int count
539
  = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
540
  / sizeof (PTRACE_XFER_TYPE);
541
  /* Allocate buffer of that many longwords.  */
542
  register PTRACE_XFER_TYPE *buffer
543
  = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
544
 
545
  if (write)
546
    {
547
      /* Fill start and end extra bytes of buffer with existing memory data.  */
548
 
549
      if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE))
550
        {
551
          /* Need part of initial word -- fetch it.  */
552
          buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_pid),
553
                              (PTRACE_ARG3_TYPE) addr, 0);
554
        }
555
 
556
      if (count > 1)            /* FIXME, avoid if even boundary */
557
        {
558
          buffer[count - 1]
559
            = ptrace (PT_READ_I, PIDGET (inferior_pid),
560
                      ((PTRACE_ARG3_TYPE)
561
                       (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
562
                      0);
563
        }
564
 
565
      /* Copy data to be written over corresponding part of buffer */
566
 
567
      memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
568
              myaddr,
569
              len);
570
 
571
      /* Write the entire buffer.  */
572
 
573
      for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
574
        {
575
          errno = 0;
576
          ptrace (PT_WRITE_D, PIDGET (inferior_pid),
577
                  (PTRACE_ARG3_TYPE) addr, buffer[i]);
578
          if (errno)
579
            {
580
              /* Using the appropriate one (I or D) is necessary for
581
                 Gould NP1, at least.  */
582
              errno = 0;
583
              ptrace (PT_WRITE_I, PIDGET (inferior_pid),
584
                      (PTRACE_ARG3_TYPE) addr, buffer[i]);
585
            }
586
          if (errno)
587
            return 0;
588
        }
589
#ifdef CLEAR_INSN_CACHE
590
      CLEAR_INSN_CACHE ();
591
#endif
592
    }
593
  else
594
    {
595
      /* Read all the longwords */
596
      for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
597
        {
598
          errno = 0;
599
          buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_pid),
600
                              (PTRACE_ARG3_TYPE) addr, 0);
601
          if (errno)
602
            return 0;
603
          QUIT;
604
        }
605
 
606
      /* Copy appropriate bytes out of the buffer.  */
607
      memcpy (myaddr,
608
              (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
609
              len);
610
    }
611
  return len;
612
}
613
 
614
 
615
static void
616
udot_info (dummy1, dummy2)
617
     char *dummy1;
618
     int dummy2;
619
{
620
#if defined (KERNEL_U_SIZE)
621
  int udot_off;                 /* Offset into user struct */
622
  int udot_val;                 /* Value from user struct at udot_off */
623
  char mess[128];               /* For messages */
624
#endif
625
 
626
  if (!target_has_execution)
627
    {
628
      error ("The program is not being run.");
629
    }
630
 
631
#if !defined (KERNEL_U_SIZE)
632
 
633
  /* Adding support for this command is easy.  Typically you just add a
634
     routine, called "kernel_u_size" that returns the size of the user
635
     struct, to the appropriate *-nat.c file and then add to the native
636
     config file "#define KERNEL_U_SIZE kernel_u_size()" */
637
  error ("Don't know how large ``struct user'' is in this version of gdb.");
638
 
639
#else
640
 
641
  for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
642
    {
643
      if ((udot_off % 24) == 0)
644
        {
645
          if (udot_off > 0)
646
            {
647
              printf_filtered ("\n");
648
            }
649
          printf_filtered ("%04x:", udot_off);
650
        }
651
      udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0);
652
      if (errno != 0)
653
        {
654
          sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
655
          perror_with_name (mess);
656
        }
657
      /* Avoid using nonportable (?) "*" in print specs */
658
      printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
659
    }
660
  printf_filtered ("\n");
661
 
662
#endif
663
}
664
#endif /* !defined (CHILD_XFER_MEMORY).  */
665
 
666
 
667
void
668
_initialize_infptrace ()
669
{
670
#if !defined (CHILD_XFER_MEMORY)
671
  add_info ("udot", udot_info,
672
            "Print contents of kernel ``struct user'' for current child.");
673
#endif
674
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.