thread_db.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* thread_db.h -- interface to libthread_db.so library for debugging -lpthread
  2. Copyright (C) 1999,2001,2002,2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _THREAD_DB_H
  16. #define _THREAD_DB_H 1
  17. /* This is the debugger interface for the LinuxThreads library. It is
  18. modelled closely after the interface with same names in Solaris with
  19. the goal to share the same code in the debugger. */
  20. #include <pthread.h>
  21. #include <stdint.h>
  22. #include <sys/types.h>
  23. #include <sys/procfs.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* Error codes of the library. */
  28. typedef enum
  29. {
  30. TD_OK, /* No error. */
  31. TD_ERR, /* No further specified error. */
  32. TD_NOTHR, /* No matching thread found. */
  33. TD_NOSV, /* No matching synchronization handle found. */
  34. TD_NOLWP, /* No matching light-weighted process found. */
  35. TD_BADPH, /* Invalid process handle. */
  36. TD_BADTH, /* Invalid thread handle. */
  37. TD_BADSH, /* Invalid synchronization handle. */
  38. TD_BADTA, /* Invalid thread agent. */
  39. TD_BADKEY, /* Invalid key. */
  40. TD_NOMSG, /* No event available. */
  41. TD_NOFPREGS, /* No floating-point register content available. */
  42. TD_NOLIBTHREAD, /* Application not linked with thread library. */
  43. TD_NOEVENT, /* Requested event is not supported. */
  44. TD_NOCAPAB, /* Capability not available. */
  45. TD_DBERR, /* Internal debug library error. */
  46. TD_NOAPLIC, /* Operation is not applicable. */
  47. TD_NOTSD, /* No thread-specific data available. */
  48. TD_MALLOC, /* Out of memory. */
  49. TD_PARTIALREG, /* Not entire register set was read or written. */
  50. TD_NOXREGS, /* X register set not available for given thread. */
  51. TD_TLSDEFER, /* Thread has not yet allocated TLS for given module. */
  52. TD_NOTALLOC = TD_TLSDEFER,
  53. TD_VERSION, /* Version if libpthread and libthread_db do not match. */
  54. TD_NOTLS /* There is TLS segment in the given module. */
  55. } td_err_e;
  56. /* Possible thread states. TD_THR_ANY_STATE is a pseudo-state used to
  57. select threads regardless of state in td_ta_thr_iter(). */
  58. typedef enum
  59. {
  60. TD_THR_ANY_STATE,
  61. TD_THR_UNKNOWN,
  62. TD_THR_STOPPED,
  63. TD_THR_RUN,
  64. TD_THR_ACTIVE,
  65. TD_THR_ZOMBIE,
  66. TD_THR_SLEEP,
  67. TD_THR_STOPPED_ASLEEP
  68. } td_thr_state_e;
  69. /* Thread type: user or system. TD_THR_ANY_TYPE is a pseudo-type used
  70. to select threads regardless of type in td_ta_thr_iter(). */
  71. typedef enum
  72. {
  73. TD_THR_ANY_TYPE,
  74. TD_THR_USER,
  75. TD_THR_SYSTEM
  76. } td_thr_type_e;
  77. /* Types of the debugging library. */
  78. /* Handle for a process. This type is opaque. */
  79. typedef struct td_thragent td_thragent_t;
  80. /* The actual thread handle type. This is also opaque. */
  81. typedef struct td_thrhandle
  82. {
  83. td_thragent_t *th_ta_p;
  84. psaddr_t th_unique;
  85. } td_thrhandle_t;
  86. /* Forward declaration of a type defined by and for the dynamic linker. */
  87. struct link_map;
  88. /* Flags for `td_ta_thr_iter'. */
  89. #define TD_THR_ANY_USER_FLAGS 0xffffffff
  90. #define TD_THR_LOWEST_PRIORITY -20
  91. #define TD_SIGNO_MASK NULL
  92. #define TD_EVENTSIZE 2
  93. #define BT_UISHIFT 5 /* log base 2 of BT_NBIPUI, to extract word index */
  94. #define BT_NBIPUI (1 << BT_UISHIFT) /* n bits per uint */
  95. #define BT_UIMASK (BT_NBIPUI - 1) /* to extract bit index */
  96. /* Bitmask of enabled events. */
  97. typedef struct td_thr_events
  98. {
  99. uint32_t event_bits[TD_EVENTSIZE];
  100. } td_thr_events_t;
  101. /* Event set manipulation macros. */
  102. #define __td_eventmask(n) \
  103. (UINT32_C (1) << (((n) - 1) & BT_UIMASK))
  104. #define __td_eventword(n) \
  105. ((UINT32_C ((n) - 1)) >> BT_UISHIFT)
  106. #define td_event_emptyset(setp) \
  107. do { \
  108. int __i; \
  109. for (__i = TD_EVENTSIZE; __i > 0; --__i) \
  110. (setp)->event_bits[__i - 1] = 0; \
  111. } while (0)
  112. #define td_event_fillset(setp) \
  113. do { \
  114. int __i; \
  115. for (__i = TD_EVENTSIZE; __i > 0; --__i) \
  116. (setp)->event_bits[__i - 1] = UINT32_C (0xffffffff); \
  117. } while (0)
  118. #define td_event_addset(setp, n) \
  119. (((setp)->event_bits[__td_eventword (n)]) |= __td_eventmask (n))
  120. #define td_event_delset(setp, n) \
  121. (((setp)->event_bits[__td_eventword (n)]) &= ~__td_eventmask (n))
  122. #define td_eventismember(setp, n) \
  123. (__td_eventmask (n) & ((setp)->event_bits[__td_eventword (n)]))
  124. #if TD_EVENTSIZE == 2
  125. # define td_eventisempty(setp) \
  126. (!((setp)->event_bits[0]) && !((setp)->event_bits[1]))
  127. #else
  128. # error "td_eventisempty must be changed to match TD_EVENTSIZE"
  129. #endif
  130. /* Events reportable by the thread implementation. */
  131. typedef enum
  132. {
  133. TD_ALL_EVENTS, /* Pseudo-event number. */
  134. TD_EVENT_NONE = TD_ALL_EVENTS, /* Depends on context. */
  135. TD_READY, /* Is executable now. */
  136. TD_SLEEP, /* Blocked in a synchronization obj. */
  137. TD_SWITCHTO, /* Now assigned to a process. */
  138. TD_SWITCHFROM, /* Not anymore assigned to a process. */
  139. TD_LOCK_TRY, /* Trying to get an unavailable lock. */
  140. TD_CATCHSIG, /* Signal posted to the thread. */
  141. TD_IDLE, /* Process getting idle. */
  142. TD_CREATE, /* New thread created. */
  143. TD_DEATH, /* Thread terminated. */
  144. TD_PREEMPT, /* Preempted. */
  145. TD_PRI_INHERIT, /* Inherited elevated priority. */
  146. TD_REAP, /* Reaped. */
  147. TD_CONCURRENCY, /* Number of processes changing. */
  148. TD_TIMEOUT, /* Conditional variable wait timed out. */
  149. TD_MIN_EVENT_NUM = TD_READY,
  150. TD_MAX_EVENT_NUM = TD_TIMEOUT,
  151. TD_EVENTS_ENABLE = 31 /* Event reporting enabled. */
  152. } td_event_e;
  153. /* Values representing the different ways events are reported. */
  154. typedef enum
  155. {
  156. NOTIFY_BPT, /* User must insert breakpoint at u.bptaddr. */
  157. NOTIFY_AUTOBPT, /* Breakpoint at u.bptaddr is automatically
  158. inserted. */
  159. NOTIFY_SYSCALL /* System call u.syscallno will be invoked. */
  160. } td_notify_e;
  161. /* Description how event type is reported. */
  162. typedef struct td_notify
  163. {
  164. td_notify_e type; /* Way the event is reported. */
  165. union
  166. {
  167. psaddr_t bptaddr; /* Address of breakpoint. */
  168. int syscallno; /* Number of system call used. */
  169. } u;
  170. } td_notify_t;
  171. /* Structure used to report event. */
  172. typedef struct td_event_msg
  173. {
  174. td_event_e event; /* Event type being reported. */
  175. const td_thrhandle_t *th_p; /* Thread reporting the event. */
  176. union
  177. {
  178. # if 0
  179. td_synchandle_t *sh; /* Handle of synchronization object. */
  180. #endif
  181. uintptr_t data; /* Event specific data. */
  182. } msg;
  183. } td_event_msg_t;
  184. /* Structure containing event data available in each thread structure. */
  185. typedef struct
  186. {
  187. td_thr_events_t eventmask; /* Mask of enabled events. */
  188. td_event_e eventnum; /* Number of last event. */
  189. void *eventdata; /* Data associated with event. */
  190. } td_eventbuf_t;
  191. /* Gathered statistics about the process. */
  192. typedef struct td_ta_stats
  193. {
  194. int nthreads; /* Total number of threads in use. */
  195. int r_concurrency; /* Concurrency level requested by user. */
  196. int nrunnable_num; /* Average runnable threads, numerator. */
  197. int nrunnable_den; /* Average runnable threads, denominator. */
  198. int a_concurrency_num; /* Achieved concurrency level, numerator. */
  199. int a_concurrency_den; /* Achieved concurrency level, denominator. */
  200. int nlwps_num; /* Average number of processes in use,
  201. numerator. */
  202. int nlwps_den; /* Average number of processes in use,
  203. denominator. */
  204. int nidle_num; /* Average number of idling processes,
  205. numerator. */
  206. int nidle_den; /* Average number of idling processes,
  207. denominator. */
  208. } td_ta_stats_t;
  209. /* Since Sun's library is based on Solaris threads we have to define a few
  210. types to map them to POSIX threads. */
  211. typedef pthread_t thread_t;
  212. typedef pthread_key_t thread_key_t;
  213. /* Callback for iteration over threads. */
  214. typedef int td_thr_iter_f (const td_thrhandle_t *, void *);
  215. /* Callback for iteration over thread local data. */
  216. typedef int td_key_iter_f (thread_key_t, void (*) (void *), void *);
  217. /* Forward declaration. This has to be defined by the user. */
  218. struct ps_prochandle;
  219. /* Information about the thread. */
  220. typedef struct td_thrinfo
  221. {
  222. td_thragent_t *ti_ta_p; /* Process handle. */
  223. unsigned int ti_user_flags; /* Unused. */
  224. thread_t ti_tid; /* Thread ID returned by
  225. pthread_create(). */
  226. char *ti_tls; /* Pointer to thread-local data. */
  227. psaddr_t ti_startfunc; /* Start function passed to
  228. pthread_create(). */
  229. psaddr_t ti_stkbase; /* Base of thread's stack. */
  230. long int ti_stksize; /* Size of thread's stack. */
  231. psaddr_t ti_ro_area; /* Unused. */
  232. int ti_ro_size; /* Unused. */
  233. td_thr_state_e ti_state; /* Thread state. */
  234. unsigned char ti_db_suspended; /* Nonzero if suspended by debugger. */
  235. td_thr_type_e ti_type; /* Type of the thread (system vs
  236. user thread). */
  237. intptr_t ti_pc; /* Unused. */
  238. intptr_t ti_sp; /* Unused. */
  239. short int ti_flags; /* Unused. */
  240. int ti_pri; /* Thread priority. */
  241. lwpid_t ti_lid; /* Unused. */
  242. sigset_t ti_sigmask; /* Signal mask. */
  243. unsigned char ti_traceme; /* Nonzero if event reporting
  244. enabled. */
  245. unsigned char ti_preemptflag; /* Unused. */
  246. unsigned char ti_pirecflag; /* Unused. */
  247. sigset_t ti_pending; /* Set of pending signals. */
  248. td_thr_events_t ti_events; /* Set of enabled events. */
  249. } td_thrinfo_t;
  250. /* Prototypes for exported library functions. */
  251. /* Initialize the thread debug support library. */
  252. extern td_err_e td_init (void);
  253. /* Historical relict. Should not be used anymore. */
  254. extern td_err_e td_log (void);
  255. /* Return list of symbols the library can request. */
  256. extern const char **td_symbol_list (void);
  257. /* Generate new thread debug library handle for process PS. */
  258. extern td_err_e td_ta_new (struct ps_prochandle *__ps, td_thragent_t **__ta);
  259. /* Free resources allocated for TA. */
  260. extern td_err_e td_ta_delete (td_thragent_t *__ta);
  261. /* Get number of currently running threads in process associated with TA. */
  262. extern td_err_e td_ta_get_nthreads (const td_thragent_t *__ta, int *__np);
  263. /* Return process handle passed in `td_ta_new' for process associated with
  264. TA. */
  265. extern td_err_e td_ta_get_ph (const td_thragent_t *__ta,
  266. struct ps_prochandle **__ph);
  267. /* Map thread library handle PT to thread debug library handle for process
  268. associated with TA and store result in *TH. */
  269. extern td_err_e td_ta_map_id2thr (const td_thragent_t *__ta, pthread_t __pt,
  270. td_thrhandle_t *__th);
  271. /* Map process ID LWPID to thread debug library handle for process
  272. associated with TA and store result in *TH. */
  273. extern td_err_e td_ta_map_lwp2thr (const td_thragent_t *__ta, lwpid_t __lwpid,
  274. td_thrhandle_t *__th);
  275. /* Call for each thread in a process associated with TA the callback function
  276. CALLBACK. */
  277. extern td_err_e td_ta_thr_iter (const td_thragent_t *__ta,
  278. td_thr_iter_f *__callback, void *__cbdata_p,
  279. td_thr_state_e __state, int __ti_pri,
  280. sigset_t *__ti_sigmask_p,
  281. unsigned int __ti_user_flags);
  282. /* Call for each defined thread local data entry the callback function KI. */
  283. extern td_err_e td_ta_tsd_iter (const td_thragent_t *__ta, td_key_iter_f *__ki,
  284. void *__p);
  285. /* Get event address for EVENT. */
  286. extern td_err_e td_ta_event_addr (const td_thragent_t *__ta,
  287. td_event_e __event, td_notify_t *__ptr);
  288. /* Enable EVENT in global mask. */
  289. extern td_err_e td_ta_set_event (const td_thragent_t *__ta,
  290. td_thr_events_t *__event);
  291. /* Disable EVENT in global mask. */
  292. extern td_err_e td_ta_clear_event (const td_thragent_t *__ta,
  293. td_thr_events_t *__event);
  294. /* Return information about last event. */
  295. extern td_err_e td_ta_event_getmsg (const td_thragent_t *__ta,
  296. td_event_msg_t *__msg);
  297. /* Set suggested concurrency level for process associated with TA. */
  298. extern td_err_e td_ta_setconcurrency (const td_thragent_t *__ta, int __level);
  299. /* Enable collecting statistics for process associated with TA. */
  300. extern td_err_e td_ta_enable_stats (const td_thragent_t *__ta, int __enable);
  301. /* Reset statistics. */
  302. extern td_err_e td_ta_reset_stats (const td_thragent_t *__ta);
  303. /* Retrieve statistics from process associated with TA. */
  304. extern td_err_e td_ta_get_stats (const td_thragent_t *__ta,
  305. td_ta_stats_t *__statsp);
  306. /* Validate that TH is a thread handle. */
  307. extern td_err_e td_thr_validate (const td_thrhandle_t *__th);
  308. /* Return information about thread TH. */
  309. extern td_err_e td_thr_get_info (const td_thrhandle_t *__th,
  310. td_thrinfo_t *__infop);
  311. /* Retrieve floating-point register contents of process running thread TH. */
  312. extern td_err_e td_thr_getfpregs (const td_thrhandle_t *__th,
  313. prfpregset_t *__regset);
  314. /* Retrieve general register contents of process running thread TH. */
  315. extern td_err_e td_thr_getgregs (const td_thrhandle_t *__th,
  316. prgregset_t __gregs);
  317. /* Retrieve extended register contents of process running thread TH. */
  318. extern td_err_e td_thr_getxregs (const td_thrhandle_t *__th, void *__xregs);
  319. /* Get size of extended register set of process running thread TH. */
  320. extern td_err_e td_thr_getxregsize (const td_thrhandle_t *__th, int *__sizep);
  321. /* Set floating-point register contents of process running thread TH. */
  322. extern td_err_e td_thr_setfpregs (const td_thrhandle_t *__th,
  323. const prfpregset_t *__fpregs);
  324. /* Set general register contents of process running thread TH. */
  325. extern td_err_e td_thr_setgregs (const td_thrhandle_t *__th,
  326. prgregset_t __gregs);
  327. /* Set extended register contents of process running thread TH. */
  328. extern td_err_e td_thr_setxregs (const td_thrhandle_t *__th,
  329. const void *__addr);
  330. /* Get address of the given module's TLS storage area for the given thread. */
  331. extern td_err_e td_thr_tlsbase (const td_thrhandle_t *__th,
  332. unsigned long int __modid,
  333. psaddr_t *__base);
  334. /* Get address of thread local variable. */
  335. extern td_err_e td_thr_tls_get_addr (const td_thrhandle_t *__th,
  336. void *__map_address, size_t __offset,
  337. void **__address);
  338. /* Enable reporting for EVENT for thread TH. */
  339. extern td_err_e td_thr_event_enable (const td_thrhandle_t *__th, int __event);
  340. /* Enable EVENT for thread TH. */
  341. extern td_err_e td_thr_set_event (const td_thrhandle_t *__th,
  342. td_thr_events_t *__event);
  343. /* Disable EVENT for thread TH. */
  344. extern td_err_e td_thr_clear_event (const td_thrhandle_t *__th,
  345. td_thr_events_t *__event);
  346. /* Get event message for thread TH. */
  347. extern td_err_e td_thr_event_getmsg (const td_thrhandle_t *__th,
  348. td_event_msg_t *__msg);
  349. /* Set priority of thread TH. */
  350. extern td_err_e td_thr_setprio (const td_thrhandle_t *__th, int __prio);
  351. /* Set pending signals for thread TH. */
  352. extern td_err_e td_thr_setsigpending (const td_thrhandle_t *__th,
  353. unsigned char __n, const sigset_t *__ss);
  354. /* Set signal mask for thread TH. */
  355. extern td_err_e td_thr_sigsetmask (const td_thrhandle_t *__th,
  356. const sigset_t *__ss);
  357. /* Return thread local data associated with key TK in thread TH. */
  358. extern td_err_e td_thr_tsd (const td_thrhandle_t *__th,
  359. const thread_key_t __tk, void **__data);
  360. /* Suspend execution of thread TH. */
  361. extern td_err_e td_thr_dbsuspend (const td_thrhandle_t *__th);
  362. /* Resume execution of thread TH. */
  363. extern td_err_e td_thr_dbresume (const td_thrhandle_t *__th);
  364. #ifdef __cplusplus
  365. }
  366. #endif
  367. #endif /* thread_db.h */