td_ta_new.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "thread_dbP.h"
  20. /* Datatype for the list of known thread agents. Normally there will
  21. be exactly one so we don't spend much though on making it fast. */
  22. struct agent_list *__td_agent_list;
  23. td_err_e
  24. td_ta_new (struct ps_prochandle *ps, td_thragent_t **ta)
  25. {
  26. psaddr_t addr;
  27. psaddr_t versaddr;
  28. char versbuf[sizeof (VERSION)];
  29. struct agent_list *elemp;
  30. LOG ("td_ta_new");
  31. /* Get the global event mask. This is one of the variables which
  32. are new in the thread library to enable debugging. If it is
  33. not available we cannot debug. */
  34. if (td_lookup (ps, PTHREAD_THREADS_EVENTS, &addr) != PS_OK)
  35. return TD_NOLIBTHREAD;
  36. /* Check whether the versions match. */
  37. if (td_lookup (ps, LINUXTHREADS_VERSION, &versaddr) != PS_OK)
  38. return TD_VERSION;
  39. if (ps_pdread (ps, versaddr, versbuf, sizeof (versbuf)) != PS_OK)
  40. return TD_ERR;
  41. versbuf[sizeof (versbuf) - 1] = '\0';
  42. if (strcmp (versbuf, VERSION) != 0)
  43. /* Not the right version. */
  44. return TD_VERSION;
  45. /* Fill in the appropriate information. */
  46. *ta = (td_thragent_t *) malloc (sizeof (td_thragent_t));
  47. if (*ta == NULL)
  48. return TD_MALLOC;
  49. /* Store the proc handle which we will pass to the callback functions
  50. back into the debugger. */
  51. (*ta)->ph = ps;
  52. /* Remember the address. */
  53. (*ta)->pthread_threads_eventsp = (td_thr_events_t *) addr;
  54. /* Get the pointer to the variable pointing to the thread descriptor
  55. with the last event. */
  56. if (td_lookup (ps, PTHREAD_LAST_EVENT, &(*ta)->pthread_last_event) != PS_OK)
  57. {
  58. free_return:
  59. free (*ta);
  60. return TD_ERR;
  61. }
  62. /* Get the pointer to the variable containing the number of active
  63. threads. */
  64. if (td_lookup (ps, PTHREAD_HANDLES_NUM, &(*ta)->pthread_handles_num)
  65. != PS_OK)
  66. goto free_return;
  67. /* See whether the library contains the necessary symbols. */
  68. if (td_lookup (ps, PTHREAD_HANDLES, &addr) != PS_OK)
  69. goto free_return;
  70. (*ta)->handles = (struct pthread_handle_struct *) addr;
  71. if (td_lookup (ps, PTHREAD_KEYS, &addr) != PS_OK)
  72. goto free_return;
  73. /* Cast to the right type. */
  74. (*ta)->keys = (struct pthread_key_struct *) addr;
  75. /* Find out about the maximum number of threads. Old implementations
  76. don't provide this information. In this case we assume that the
  77. debug library is compiled with the same values. */
  78. if (td_lookup (ps, LINUXTHREADS_PTHREAD_THREADS_MAX, &addr) != PS_OK)
  79. (*ta)->pthread_threads_max = PTHREAD_THREADS_MAX;
  80. else
  81. {
  82. if (ps_pdread (ps, addr, &(*ta)->pthread_threads_max, sizeof (int))
  83. != PS_OK)
  84. goto free_return;
  85. }
  86. /* Similar for the maximum number of thread local data keys. */
  87. if (td_lookup (ps, LINUXTHREADS_PTHREAD_KEYS_MAX, &addr) != PS_OK)
  88. (*ta)->pthread_keys_max = PTHREAD_KEYS_MAX;
  89. else
  90. {
  91. if (ps_pdread (ps, addr, &(*ta)->pthread_keys_max, sizeof (int))
  92. != PS_OK)
  93. goto free_return;
  94. }
  95. /* And for the size of the second level arrays for the keys. */
  96. if (td_lookup (ps, LINUXTHREADS_PTHREAD_SIZEOF_DESCR, &addr) != 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. /* Don't let bogons in the inferior make us mess ourselves. */
  103. if ((*ta)->sizeof_descr > sizeof (struct _pthread_descr_struct))
  104. (*ta)->sizeof_descr = sizeof (struct _pthread_descr_struct);
  105. }
  106. /* Now add the new agent descriptor to the list. */
  107. elemp = (struct agent_list *) malloc (sizeof (struct agent_list));
  108. if (elemp == NULL)
  109. {
  110. /* Argh, now that everything else worked... */
  111. free (*ta);
  112. return TD_MALLOC;
  113. }
  114. /* We don't care for thread-safety here. */
  115. elemp->ta = *ta;
  116. elemp->next = __td_agent_list;
  117. __td_agent_list = elemp;
  118. return TD_OK;
  119. }