td_ta_new.c 4.8 KB

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