blob: d89904515747737d5495fa6e268c77dc1be70902 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 */
4
5#include <dirent.h>
6#include <sys/ptrace.h>
7#include <stdint.h>
8#include <thread_db.h>
9#include <stdlib.h>
10#include <stdio.h>
11
12extern int ps_pglobal_lookup (void *, const char *obj, const char *name, void **sym_addr);
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -070013extern pid_t ps_getpid(struct ps_prochandle *ph);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080014
15/*
16 * This is the list of "special" symbols we care about whose addresses are
17 * cached by gdbserver from the host at init time.
18 */
19enum {
20 SYM_TD_CREATE,
21 SYM_THREAD_LIST,
22 NUM_SYMS
23};
24
25static char const * gSymbols[] = {
26 [SYM_TD_CREATE] = "_thread_created_hook",
27 NULL
28};
29
30
31char const **
32td_symbol_list(void)
33{
34 return gSymbols;
35}
36
37
38td_err_e
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -070039td_ta_new(struct ps_prochandle * proc_handle, td_thragent_t ** agent_out)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040{
41 td_thragent_t * agent;
42
43 agent = (td_thragent_t *)malloc(sizeof(td_thragent_t));
44 if (!agent) {
45 return TD_MALLOC;
46 }
47
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -070048 agent->pid = ps_getpid(proc_handle);
49 agent->ph = proc_handle;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050 *agent_out = agent;
51
52 return TD_OK;
53}
54
55
56td_err_e
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -070057td_ta_delete(td_thragent_t * ta)
58{
59 free(ta);
60 // FIXME: anything else to do?
61 return TD_OK;
62}
63
64
65/* NOTE: not used by gdb 7.0 */
66
67td_err_e
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068td_ta_set_event(td_thragent_t const * agent, td_thr_events_t * events)
69{
70 return TD_OK;
71}
72
73
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -070074/* NOTE: not used by gdb 7.0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075static td_thrhandle_t gEventMsgHandle;
76
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -070077/* NOTE: not used by gdb 7.0 */
78
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079static int
80_event_getmsg_helper(td_thrhandle_t const * handle, void * bkpt_addr)
81{
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070082#if defined(__arm__)
83 void* pc = (void *)ptrace(PTRACE_PEEKUSR, handle->tid, (void *)60 /* r15/pc */, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084 if (pc == bkpt_addr) {
85 // The hook function takes the id of the new thread as it's first param,
86 // so grab it from r0.
87 gEventMsgHandle.pid = ptrace(PTRACE_PEEKUSR, handle->tid, (void *)0 /* r0 */, NULL);
88 gEventMsgHandle.tid = gEventMsgHandle.pid;
89 return 0x42;
90 }
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070091#elif defined(__i386__)
92 // Get the eip from offset 12*4 = 48 as defined in the struct
93 // user_regs_struct in user_32.h
94 void* pc = (void *)ptrace(PTRACE_PEEKUSR, handle->tid, (void *)48 /* eip */, NULL);
95 // FIXME - pc is a non-decremented breakpoint address, hence the
96 // addition of 1 on test. This seems to work for the thread hook
97 // function in libc.so but should be properly fixed.
98 if (pc == ((int)bkpt_addr + 1)) {
99 // The hook function takes the id of the new thread as it's first
100 // param, so grab it from ecx at offset 4 in struct user_regs_struct
101 // (using fastcall convention for x86)
102 gEventMsgHandle.pid = ptrace(PTRACE_PEEKUSR, handle->tid, (void *)4 /* ecx */, NULL);
103 gEventMsgHandle.tid = gEventMsgHandle.pid;
104 return 0x42;
105 }
Chris Dearman726800e2012-05-01 17:45:53 -0700106#elif defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700107 void* pc = (void *)ptrace(PTRACE_PEEKUSR, handle->tid, (void *)(64*4) /* pc */, NULL);
Chris Dearman726800e2012-05-01 17:45:53 -0700108 if (pc == bkpt_addr) {
109 // The hook function takes the id of the new thread as it's first param,
110 // so grab it from a0
111 gEventMsgHandle.pid = ptrace(PTRACE_PEEKUSR, handle->tid, (void *)(4*4) /* a0 */, NULL);
112 gEventMsgHandle.tid = gEventMsgHandle.pid;
113 return 0x42;
114 }
Bruce Beare8e551a62011-03-28 09:47:35 -0700115#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116 return 0;
117}
118
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -0700119/* NOTE: not used by gdb 7.0 */
120
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121td_err_e
122td_ta_event_getmsg(td_thragent_t const * agent, td_event_msg_t * event)
123{
124 td_err_e err;
125 void * bkpt_addr;
126
127 err = ps_pglobal_lookup(NULL, NULL, gSymbols[SYM_TD_CREATE], &bkpt_addr);
128 if (err) {
129 return err;
130 }
131
132 err = td_ta_thr_iter(agent, _event_getmsg_helper, bkpt_addr, 0, 0, NULL, 0);
133 if (err != 0x42) {
134 return TD_NOMSG;
135 }
136
137 event->event = TD_CREATE;
138 event->th_p = &gEventMsgHandle; // Nasty hack, but it's the only way!
139
140 return TD_OK;
141}
142
143
144td_err_e
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -0700145td_ta_map_lwp2thr(td_thragent_t const * agent, lwpid_t lwpid,
146 td_thrhandle_t *th)
147{
148 th->pid = ps_getpid(agent->ph);
149 th->tid = lwpid;
150 return TD_OK;
151}
152
153
154td_err_e
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155td_thr_get_info(td_thrhandle_t const * handle, td_thrinfo_t * info)
156{
157 info->ti_tid = handle->tid;
Elliott Hughes40eabe22013-02-14 18:59:37 -0800158 info->ti_lid = handle->tid;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159 info->ti_state = TD_THR_SLEEP; /* XXX this needs to be read from /proc/<pid>/task/<tid>.
160 This is only used to see if the thread is a zombie or not */
161 return TD_OK;
162}
163
164
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -0700165/* NOTE: not used by gdb 7.0 */
166
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167td_err_e
168td_thr_event_enable(td_thrhandle_t const * handle, td_event_e event)
169{
170 // I don't think we need to do anything here...
171 return TD_OK;
172}
173
174
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -0700175/* NOTE: not used by gdb 7.0 */
176
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177td_err_e
178td_ta_event_addr(td_thragent_t const * agent, td_event_e event, td_notify_t * notify_out)
179{
180 int32_t err;
181
Bruce Beare8e551a62011-03-28 09:47:35 -0700182 /*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183 * This is nasty, ps_pglobal_lookup is implemented in gdbserver and looks up
184 * the symbol from it's cache, which is populated at start time with the
185 * symbols returned from td_symbol_list via calls back to the host.
186 */
187
188 switch (event) {
189 case TD_CREATE:
190 err = ps_pglobal_lookup(NULL, NULL, gSymbols[SYM_TD_CREATE], &notify_out->u.bptaddr);
191 if (err) {
192 return TD_NOEVENT;
193 }
194 return TD_OK;
195 }
196 return TD_NOEVENT;
197}
198
199
200td_err_e
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -0700201td_ta_clear_event(const td_thragent_t * ta_arg, td_thr_events_t * event)
202{
203 /* Given that gdb 7.0 doesn't use thread events,
204 there's nothing we need to do here. */
205 return TD_OK;
206}
207
208
209td_err_e
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210td_ta_thr_iter(td_thragent_t const * agent, td_thr_iter_f * func, void * cookie,
211 td_thr_state_e state, int32_t prio, sigset_t * sigmask, uint32_t user_flags)
212{
213 td_err_e err = TD_OK;
214 char path[32];
215 DIR * dir;
216 struct dirent * entry;
217 td_thrhandle_t handle;
218
219 snprintf(path, sizeof(path), "/proc/%d/task/", agent->pid);
220 dir = opendir(path);
221 if (!dir) {
222 return TD_NOEVENT;
223 }
224
225 handle.pid = agent->pid;
226 while ((entry = readdir(dir)) != NULL) {
227 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
228 continue;
229 }
230 handle.tid = atoi(entry->d_name);
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -0700231 if (func(&handle, cookie) != 0) {
232 err = TD_DBERR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233 break;
234 }
235 }
236
237 closedir(dir);
238
239 return err;
240}
241
David 'Digit' Turner3d4edfc2010-03-19 16:01:28 -0700242td_err_e
243td_thr_tls_get_addr(const td_thrhandle_t * th,
244 psaddr_t map_address, size_t offset, psaddr_t * address)
245{
246 return TD_NOAPLIC; // FIXME: TODO
247}