summaryrefslogtreecommitdiffstats
path: root/c/src/librdbg/src/_servtgt.c
blob: 1ae011542b997ad23b50abf50a0d2fd7172c31ff (plain) (blame)
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
/*
 ============================================================================
 _SERVTGT
 $Id$
 ============================================================================
*/

#include <string.h>
#include <rtems.h>
#include <rtems/error.h>

#include <rdbg/rdbg.h>
#include <rdbg/rdbg_f.h>
#include <rdbg/servrpc.h>
#include <errno.h>
#include <sys/socket.h>
#include <assert.h>
#include <rtems/score/cpu.h>

#ifdef DDEBUG
#define Ptrace	TgtDbgPtrace
#else
#define Ptrace	TgtRealPtrace
#endif

extern int errno;

rtems_id eventTaskId;
rtems_id serializeSemId;
rtems_id wakeupEventSemId;
rtems_id debugId;

CPU_Exception_frame Idle_frame;

/* -----------------------------------------------------------------
   TgtRealPtrace - lowest level ptrace() wrapper
   ----------------------------------------------------------------- */

  int
TgtRealPtrace (int req, PID aid, char *addr, int d, void *addr2)
{
  return ptrace (req, aid, addr, d, addr2);
}

/* -----------------------------------------------------------------------
   TgtChange() is called when the system stops.
   It informs the generic layers must be informed of
   that fact. 
   ----------------------------------------------------------------------- */

  static int
TgtChange (PID pid, CPU_Exception_frame * ctx, int status)
{

  if (TgtHandleChildChange (pid, &status, NULL, ctx)) {
    TgtNotifyWaitChange (pid, status, -1);
  }

  return 0;
}

/* -----------------------------------------------------------------------
   eventTask
   ----------------------------------------------------------------------- */

  rtems_task
eventTask (rtems_task_argument pid)
{
  Exception_context *ctx;

  DPRINTF (("event task: pid %d\n", pid));

  /*
   * we spend all our time waiting for a semaphore.
   * If wait change, we send info
   */

  for (;;) {
    DPRINTF (("Event Task: wait event\n"));
    rtems_semaphore_obtain (wakeupEventSemId, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    DPRINTF (("Event Task: wake up !!!!!!!!!!!!!\n"));

    errno = 0;
    ctx = GetExceptCtx (currentTargetThread);

    CheckForSingleStep (ctx->ctx);

    TgtChange (pid, ctx->ctx, STS_MAKESIG (ExcepToSig (ctx)));
  }
}

/* -------------------------------------------------------------------
   MyThreadIdle -

   This task is used to initiate the exception mechanism:
   It calls the enterDebug function with justSaveContext=1
   only to push a first valid context in the list
   ---------------------------------------------------------------------*/

  rtems_task
MyThreadIdle (rtems_task_argument argument)
{
  enterRdbg ();
  rtems_task_delete (RTEMS_SELF);
}

/* -----------------------------------------------------------------------
   TgtAttach - attach to a process that is running without control.

   Notes:
      - this function performs a ptrace ATTACH equivalent (attaching to a
	process that we do not control now).
   ----------------------------------------------------------------------- */

  Boolean
TgtAttach (int conn_idx,        /* client that is requesting */
           PID pid)
{                               /* process to attach to */
  rtems_name task_name;
  rtems_status_code status;

  errno = 0;

  DPRINTF (("TgtAttach pid=%d\n", pid));

  Ptrace (RPT_ATTACH, pid, NULL, 0, NULL);
  if (errno)
    return (False);             /* failed */

  TgtCreateNew (pid, conn_idx, 0, NULL, False);

  connect_rdbg_exception ();

  /*
   * Search for and remove debugging tasks / semaphores left over from
   * a previous attach without a detach.
   */

  if (rtems_task_ident (rtems_build_name ('E', 'v', 'n', 't'),
                        RTEMS_SEARCH_ALL_NODES,
                        &eventTaskId) == RTEMS_SUCCESSFUL)
    rtems_task_delete (eventTaskId);

  if (rtems_semaphore_ident (rtems_build_name ('D', 'B', 'G', 's'),
                             RTEMS_SEARCH_ALL_NODES,
                             &serializeSemId) == RTEMS_SUCCESSFUL)
    rtems_semaphore_delete (serializeSemId);

  if (rtems_semaphore_ident (rtems_build_name ('D', 'B', 'G', 'w'),
                             RTEMS_SEARCH_ALL_NODES,
                             &wakeupEventSemId) == RTEMS_SUCCESSFUL)
    rtems_semaphore_delete (wakeupEventSemId);

  /*
   * Create the attach debugger task
   */
  task_name = rtems_build_name ('E', 'v', 'n', 't');
  if ((status = rtems_task_create (task_name, 10, 24576,
                                   RTEMS_INTERRUPT_LEVEL (0),
                                   RTEMS_DEFAULT_ATTRIBUTES |
                                   RTEMS_SYSTEM_TASK, &eventTaskId))
      != RTEMS_SUCCESSFUL) {
    printf ("status = %d\n", status);
    rtems_panic ("Can't create task.\n");
  }

  status = rtems_task_start (eventTaskId, eventTask, pid);

  status = rtems_semaphore_create (rtems_build_name ('D', 'B', 'G', 's'),
                                   1,
                                   RTEMS_FIFO |
                                   RTEMS_COUNTING_SEMAPHORE |
                                   RTEMS_NO_INHERIT_PRIORITY |
                                   RTEMS_NO_PRIORITY_CEILING |
                                   RTEMS_LOCAL, 0, &serializeSemId);
  if (status != RTEMS_SUCCESSFUL)
    rtems_panic ("Can't create serialize semaphore: `%s'\n",
                 rtems_status_text (status));

  status = rtems_semaphore_create (rtems_build_name ('D', 'B', 'G', 'w'),
                                   0,
                                   RTEMS_FIFO |
                                   RTEMS_COUNTING_SEMAPHORE |
                                   RTEMS_NO_INHERIT_PRIORITY |
                                   RTEMS_NO_PRIORITY_CEILING |
                                   RTEMS_LOCAL, 0, &wakeupEventSemId);
  if (status != RTEMS_SUCCESSFUL)
    rtems_panic ("Can't create wakeup semaphore: `%s'\n",
                 rtems_status_text (status));

  /*
   * Create the MyThreadIdle task to init Exception mechanism 
   */
  task_name = rtems_build_name ('R', 'i', 'n', 'i');
  if ((status = rtems_task_create (task_name, 10, 24576,
                                   RTEMS_INTERRUPT_LEVEL (0),
                                   RTEMS_DEFAULT_ATTRIBUTES, &debugId))
      != RTEMS_SUCCESSFUL) {
    printf ("status = %d (%s)\n", status, rtems_status_text (status));
    rtems_panic ("Can't create task.\n");
  }

  status = rtems_task_start (debugId, MyThreadIdle, pid);

  return (True);
}

/* -----------------------------------------------------------------------
   TgtPtrace - handle ptrace requests for server.
   ----------------------------------------------------------------------- */

int
TgtPtrace (int req, PID pid, char *addr, int data, void *addr2)
{
  if ((req == RPT_SINGLESTEP || req == RPT_CONT)
      && addr2) {               /* clear then step *//* addr2 is the old value */

    int ret;

    errno = 0;
    TgtBreakRestoreOrig (pid, addr, addr2);
    ret = Ptrace (RPT_SINGLESTEP, pid, addr, data, NULL);   /* step over */
    if (ret) {                  /* error, cannot single-step */
      int pid_idx = FindPidEntry (pid);
      TgtBreakCancelStep (&pid_list[pid_idx]);
    }
    return (ret);               /* failed or done */
  } else
      return (Ptrace (req, pid, addr, data, addr2));  /* normal call */
}

/* -----------------------------------------------------------------
   TgtGetThreadName - get thread name
   --------------------------------------------------------------- */

  int
TgtGetThreadName (PID_LIST * plst,  /* Process entry */
                  unsigned Id,  /* Thread ID */
                  char *ThrName)
{                               /* Thread name */
  int index;
  unsigned name;

  if (Id < _Objects_Information_table[OBJECTS_RTEMS_TASKS]->maximum_id &&
      Id > _Objects_Information_table[OBJECTS_RTEMS_TASKS]->minimum_id) {

    index = Id - _Objects_Information_table[OBJECTS_RTEMS_TASKS]->minimum_id;
    name =
      *(unsigned *) (_Objects_Information_table[OBJECTS_RTEMS_TASKS]->
                     local_table[1 + index]->name);
    ThrName[0] = (char) ((name >> 24) & 0xFF);
    ThrName[1] = (char) ((name >> 16) & 0xFF);
    ThrName[2] = (char) ((name >> 8) & 0xFF);
    ThrName[3] = (char) (name & 0xFF);
    ThrName[4] = 0x0;
    return 0;
  }

  if (Id < _Objects_Information_table[OBJECTS_POSIX_THREADS]->maximum_id &&
      Id > _Objects_Information_table[OBJECTS_POSIX_THREADS]->minimum_id) {

    index =
      Id - _Objects_Information_table[OBJECTS_POSIX_THREADS]->minimum_id;
    name =
      *(unsigned *) (_Objects_Information_table[OBJECTS_POSIX_THREADS]->
                     local_table[1 + index]->name);
    ThrName[0] = (char) ((name >> 24) & 0xFF);
    ThrName[1] = (char) ((name >> 16) & 0xFF);
    ThrName[2] = (char) ((name >> 8) & 0xFF);
    ThrName[3] = (char) (name & 0xFF);
    ThrName[4] = 0x0;
    return 0;
  }

  return -1;
}

/* -----------------------------------------------------------------
   TgtThreadList - return all the threads in the system
   ----------------------------------------------------------------- */

  int
TgtThreadList (PID_LIST * plst, /* Process entry */
               unsigned *threads,   /* Output buffer */
               unsigned size)
{                               /* Output buffer size */
  int curr = 0;
  Objects_Id id;
  unsigned index;

  id = _Objects_Information_table[OBJECTS_RTEMS_TASKS]->minimum_id;

  while (id < _Objects_Information_table[OBJECTS_RTEMS_TASKS]->maximum_id) {
    index = id - _Objects_Information_table[OBJECTS_RTEMS_TASKS]->minimum_id;
    if (_Objects_Information_table[OBJECTS_RTEMS_TASKS]->
        local_table[1 + index] != NULL) {
      threads[curr] = (unsigned) id;
      curr++;
    }
    id++;
  }

  id = _Objects_Information_table[OBJECTS_POSIX_THREADS]->minimum_id;

  while (id < _Objects_Information_table[OBJECTS_POSIX_THREADS]->maximum_id) {
    index =
      id - _Objects_Information_table[OBJECTS_POSIX_THREADS]->minimum_id;
    if (_Objects_Information_table[OBJECTS_POSIX_THREADS]->
        local_table[1 + index] != NULL) {
      threads[curr] = (unsigned) id;
      curr++;
    }
    id++;
  }

  return curr;
}