summaryrefslogtreecommitdiffstats
path: root/c/src/lib/librdbg/_servtgt.c
blob: 184415a1e2e79778fbec2aaa5aa460be88f3a6ae (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
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
/*
 ============================================================================
 _SERVTGT 
 ============================================================================
*/


#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>

extern void rtems_exception_prologue_50();


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

extern int errno;

rtems_id eventTaskId;
rtems_id serializeSemId;
rtems_id wakeupEventSemId;

CPU_Exception_frame Idle_frame;

cpuExcHandlerType old_currentExcHandler;

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

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


/* -----------------------------------------------------------------
   Maping of hardware exceptions into Unix-like signal numbers.
   It is identical to the one used by the PM and the AM.
   ----------------------------------------------------------------- */

    int
ExcepToSig (int excep)
{
    switch (excep) {
      
    case I386_EXCEPTION_MATH_COPROC_UNAVAIL: 
    case I386_EXCEPTION_I386_COPROC_SEG_ERR: 
    case I386_EXCEPTION_FLOAT_ERROR: 
    case I386_EXCEPTION_BOUND: 
	return SIGFPE;

    case I386_EXCEPTION_DEBUG: 
    case I386_EXCEPTION_BREAKPOINT: 
    case I386_EXCEPTION_ENTER_RDBG: 
	return SIGTRAP;

    case I386_EXCEPTION_OVERFLOW: 
    case I386_EXCEPTION_DIVIDE_BY_ZERO:
    case I386_EXCEPTION_ILLEGAL_INSTR:  
	return SIGILL;

    case I386_EXCEPTION_SEGMENT_NOT_PRESENT:
    case I386_EXCEPTION_STACK_SEGMENT_FAULT:
    case I386_EXCEPTION_GENERAL_PROT_ERR: 
    case I386_EXCEPTION_PAGE_FAULT: 
	return SIGSEGV;

    default:
	break;
    }
    return SIGKILL;
}

/* -----------------------------------------------------------------------
   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->ctx->idtIndex)));
      
  }
}

/* -------------------------------------------------------------------
   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;
  rtems_id debugId;
  interrupt_gate_descriptor	 *currentIdtEntry;
  unsigned			 limit;
  unsigned			 level;
  
  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 the Exception used to debug
   */
  i386_get_info_from_IDTR (&currentIdtEntry, &limit);
  
  _CPU_ISR_Disable(level);
  create_interrupt_gate_descriptor (&currentIdtEntry[50], rtems_exception_prologue_50);
  _CPU_ISR_Enable(level);

  old_currentExcHandler = _currentExcHandler;
  _currentExcHandler = BreakPointExcHdl ;


  /*
   * Create the attach debuger 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\n",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;
}