td_ta_new.c 4.8 KB

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