1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _LIBTHREAD_DB__THREAD_DB_H
18 #define _LIBTHREAD_DB__THREAD_DB_H
19 
20 #include <pthread.h>
21 #include <signal.h>
22 #include <stdint.h>
23 #include <sys/procfs.h>
24 #include <sys/types.h>
25 
26 #define TD_THR_ANY_USER_FLAGS       0xffffffff
27 #define TD_THR_LOWEST_PRIORITY      -20
28 #define TD_SIGNO_MASK               NULL
29 
30 /* td_err_e values */
31 enum {
32     TD_OK,
33     TD_ERR,
34     TD_NOTHR,
35     TD_NOSV,
36     TD_NOLWP,
37     TD_BADPH,
38     TD_BADTH,
39     TD_BADSH,
40     TD_BADTA,
41     TD_BADKEY,
42     TD_NOMSG,
43     TD_NOFPREGS,
44     TD_NOLIBTHREAD,
45     TD_NOEVENT,
46     TD_NOCAPAB,
47     TD_DBERR,
48     TD_NOAPLIC,
49     TD_NOTSD,
50     TD_MALLOC,
51     TD_PARTIALREG,
52     TD_NOXREGS,
53     TD_VERSION
54 };
55 
56 /*
57  * td_event_e values
58  * NOTE: There is a max of 32 events
59  */
60 enum {
61     TD_CREATE,
62     TD_DEATH
63 };
64 
65 /* td_thr_state_e values */
66 enum {
67     TD_THR_ANY_STATE,
68     TD_THR_UNKNOWN,
69     TD_THR_SLEEP,
70     TD_THR_ZOMBIE
71 };
72 
73 typedef int32_t td_err_e;
74 typedef uint32_t td_event_e;
75 typedef uint32_t td_notify_e;
76 typedef uint32_t td_thr_state_e;
77 typedef pthread_t thread_t;
78 
79 typedef struct
80 {
81     pid_t pid;
82     struct ps_prochandle *ph;
83 } td_thragent_t;
84 
85 typedef struct
86 {
87     pid_t pid;
88     pid_t tid;
89     psaddr_t th_unique;
90 } td_thrhandle_t;
91 
92 typedef struct
93 {
94     td_event_e event;
95     td_thrhandle_t const * th_p;
96     union {
97         void * data;
98     } msg;
99 } td_event_msg_t;
100 
101 typedef struct
102 {
103     uint32_t events;
104 } td_thr_events_t;
105 
106 typedef struct
107 {
108     union {
109         void * bptaddr;
110     } u;
111 } td_notify_t;
112 
113 typedef struct
114 {
115     td_thr_state_e ti_state;
116     thread_t ti_tid; // pthread's id for the thread
117     int32_t ti_lid; // the kernel's id for the thread
118 } td_thrinfo_t;
119 
120 
121 #define td_event_emptyset(set) \
122     (set)->events = 0
123 
124 #define td_event_fillset(set) \
125     (set)->events = 0xffffffff
126 
127 #define td_event_addset(set, n) \
128     (set)->events |= (1 << n)
129 
130 
131 typedef int td_thr_iter_f(td_thrhandle_t const *, void *);
132 
133 
134 struct ps_prochandle;
135 
136 #ifdef __cplusplus
137 extern "C"{
138 #endif
139 
140 extern td_err_e td_ta_new(struct ps_prochandle * proc_handle, td_thragent_t ** thread_agent);
141 
142 extern td_err_e td_ta_delete(td_thragent_t * ta);
143 
144 extern td_err_e td_ta_set_event(td_thragent_t const * agent, td_thr_events_t * event);
145 
146 extern td_err_e td_ta_event_addr(td_thragent_t const * agent, td_event_e event, td_notify_t * notify);
147 
148 extern td_err_e td_ta_clear_event(const td_thragent_t * ta_arg,
149 				  td_thr_events_t * event);
150 
151 extern td_err_e td_ta_event_getmsg(td_thragent_t const * agent, td_event_msg_t * event);
152 
153 extern td_err_e td_ta_map_lwp2thr(td_thragent_t const * agent, lwpid_t lwpid,
154 				  td_thrhandle_t *th);
155 
156 extern td_err_e td_thr_get_info(td_thrhandle_t const * handle,
157 				td_thrinfo_t * info);
158 
159 extern td_err_e td_thr_event_enable(const td_thrhandle_t * handle,
160 				    int event);
161 
162 extern td_err_e td_ta_thr_iter(td_thragent_t const * agent, td_thr_iter_f * func, void * cookie,
163                                td_thr_state_e state, int32_t prio, sigset_t * sigmask, uint32_t user_flags);
164 
165 extern char const ** td_symbol_list(void);
166 
167 extern td_err_e td_thr_get_info(td_thrhandle_t const * handle, td_thrinfo_t * info);
168 
169 extern td_err_e td_thr_tlsbase(const td_thrhandle_t*, unsigned long int, psaddr_t*);
170 
171 extern td_err_e td_thr_tls_get_addr(const td_thrhandle_t*, psaddr_t, size_t, psaddr_t*);
172 
173 #ifdef __cplusplus
174 }
175 #endif
176 
177 #endif
178