chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / linuxthreads_db / td_ta_new.c
1 /* Attach to target process.
2    Copyright (C) 1999, 2001, 2002, 2003 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 Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the 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    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <version.h>
25
26 #include "thread_dbP.h"
27
28
29 /* Datatype for the list of known thread agents.  Normally there will
30    be exactly one so we don't spend much though on making it fast.  */
31 struct agent_list *__td_agent_list;
32
33
34 td_err_e
35 td_ta_new (struct ps_prochandle *ps, td_thragent_t **ta)
36 {
37   psaddr_t addr;
38   psaddr_t versaddr;
39   char versbuf[sizeof (VERSION)];
40   struct agent_list *elemp;
41
42   LOG ("td_ta_new");
43
44   /* Get the global event mask.  This is one of the variables which
45      are new in the thread library to enable debugging.  If it is
46      not available we cannot debug.  */
47   if (td_lookup (ps, PTHREAD_THREADS_EVENTS, &addr) != PS_OK)
48     return TD_NOLIBTHREAD;
49
50   /* Check whether the versions match.  */
51   if (td_lookup (ps, LINUXTHREADS_VERSION, &versaddr) != PS_OK)
52     return TD_VERSION;
53   if (ps_pdread (ps, versaddr, versbuf, sizeof (versbuf)) != PS_OK)
54     return TD_ERR;
55
56   versbuf[sizeof (versbuf) - 1] = '\0';
57   if (strcmp (versbuf, VERSION) != 0)
58     /* Not the right version.  */
59     return TD_VERSION;
60
61   /* Fill in the appropriate information.  */
62   *ta = (td_thragent_t *) malloc (sizeof (td_thragent_t));
63   if (*ta == NULL)
64     return TD_MALLOC;
65
66   /* Store the proc handle which we will pass to the callback functions
67      back into the debugger.  */
68   (*ta)->ph = ps;
69
70   /* Remember the address.  */
71   (*ta)->pthread_threads_eventsp = (td_thr_events_t *) addr;
72
73   /* Get the pointer to the variable pointing to the thread descriptor
74      with the last event.  */
75   if (td_lookup (ps, PTHREAD_LAST_EVENT, &(*ta)->pthread_last_event) != PS_OK)
76     {
77     free_return:
78       free (*ta);
79       return TD_ERR;
80     }
81
82   /* Get the pointer to the variable containing the number of active
83      threads.  */
84   if (td_lookup (ps, PTHREAD_HANDLES_NUM, &(*ta)->pthread_handles_num)
85       != PS_OK)
86     goto free_return;
87
88   /* See whether the library contains the necessary symbols.  */
89   if (td_lookup (ps, PTHREAD_HANDLES, &addr) != PS_OK)
90     goto free_return;
91
92   (*ta)->handles = (struct pthread_handle_struct *) addr;
93
94
95   if (td_lookup (ps, PTHREAD_KEYS, &addr) != PS_OK)
96     goto free_return;
97
98   /* Cast to the right type.  */
99   (*ta)->keys = (struct pthread_key_struct *) addr;
100
101   /* Find out about the maximum number of threads.  Old implementations
102      don't provide this information.  In this case we assume that the
103      debug  library is compiled with the same values.  */
104   if (td_lookup (ps, LINUXTHREADS_PTHREAD_THREADS_MAX, &addr) != PS_OK)
105     (*ta)->pthread_threads_max = PTHREAD_THREADS_MAX;
106   else
107     {
108       if (ps_pdread (ps, addr, &(*ta)->pthread_threads_max, sizeof (int))
109           != PS_OK)
110         goto free_return;
111     }
112
113   /* Similar for the maximum number of thread local data keys.  */
114   if (td_lookup (ps, LINUXTHREADS_PTHREAD_KEYS_MAX, &addr) != PS_OK)
115     (*ta)->pthread_keys_max = PTHREAD_KEYS_MAX;
116   else
117     {
118       if (ps_pdread (ps, addr, &(*ta)->pthread_keys_max, sizeof (int))
119           != PS_OK)
120         goto free_return;
121     }
122
123   /* And for the size of the second level arrays for the keys.  */
124   if (td_lookup (ps, LINUXTHREADS_PTHREAD_SIZEOF_DESCR, &addr) != PS_OK)
125     (*ta)->sizeof_descr = sizeof (struct _pthread_descr_struct);
126   else
127     {
128       if (ps_pdread (ps, addr, &(*ta)->sizeof_descr, sizeof (int)) != PS_OK)
129         goto free_return;
130       /* Don't let bogons in the inferior make us mess ourselves.  */
131       if ((*ta)->sizeof_descr > sizeof (struct _pthread_descr_struct))
132         (*ta)->sizeof_descr = sizeof (struct _pthread_descr_struct);
133     }
134
135   /* Now add the new agent descriptor to the list.  */
136   elemp = (struct agent_list *) malloc (sizeof (struct agent_list));
137   if (elemp == NULL)
138     {
139       /* Argh, now that everything else worked...  */
140       free (*ta);
141       return TD_MALLOC;
142     }
143
144   /* We don't care for thread-safety here.  */
145   elemp->ta = *ta;
146   elemp->next = __td_agent_list;
147   __td_agent_list = elemp;
148
149   return TD_OK;
150 }