include/elf.h: sync with GNU C library
[uclibc-ng.git] / libpthread / linuxthreads_db / td_ta_new.c
blob7cf0edc669dde257dd70909d211143cdc8672454
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.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "thread_dbP.h"
27 /* Datatype for the list of known thread agents. Normally there will
28 be exactly one so we don't spend much though on making it fast. */
29 struct agent_list *__td_agent_list;
32 td_err_e
33 td_ta_new (struct ps_prochandle *ps, td_thragent_t **ta)
35 psaddr_t addr;
36 psaddr_t versaddr;
37 char versbuf[sizeof (VERSION)];
38 struct agent_list *elemp;
40 LOG ("td_ta_new");
42 /* Get the global event mask. This is one of the variables which
43 are new in the thread library to enable debugging. If it is
44 not available we cannot debug. */
45 if (td_lookup (ps, PTHREAD_THREADS_EVENTS, &addr) != PS_OK)
46 return TD_NOLIBTHREAD;
48 /* Check whether the versions match. */
49 if (td_lookup (ps, LINUXTHREADS_VERSION, &versaddr) != PS_OK)
50 return TD_VERSION;
51 if (ps_pdread (ps, versaddr, versbuf, sizeof (versbuf)) != PS_OK)
52 return TD_ERR;
54 versbuf[sizeof (versbuf) - 1] = '\0';
55 if (strcmp (versbuf, VERSION) != 0)
56 /* Not the right version. */
57 return TD_VERSION;
59 /* Fill in the appropriate information. */
60 *ta = (td_thragent_t *) malloc (sizeof (td_thragent_t));
61 if (*ta == NULL)
62 return TD_MALLOC;
64 /* Store the proc handle which we will pass to the callback functions
65 back into the debugger. */
66 (*ta)->ph = ps;
68 /* Remember the address. */
69 (*ta)->pthread_threads_eventsp = (td_thr_events_t *) addr;
71 /* Get the pointer to the variable pointing to the thread descriptor
72 with the last event. */
73 if (td_lookup (ps, PTHREAD_LAST_EVENT, &(*ta)->pthread_last_event) != PS_OK)
75 free_return:
76 free (*ta);
77 return TD_ERR;
80 /* Get the pointer to the variable containing the number of active
81 threads. */
82 if (td_lookup (ps, PTHREAD_HANDLES_NUM, &(*ta)->pthread_handles_num)
83 != PS_OK)
84 goto free_return;
86 /* See whether the library contains the necessary symbols. */
87 if (td_lookup (ps, PTHREAD_HANDLES, &addr) != PS_OK)
88 goto free_return;
90 (*ta)->handles = (struct pthread_handle_struct *) addr;
93 if (td_lookup (ps, PTHREAD_KEYS, &addr) != PS_OK)
94 goto free_return;
96 /* Cast to the right type. */
97 (*ta)->keys = (struct pthread_key_struct *) addr;
99 /* Find out about the maximum number of threads. Old implementations
100 don't provide this information. In this case we assume that the
101 debug library is compiled with the same values. */
102 if (td_lookup (ps, LINUXTHREADS_PTHREAD_THREADS_MAX, &addr) != PS_OK)
103 (*ta)->pthread_threads_max = PTHREAD_THREADS_MAX;
104 else
106 if (ps_pdread (ps, addr, &(*ta)->pthread_threads_max, sizeof (int))
107 != PS_OK)
108 goto free_return;
111 /* Similar for the maximum number of thread local data keys. */
112 if (td_lookup (ps, LINUXTHREADS_PTHREAD_KEYS_MAX, &addr) != PS_OK)
113 (*ta)->pthread_keys_max = PTHREAD_KEYS_MAX;
114 else
116 if (ps_pdread (ps, addr, &(*ta)->pthread_keys_max, sizeof (int))
117 != PS_OK)
118 goto free_return;
121 /* And for the size of the second level arrays for the keys. */
122 if (td_lookup (ps, LINUXTHREADS_PTHREAD_SIZEOF_DESCR, &addr) != PS_OK)
123 (*ta)->sizeof_descr = sizeof (struct _pthread_descr_struct);
124 else
126 if (ps_pdread (ps, addr, &(*ta)->sizeof_descr, sizeof (int)) != PS_OK)
127 goto free_return;
128 /* Don't let bogons in the inferior make us mess ourselves. */
129 if ((*ta)->sizeof_descr > sizeof (struct _pthread_descr_struct))
130 (*ta)->sizeof_descr = sizeof (struct _pthread_descr_struct);
133 /* Now add the new agent descriptor to the list. */
134 elemp = (struct agent_list *) malloc (sizeof (struct agent_list));
135 if (elemp == NULL)
137 /* Argh, now that everything else worked... */
138 free (*ta);
139 return TD_MALLOC;
142 /* We don't care for thread-safety here. */
143 elemp->ta = *ta;
144 elemp->next = __td_agent_list;
145 __td_agent_list = elemp;
147 return TD_OK;