OSDN Git Service

Merge in the pthread library. This is the linuxthreads library taken from
[uclinux-h8/uClibc.git] / libpthread / linuxthreads_db / td_ta_new.c
1 /* Attach to target process.
2    Copyright (C) 1999 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <stddef.h>
22 #include <stdlib.h>
23 //#include <gnu/lib-names.h>
24
25 #include "thread_dbP.h"
26
27
28 /* Datatype for the list of known thread agents.  Normally there will
29    be exactly one so we don't spend much though on making it fast.  */
30 struct agent_list *__td_agent_list;
31
32
33 td_err_e
34 td_ta_new (struct ps_prochandle *ps, td_thragent_t **ta)
35 {
36   psaddr_t addr;
37   struct agent_list *elemp;
38
39   LOG (__FUNCTION__);
40
41   /* Get the global event mask.  This is one of the variables which
42      are new in the thread library to enable debugging.  If it is
43      not available we cannot debug.  */
44   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO,
45                          "__pthread_threads_events", &addr) != PS_OK)
46     return TD_NOLIBTHREAD;
47
48   /* Fill in the appropriate information.  */
49   *ta = (td_thragent_t *) malloc (sizeof (td_thragent_t));
50   if (*ta == NULL)
51     return TD_MALLOC;
52
53   /* Store the proc handle which we will pass to the callback functions
54      back into the debugger.  */
55   (*ta)->ph = ps;
56
57   /* Remember the address.  */
58   (*ta)->pthread_threads_eventsp = (td_thr_events_t *) addr;
59
60   /* Get the pointer to the variable pointing to the thread descriptor
61      with the last event.  */
62   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO,
63                          "__pthread_last_event",
64                          &(*ta)->pthread_last_event) != PS_OK)
65     {
66     free_return:
67       free (*ta);
68       return TD_ERR;
69     }
70
71   /* Get the pointer to the variable containing the number of active
72      threads.  */
73   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO,
74                          "__pthread_handles_num",
75                          &(*ta)->pthread_handles_num) != PS_OK)
76     goto free_return;
77
78   /* See whether the library contains the necessary symbols.  */
79   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO, "__pthread_handles",
80                          &addr) != PS_OK)
81     goto free_return;
82
83   (*ta)->handles = (struct pthread_handle_struct *) addr;
84
85
86   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO, "pthread_keys",
87                          &addr) != PS_OK)
88     goto free_return;
89
90   /* Cast to the right type.  */
91   (*ta)->keys = (struct pthread_key_struct *) addr;
92
93   /* Find out about the maximum number of threads.  Old implementations
94      don't provide this information.  In this case we assume that the
95      debug  library is compiled with the same values.  */
96   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO,
97                          "__linuxthreads_pthread_threads_max", &addr) != PS_OK)
98     (*ta)->pthread_threads_max = PTHREAD_THREADS_MAX;
99   else
100     {
101       if (ps_pdread (ps, addr, &(*ta)->pthread_threads_max, sizeof (int))
102           != PS_OK)
103         goto free_return;
104     }
105
106   /* Similar for the maximum number of thread local data keys.  */
107   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO,
108                          "__linuxthreads_pthread_keys_max", &addr) != PS_OK)
109     (*ta)->pthread_keys_max = PTHREAD_KEYS_MAX;
110   else
111     {
112       if (ps_pdread (ps, addr, &(*ta)->pthread_keys_max, sizeof (int))
113           != PS_OK)
114         goto free_return;
115     }
116
117   /* And for the size of the second level arrays for the keys.  */
118   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO,
119                          "__linuxthreads_pthread_sizeof_descr", &addr)
120       != PS_OK)
121     (*ta)->sizeof_descr = sizeof (struct _pthread_descr_struct);
122   else
123     {
124       if (ps_pdread (ps, addr, &(*ta)->sizeof_descr, sizeof (int)) != PS_OK)
125         goto free_return;
126     }
127
128   /* Similar for the maximum number of thread local data keys.  */
129   if (ps_pglobal_lookup (ps, LIBPTHREAD_SO,
130                          "__linuxthreads_pthread_keys_max", &addr) != PS_OK)
131     (*ta)->pthread_keys_max = PTHREAD_KEYS_MAX;
132   else
133     {
134       if (ps_pdread (ps, addr, &(*ta)->pthread_keys_max, sizeof (int))
135           != PS_OK)
136         goto free_return;
137     }
138
139   /* Now add the new agent descriptor to the list.  */
140   elemp = (struct agent_list *) malloc (sizeof (struct agent_list));
141   if (elemp == NULL)
142     {
143       /* Argh, now that everything else worked...  */
144       free (*ta);
145       return TD_MALLOC;
146     }
147
148   /* We don't care for thread-safety here.  */
149   elemp->ta = *ta;
150   elemp->next = __td_agent_list;
151   __td_agent_list = elemp;
152
153   return TD_OK;
154 }