Update.
[glibc.git] / linuxthreads_db / td_ta_new.c
blob7505f53e63c7dc367e7722c3f0aa08b1a83d1e16
1 /* Attach to target process.
2 Copyright (C) 1999, 2001 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <stddef.h>
22 #include <stdlib.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 struct agent_list *elemp;
38 LOG ("td_ta_new");
40 /* Get the global event mask. This is one of the variables which
41 are new in the thread library to enable debugging. If it is
42 not available we cannot debug. */
43 if (td_lookup (ps, PTHREAD_THREADS_EVENTS, &addr) != PS_OK)
44 return TD_NOLIBTHREAD;
46 /* Fill in the appropriate information. */
47 *ta = (td_thragent_t *) malloc (sizeof (td_thragent_t));
48 if (*ta == NULL)
49 return TD_MALLOC;
51 /* Store the proc handle which we will pass to the callback functions
52 back into the debugger. */
53 (*ta)->ph = ps;
55 /* Remember the address. */
56 (*ta)->pthread_threads_eventsp = (td_thr_events_t *) addr;
58 /* Get the pointer to the variable pointing to the thread descriptor
59 with the last event. */
60 if (td_lookup (ps, PTHREAD_LAST_EVENT, &(*ta)->pthread_last_event) != PS_OK)
62 free_return:
63 free (*ta);
64 return TD_ERR;
67 /* Get the pointer to the variable containing the number of active
68 threads. */
69 if (td_lookup (ps, PTHREAD_HANDLES_NUM, &(*ta)->pthread_handles_num)
70 != PS_OK)
71 goto free_return;
73 /* See whether the library contains the necessary symbols. */
74 if (td_lookup (ps, PTHREAD_HANDLES, &addr) != PS_OK)
75 goto free_return;
77 (*ta)->handles = (struct pthread_handle_struct *) addr;
80 if (td_lookup (ps, PTHREAD_KEYS, &addr) != PS_OK)
81 goto free_return;
83 /* Cast to the right type. */
84 (*ta)->keys = (struct pthread_key_struct *) addr;
86 /* Find out about the maximum number of threads. Old implementations
87 don't provide this information. In this case we assume that the
88 debug library is compiled with the same values. */
89 if (td_lookup (ps, LINUXTHREADS_PTHREAD_THREADS_MAX, &addr) != PS_OK)
90 (*ta)->pthread_threads_max = PTHREAD_THREADS_MAX;
91 else
93 if (ps_pdread (ps, addr, &(*ta)->pthread_threads_max, sizeof (int))
94 != PS_OK)
95 goto free_return;
98 /* Similar for the maximum number of thread local data keys. */
99 if (td_lookup (ps, LINUXTHREADS_PTHREAD_KEYS_MAX, &addr) != PS_OK)
100 (*ta)->pthread_keys_max = PTHREAD_KEYS_MAX;
101 else
103 if (ps_pdread (ps, addr, &(*ta)->pthread_keys_max, sizeof (int))
104 != PS_OK)
105 goto free_return;
108 /* And for the size of the second level arrays for the keys. */
109 if (td_lookup (ps, LINUXTHREADS_PTHREAD_SIZEOF_DESCR, &addr) != PS_OK)
110 (*ta)->sizeof_descr = sizeof (struct _pthread_descr_struct);
111 else
113 if (ps_pdread (ps, addr, &(*ta)->sizeof_descr, sizeof (int)) != PS_OK)
114 goto free_return;
117 /* Now add the new agent descriptor to the list. */
118 elemp = (struct agent_list *) malloc (sizeof (struct agent_list));
119 if (elemp == NULL)
121 /* Argh, now that everything else worked... */
122 free (*ta);
123 return TD_MALLOC;
126 /* We don't care for thread-safety here. */
127 elemp->ta = *ta;
128 elemp->next = __td_agent_list;
129 __td_agent_list = elemp;
131 return TD_OK;