File: dec-thread.c

package info (click to toggle)
gnat-gdb 5.3.gnat.0.0.20030225-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 75,144 kB
  • ctags: 101,348
  • sloc: ansic: 873,511; exp: 46,950; sh: 16,123; makefile: 11,757; yacc: 6,092; asm: 5,027; cpp: 4,044; perl: 2,624; lex: 877; sed: 550; lisp: 394; awk: 170; pascal: 57; java: 7; fortran: 5
file content (527 lines) | stat: -rw-r--r-- 11,768 bytes parent folder | download
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
/* Low level interface for debugging DEC threads for GDB, the GNU debugger.
   Copyright 1996 Free Software Foundation, Inc.

This file is part of GDB.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include "defs.h"

#include <pthread_debug.h>

#include "gdbthread.h"

#include "target.h"
#include "inferior.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/procfs.h>
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include "gdbcmd.h"

extern unsigned long current_thread;

static int dec_thread_active = 0;

extern struct target_ops procfs_ops; /* target vector for procfs.c */

pthreadDebugContext_t debug_context;

extern pthreadDebugCallbacks_t debug_callbacks;

extern void dec_thread_init ();

/* Two debug macros than can be used to check the memory state before
   and after calls to the debug library */
#define PROTECT_MEM
#define UNPROTECT_MEM

 /*
	Return the pthread_debug error string associated with errcode.
	If errcode is unknown, then return a message.
 */

struct string_map {
  int num;
  char *str;
};

static char *
ptd_err_string (errcode)
     int errcode;
{
  static struct string_map
    ptd_err_table[] = {
      {ESUCCESS,	"Successful completion"},
      {ENOENT,		"End of \"next thread\" sequence (no more)"},
      {ENOTSUP,		"Function not supported (usually callback error)"},
      {EINVAL,		"Invalid parameter"},
      {ESRCH,		"No such thread"},
      {ENOMEM,		"Insufficient memory"},
      {EPERM,		"Unable to perform operation (priv, state)"}
    };
  static int ptd_err_size = sizeof ptd_err_table / sizeof (struct string_map);
  int i;
  static char buf[50];

  for (i = 0; i < ptd_err_size; i++)
    if (ptd_err_table[i].num == errcode)
      return ptd_err_table[i].str;

  sprintf (buf, "Unknown pthread_debug error code: %d", errcode);

  return buf;
}

/*

LOCAL FUNCTION

	ptd_state_string - Convert a pthread_debug state code to a string

SYNOPSIS

	char * ptd_state_string (statecode)

DESCRIPTION

	Return the pthread_debug state string associated with statecode.  If
	statecode is unknown, then return a message.

 */

static char *
ptd_state_string (statecode)
     int statecode;
{
  static struct string_map
    ptd_state_table[] = {
      {PTHREAD_DEBUG_STATE_UNKNOWN,	"<bad value>"},
      {PTHREAD_DEBUG_STATE_RUNNING,	"running"},
      {PTHREAD_DEBUG_STATE_READY,	"ready"},
      {PTHREAD_DEBUG_STATE_BLOCKED,	"blocked"},
      {PTHREAD_DEBUG_STATE_NEW,		"new"},
      {PTHREAD_DEBUG_STATE_SORTING,	"sorting"},
      {PTHREAD_DEBUG_STATE_TERMINATED,	"terminated"},
      {PTHREAD_DEBUG_STATE_ZOMBIE,	"zombie"}
    };
  const int ptd_state_table_size = sizeof ptd_state_table / sizeof (struct string_map);
  int i;
  static char buf[50];

  for (i = 0; i < ptd_state_table_size; i++)
    if (ptd_state_table[i].num == statecode)
      return ptd_state_table[i].str;

  sprintf (buf, "Unknown pthread_debug state code: %d", statecode);

  return buf;
}

/* read the registers of the current thread (designated by the global variable
   current_thread and store the general registers in gregset and the floating
   point registers in fpgregset iff fpgregset is non null.
   Return 0 on success, -1 on error.
*/

int
dec_thread_get_registers (gregset, fpregset)
  gregset_t *gregset;
  fpregset_t *fpregset;
{
  pthreadDebugId_t thread;
  pthreadDebugThreadInfo_t info;
  int res;

  dec_thread_init ();

  /* Get the integer regs */

  PROTECT_MEM;

  res = pthreadDebugThdSeqInit (debug_context, &thread);

  while (res == ESUCCESS)
    {
      res = pthreadDebugGetThreadState (debug_context, thread, &info);

      if (info.teb == (pthread_t) current_thread)
	{
	  break;
	}
      res = pthreadDebugThdSeqNext (debug_context, &thread);
    }
  
  if (res != ESUCCESS) {
    pthreadDebugThdSeqDestroy (debug_context);
    UNPROTECT_MEM;
    return -1;
  }

  res = pthreadDebugGetReg (debug_context, thread, gregset);
  if (res != ESUCCESS) {
    error ("dec_thread_fetch_registers: pthreadDebugGetReg %s",
	   ptd_err_string (res));
    pthreadDebugThdSeqDestroy (debug_context);
    UNPROTECT_MEM;
    return -1;
  }

  /* And, now the fp regs */

  if (fpregset != NULL) {
    res = pthreadDebugGetFpreg (debug_context, thread, fpregset);

    if (res != ESUCCESS) {
      error ("dec_thread_fetch_registers: pthreadDebugGetReg %s",
	     ptd_err_string (res));
      pthreadDebugThdSeqDestroy (debug_context);
      UNPROTECT_MEM;
      return -1;
    }
  }

  pthreadDebugThdSeqDestroy (debug_context);
  UNPROTECT_MEM;
  return 0;
}

int
dec_thread_fetch_registers ()
{
  gregset_t gregset;
  fpregset_t fpregset;

  dec_thread_init ();

  if (dec_thread_get_registers (&gregset, &fpregset) == 0) {

    supply_gregset (&gregset);
    supply_fpregset (&fpregset);

    return 0;
  }

  return -1;
}

unsigned long
dec_thread_get_current_thread ()
{
  pthreadDebugId_t thread;
  pthreadDebugThreadInfo_t info;
  int res;

  dec_thread_init ();

  PROTECT_MEM;

  res = pthreadDebugThdSeqInit (debug_context, &thread);

  while (res == ESUCCESS)
    {
      res = pthreadDebugGetThreadState (debug_context, thread, &info);

      if (info.state == PTHREAD_DEBUG_STATE_RUNNING)
        {
          break;
        }
      res = pthreadDebugThdSeqNext (debug_context, &thread);
    }
 
  pthreadDebugThdSeqDestroy (debug_context);
  UNPROTECT_MEM;

  if (res != ESUCCESS) return 0;
  return (unsigned long) info.teb;
}

/* This routine must be called once before using the other functions of this
    file. */

void
dec_thread_init ()
{
  int val, i;
  void *symtable;
  struct minimal_symbol *msym;
  void* caller_context;
  char buf [64];
  struct value malloc_val;
  struct value *value;

  if (!dec_thread_active)
    {
#ifdef DEBUG
      printf_unfiltered ("in dec_thread_init\n");
#endif

      msym = lookup_minimal_symbol ("__pthread_dbg_symtable", NULL, NULL);

      if (msym != NULL)
        {
          symtable = (void *) SYMBOL_VALUE_ADDRESS (msym);
#ifdef DEBUG
	  printf_unfiltered ("dbg_symtable = %lx\n", symtable);
#endif
	}
      else
	{
          error ("dec_thread_init: unable to find __pthread_dbg_symtable\n");
	  return;
	}

      /* Initialize the thread debugging library.  This needs to be done after
         the process has started because it needs to call some functions
	 (e.g malloc) in the inferior */

      PROTECT_MEM;
      val = pthreadDebugContextInit (&caller_context, &debug_callbacks, 
				     symtable, &debug_context);
      UNPROTECT_MEM;

      if (val != ESUCCESS)
        error ("dec_thread_init: pthreadDebugContextInit: %s",
               ptd_err_string (val));

      dec_thread_active = 1;
    }
}

/* These routines implement the lower half of the pthread_debug interface.
   Ie: the *_clbk routines.  */

/* The next 4 routines are called by pthread_debug to tell us to stop and
   continue a particular process or thread.  Since GDB ensures that these are
   all stopped by the time we call anything in pthread_debug, these routines
   need to do nothing.  */

static int suspend_clbk (void *caller_context)
{
  return ESUCCESS;
}

static int resume_clbk (void *caller_context)
{
  return ESUCCESS;
}

static int hold_clbk (void *caller_context, pthreadDebugKId_t thread_id)
{
  return ESUCCESS;
}

static int unhold_clbk (void *caller_context, pthreadDebugKId_t thread_id)
{
  return ESUCCESS;
}

/* Common routine for reading and writing memory.  */

static int rw_common (
  int dowrite,
  void *caller_context,
  char *address,
  char *buffer,
  unsigned long size)
{
#ifdef DEBUG
  printf_unfiltered ("rw_common (%d, %p, %p, %p, %ld)\n",
    dowrite, caller_context, address, buffer, size);
#endif

  if (address == NULL)
    return EINVAL;

  while (size > 0)
    {
      int cc;

      cc = procfs_ops.to_xfer_memory
             ((CORE_ADDR) address, buffer, size, dowrite, NULL, &procfs_ops);

      if (cc < 0)
	{
	  if (dowrite == 0)
	    print_sys_errmsg ("rw_common (): read", errno);
	  else
	    print_sys_errmsg ("rw_common (): write", errno);

	  return EINVAL;
	}
      size -= cc;
      buffer += cc;
      address += cc;
    }
  return ESUCCESS;
}

static int read_clbk (
    void *caller_context,
    void *address,
    void *buffer,
    unsigned long size)
{
  return rw_common (0, caller_context, address, buffer, size);
}

static int write_clbk (
    void *caller_context,
    void *address,
    void *buffer,
    unsigned long size)
{
  return rw_common (1, caller_context, address, buffer, size);
}

/* Get integer regs */

static int get_reg_clbk(void *caller_context,
  gregset_t *gregset,
  pthreadDebugKId_t thread_id)
{
#ifdef DEBUG
  printf_unfiltered ("in get_reg_clbk...\n");
#endif

  procfs_ops.to_fetch_registers (-1);
  fill_gregset (gregset, -1);

  return ESUCCESS;
}

/* Set integer regs */

static int set_reg_clbk(void *caller_context,
  gregset_t *gregset,
  pthreadDebugKId_t thread_id)
{
#ifdef DEBUG
  printf_unfiltered ("in set_reg_clbk...\n");
#endif

  supply_gregset (gregset);
  procfs_ops.to_store_registers (-1);

  return ESUCCESS;
}

/*
 * Routine to write a line of standard output
 */

static int output_clbk (void *caller_context, char *line)
{
  fprintf_filtered (gdb_stdout, "%s\n", line);
  return ESUCCESS;
}

/*
 * Routine to write a line of error
 */

static int error_clbk (void *caller_context, char *line)
{
  fprintf_filtered (gdb_stderr, "%s\n", line);
  return ESUCCESS;
}

/* Get floating-point regs.  */

static int get_fpreg_clbk (void *caller_context,
  fpregset_t *fpregset,
  pthreadDebugKId_t thread_id)
{
#ifdef DEBUG
  printf_unfiltered ("in get_fpreg_clbk...\n");
#endif

  procfs_ops.to_fetch_registers (-1);
  fill_fpregset (fpregset, -1);

  return ESUCCESS;
}

/* Set floating-point regs.  */

static int set_fpreg_clbk (void *caller_context,
  fpregset_t *fpregset,
  pthreadDebugKId_t thread_id)
{
#ifdef DEBUG
  printf_unfiltered ("in set_fpreg_clbk...\n");
#endif

  supply_fpregset (fpregset);
  procfs_ops.to_store_registers (-1);

  return ESUCCESS;
}

/*
 * Routine to allocate memory.
 */
static void *malloc_clbk (void *caller_context, size_t size)
{
  return malloc (size);
}

/*
 * Routine to deallocate memory
 */
static void free_clbk (void *caller_context, void *address)
{
  free (address);
}

static int kthdinfo_clbk (
  pthreadDebugClient_t        caller_context,
  pthreadDebugKId_t           thread_id,
  pthreadDebugKThreadInfo_p   thread_info)
{
  return ENOTSUP;
}

static int speckthd_clbk (
  pthreadDebugClient_t        caller_context,
  pthreadDebugSpecialType_t   type,
  pthreadDebugKId_t           *thread_id)
{
  return ENOTSUP;
}

pthreadDebugCallbacks_t debug_callbacks = {
  PTHREAD_DEBUG_VERSION,
  (pthreadDebugGetMemRtn_t) read_clbk,
  (pthreadDebugSetMemRtn_t) write_clbk,
  suspend_clbk,
  resume_clbk,
  kthdinfo_clbk,
  hold_clbk,
  unhold_clbk,
  (pthreadDebugGetFregRtn_t) get_fpreg_clbk,
  (pthreadDebugSetFregRtn_t) set_fpreg_clbk,
  (pthreadDebugGetRegRtn_t) get_reg_clbk,
  (pthreadDebugSetRegRtn_t) set_reg_clbk,
  (pthreadDebugOutputRtn_t) output_clbk,
  (pthreadDebugOutputRtn_t) error_clbk,
  malloc_clbk,
  free_clbk,
  speckthd_clbk
};