Rev 2813 | Rev 2816 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2813 | Rev 2815 | ||
---|---|---|---|
1 | /** @addtogroup generic |
1 | /** @addtogroup generic |
2 | * @{ |
2 | * @{ |
3 | */ |
3 | */ |
4 | 4 | ||
5 | /** |
5 | /** |
6 | * @file |
6 | * @file |
7 | * @brief Tdebug. |
7 | * @brief Tdebug. |
8 | */ |
8 | */ |
9 | 9 | ||
10 | #include <console/klog.h> |
10 | #include <console/klog.h> |
11 | #include <proc/task.h> |
11 | #include <proc/task.h> |
12 | #include <proc/thread.h> |
12 | #include <proc/thread.h> |
- | 13 | #include <arch.h> |
|
13 | #include <errno.h> |
14 | #include <errno.h> |
14 | #include <ipc/ipc.h> |
15 | #include <ipc/ipc.h> |
15 | #include <syscall/copy.h> |
16 | #include <syscall/copy.h> |
16 | #include <udebug/udebug.h> |
17 | #include <udebug/udebug.h> |
17 | #include <udebug/udebug_ipc.h> |
18 | #include <udebug/udebug_ipc.h> |
18 | 19 | ||
19 | static task_t *get_lock_callee_task(phone_t *phone) |
20 | static task_t *get_lock_callee_task(phone_t *phone) |
20 | { |
21 | { |
21 | answerbox_t *answerbox; |
22 | answerbox_t *answerbox; |
22 | task_t *ta; |
23 | task_t *ta; |
23 | 24 | ||
24 | // FIXME: locking!!! |
25 | // FIXME: locking!!! |
25 | 26 | ||
26 | answerbox = phone->callee; |
27 | answerbox = phone->callee; |
27 | ta = answerbox->task; |
28 | ta = answerbox->task; |
28 | 29 | ||
29 | spinlock_lock(&ta->lock); |
30 | spinlock_lock(&ta->lock); |
30 | 31 | ||
31 | return ta; |
32 | return ta; |
32 | } |
33 | } |
33 | 34 | ||
34 | static thread_t *get_task_thread_by_id(task_t *ta, thread_id_t tid) |
35 | static thread_t *get_task_thread_by_id(task_t *ta, thread_id_t tid) |
35 | { |
36 | { |
36 | thread_t *t; |
37 | thread_t *t; |
37 | link_t *cur; |
38 | link_t *cur; |
38 | 39 | ||
39 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
40 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
40 | t = list_get_instance(cur, thread_t, th_link); |
41 | t = list_get_instance(cur, thread_t, th_link); |
41 | if (tid == t->tid) return t; |
42 | if (tid == t->tid) return t; |
42 | } |
43 | } |
43 | 44 | ||
44 | return NULL; |
45 | return NULL; |
45 | } |
46 | } |
46 | 47 | ||
47 | static int udebug_rp_begin(call_t *call, phone_t *phone) |
48 | static int udebug_rp_begin(call_t *call, phone_t *phone) |
48 | { |
49 | { |
49 | task_t *ta; |
50 | task_t *ta; |
50 | 51 | ||
51 | klog_printf("debug_begin()"); |
52 | klog_printf("debug_begin()"); |
52 | 53 | ||
53 | ta = get_lock_callee_task(phone); |
54 | ta = get_lock_callee_task(phone); |
54 | klog_printf("debugging task %llu", ta->taskid); |
55 | klog_printf("debugging task %llu", ta->taskid); |
55 | 56 | ||
56 | if (ta->being_debugged != false) { |
57 | if (ta->being_debugged != false) { |
57 | spinlock_unlock(&ta->lock); |
58 | spinlock_unlock(&ta->lock); |
58 | klog_printf("debug_begin(): busy error"); |
59 | klog_printf("debug_begin(): busy error"); |
59 | return EBUSY; |
60 | return EBUSY; |
60 | } |
61 | } |
61 | 62 | ||
62 | ta->being_debugged = true; |
63 | ta->being_debugged = true; |
63 | ta->stop_request = true; |
64 | ta->stop_request = true; |
64 | ta->debug_begin_call = call; |
65 | ta->debug_begin_call = call; |
65 | 66 | ||
66 | if (ta->not_stoppable_count == 0) { |
67 | if (ta->not_stoppable_count == 0) { |
67 | ta->debug_begin_call = NULL; |
68 | ta->debug_begin_call = NULL; |
68 | ta->stop_request = false; |
69 | ta->stop_request = false; |
69 | spinlock_unlock(&ta->lock); |
70 | spinlock_unlock(&ta->lock); |
70 | klog_printf("debug_begin(): immediate backsend"); |
71 | klog_printf("debug_begin(): immediate backsend"); |
71 | return 1; /* actually we need backsend with 0 retval */ |
72 | return 1; /* actually we need backsend with 0 retval */ |
72 | } |
73 | } |
73 | 74 | ||
74 | spinlock_unlock(&ta->lock); |
75 | spinlock_unlock(&ta->lock); |
75 | 76 | ||
76 | klog_printf("debug_begin() done (wait for stoppability)"); |
77 | klog_printf("debug_begin() done (wait for stoppability)"); |
77 | return 0; |
78 | return 0; |
78 | } |
79 | } |
79 | 80 | ||
80 | static int udebug_rp_go(call_t *call, phone_t *phone) |
81 | static int udebug_rp_go(call_t *call, phone_t *phone) |
81 | { |
82 | { |
82 | thread_t *t; |
83 | thread_t *t; |
83 | task_t *ta; |
84 | task_t *ta; |
84 | 85 | ||
85 | klog_printf("debug_go()"); |
86 | klog_printf("debug_go()"); |
86 | ta = get_lock_callee_task(phone); |
87 | ta = get_lock_callee_task(phone); |
87 | 88 | ||
88 | ta->debug_go_call = call; |
89 | ta->debug_go_call = call; |
89 | t = get_task_thread_by_id(ta, IPC_GET_ARG2(call->data)); |
90 | t = get_task_thread_by_id(ta, IPC_GET_ARG2(call->data)); |
90 | if (t == NULL) { |
91 | if (t == NULL) { |
91 | spinlock_unlock(&ta->lock); |
92 | spinlock_unlock(&ta->lock); |
92 | return ENOENT; |
93 | return ENOENT; |
93 | } |
94 | } |
94 | 95 | ||
95 | klog_printf("debug_go(): waitq_wakeup"); |
96 | klog_printf("debug_go(): waitq_wakeup"); |
96 | waitq_wakeup(&t->go_wq, WAKEUP_FIRST); |
97 | waitq_wakeup(&t->go_wq, WAKEUP_FIRST); |
97 | 98 | ||
98 | spinlock_unlock(&ta->lock); |
99 | spinlock_unlock(&ta->lock); |
99 | 100 | ||
100 | return 0; /* no backsend */ |
101 | return 0; /* no backsend */ |
101 | } |
102 | } |
102 | 103 | ||
103 | static int udebug_rp_args_read(call_t *call, phone_t *phone) |
104 | static int udebug_rp_args_read(call_t *call, phone_t *phone) |
104 | { |
105 | { |
105 | thread_t *t; |
106 | thread_t *t; |
106 | task_t *ta; |
107 | task_t *ta; |
107 | void *uspace_buffer; |
108 | void *uspace_buffer; |
108 | unative_t to_copy; |
109 | unative_t to_copy; |
109 | int rc; |
110 | int rc; |
110 | 111 | ||
111 | klog_printf("debug_args_read()"); |
112 | klog_printf("debug_args_read()"); |
112 | // FIXME: verify task/thread state |
113 | // FIXME: verify task/thread state |
113 | 114 | ||
114 | ta = get_lock_callee_task(phone); |
115 | ta = get_lock_callee_task(phone); |
115 | klog_printf("task %llu", ta->taskid); |
116 | klog_printf("task %llu", ta->taskid); |
116 | t = get_task_thread_by_id(ta, IPC_GET_ARG2(call->data)); |
117 | t = get_task_thread_by_id(ta, IPC_GET_ARG2(call->data)); |
117 | if (t == NULL) { |
118 | if (t == NULL) { |
118 | spinlock_unlock(&ta->lock); |
119 | spinlock_unlock(&ta->lock); |
119 | return ENOENT; |
120 | return ENOENT; |
120 | } |
121 | } |
121 | 122 | ||
122 | uspace_buffer = (void *)IPC_GET_ARG3(call->data); |
123 | uspace_buffer = (void *)IPC_GET_ARG3(call->data); |
123 | to_copy = IPC_GET_ARG4(call->data); |
124 | to_copy = IPC_GET_ARG4(call->data); |
124 | if (to_copy > 6 * sizeof(unative_t)) to_copy = 6 * sizeof(unative_t); |
125 | if (to_copy > 6 * sizeof(unative_t)) to_copy = 6 * sizeof(unative_t); |
125 | 126 | ||
126 | rc = copy_to_uspace(uspace_buffer, t->syscall_args, to_copy); |
127 | rc = copy_to_uspace(uspace_buffer, t->syscall_args, to_copy); |
127 | if (rc != 0) { |
128 | if (rc != 0) { |
128 | spinlock_unlock(&ta->lock); |
129 | spinlock_unlock(&ta->lock); |
129 | klog_printf("debug_args_read() - copy failed"); |
130 | klog_printf("debug_args_read() - copy failed"); |
130 | return rc; |
131 | return rc; |
131 | } |
132 | } |
132 | 133 | ||
133 | spinlock_unlock(&ta->lock); |
134 | spinlock_unlock(&ta->lock); |
134 | 135 | ||
135 | IPC_SET_ARG1(call->data, to_copy); |
136 | IPC_SET_ARG1(call->data, to_copy); |
136 | 137 | ||
137 | klog_printf("debug_args_read() done"); |
138 | klog_printf("debug_args_read() done"); |
138 | return 1; /* actually need becksend with retval 0 */ |
139 | return 1; /* actually need becksend with retval 0 */ |
139 | } |
140 | } |
140 | 141 | ||
141 | static int udebug_rp_thread_read(call_t *call, phone_t *phone) |
142 | static int udebug_rp_thread_read(call_t *call, phone_t *phone) |
142 | { |
143 | { |
143 | thread_t *t; |
144 | thread_t *t; |
144 | link_t *cur; |
145 | link_t *cur; |
145 | task_t *ta; |
146 | task_t *ta; |
146 | unative_t *uspace_buffer; |
147 | unative_t *uspace_buffer; |
147 | unative_t to_copy; |
148 | unative_t to_copy; |
148 | int rc; |
149 | int rc; |
149 | unsigned copied, total; |
150 | unsigned copied, total; |
150 | unsigned buf_size; |
151 | unsigned buf_size; |
151 | unative_t tid; |
152 | unative_t tid; |
152 | 153 | ||
153 | klog_printf("debug_thread_read()"); |
154 | klog_printf("debug_thread_read()"); |
154 | // FIXME: verify task/thread state |
155 | // FIXME: verify task/thread state |
155 | 156 | ||
156 | ta = get_lock_callee_task(phone); |
157 | ta = get_lock_callee_task(phone); |
157 | klog_printf("task %llu", ta->taskid); |
158 | klog_printf("task %llu", ta->taskid); |
158 | 159 | ||
159 | uspace_buffer = (void *)IPC_GET_ARG2(call->data); |
160 | uspace_buffer = (void *)IPC_GET_ARG2(call->data); |
160 | buf_size = IPC_GET_ARG3(call->data); |
161 | buf_size = IPC_GET_ARG3(call->data); |
161 | 162 | ||
162 | copied = total = 0; |
163 | copied = total = 0; |
163 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
164 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
164 | t = list_get_instance(cur, thread_t, th_link); |
165 | t = list_get_instance(cur, thread_t, th_link); |
165 | 166 | ||
166 | /* Not interested in kernel threads */ |
167 | /* Not interested in kernel threads */ |
167 | if ((t->flags & THREAD_FLAG_USPACE) == 0) |
168 | if ((t->flags & THREAD_FLAG_USPACE) == 0) |
168 | continue; |
169 | continue; |
169 | 170 | ||
170 | //FIXME: id cropped!! |
171 | //FIXME: id cropped!! |
171 | tid = (unative_t) t->tid; |
172 | tid = (unative_t) t->tid; |
172 | 173 | ||
173 | to_copy = sizeof(unative_t); |
174 | to_copy = sizeof(unative_t); |
174 | if (copied + to_copy >= buf_size) |
175 | if (copied + to_copy >= buf_size) |
175 | to_copy = buf_size - copied; |
176 | to_copy = buf_size - copied; |
176 | 177 | ||
177 | if (to_copy > 0) { |
178 | if (to_copy > 0) { |
178 | rc = copy_to_uspace(uspace_buffer, &tid, to_copy); |
179 | rc = copy_to_uspace(uspace_buffer, &tid, to_copy); |
179 | if (rc != 0) { |
180 | if (rc != 0) { |
180 | spinlock_unlock(&ta->lock); |
181 | spinlock_unlock(&ta->lock); |
181 | klog_printf("debug_thread_read() - copy failed"); |
182 | klog_printf("debug_thread_read() - copy failed"); |
182 | return rc; |
183 | return rc; |
183 | } |
184 | } |
184 | } |
185 | } |
185 | 186 | ||
186 | ++uspace_buffer; |
187 | ++uspace_buffer; |
187 | total += sizeof(unative_t); |
188 | total += sizeof(unative_t); |
188 | copied += to_copy; |
189 | copied += to_copy; |
189 | } |
190 | } |
190 | 191 | ||
191 | spinlock_unlock(&ta->lock); |
192 | spinlock_unlock(&ta->lock); |
192 | 193 | ||
193 | IPC_SET_ARG1(call->data, copied); |
194 | IPC_SET_ARG1(call->data, copied); |
194 | IPC_SET_ARG2(call->data, total); |
195 | IPC_SET_ARG2(call->data, total); |
195 | 196 | ||
196 | klog_printf("debug_thread_read() done"); |
197 | klog_printf("debug_thread_read() done"); |
197 | return 1; /* actually need becksend with retval 0 */ |
198 | return 1; /* actually need becksend with retval 0 */ |
198 | } |
199 | } |
199 | 200 | ||
200 | int udebug_request_preprocess(call_t *call, phone_t *phone) |
201 | int udebug_request_preprocess(call_t *call, phone_t *phone) |
201 | { |
202 | { |
202 | int rc; |
203 | int rc; |
203 | 204 | ||
204 | switch (IPC_GET_ARG1(call->data)) { |
205 | switch (IPC_GET_ARG1(call->data)) { |
205 | case UDEBUG_M_BEGIN: |
206 | case UDEBUG_M_BEGIN: |
206 | rc = udebug_rp_begin(call, phone); |
207 | rc = udebug_rp_begin(call, phone); |
207 | return rc; |
208 | return rc; |
208 | case UDEBUG_M_GO: |
209 | case UDEBUG_M_GO: |
209 | rc = udebug_rp_go(call, phone); |
210 | rc = udebug_rp_go(call, phone); |
210 | return rc; |
211 | return rc; |
211 | case UDEBUG_M_ARGS_READ: |
212 | case UDEBUG_M_ARGS_READ: |
212 | rc = udebug_rp_args_read(call, phone); |
213 | rc = udebug_rp_args_read(call, phone); |
213 | return rc; |
214 | return rc; |
214 | case UDEBUG_M_THREAD_READ: |
215 | case UDEBUG_M_THREAD_READ: |
215 | rc = udebug_rp_thread_read(call, phone); |
216 | rc = udebug_rp_thread_read(call, phone); |
216 | return rc; |
217 | return rc; |
217 | default: |
218 | default: |
218 | break; |
219 | break; |
219 | } |
220 | } |
220 | 221 | ||
221 | return 0; |
222 | return 0; |
222 | } |
223 | } |
223 | 224 | ||
- | 225 | static void udebug_receive_mem_read(call_t *call) |
|
- | 226 | { |
|
- | 227 | unative_t uspace_dst; |
|
- | 228 | void *uspace_ptr; |
|
- | 229 | unsigned size; |
|
- | 230 | void *buffer; |
|
- | 231 | int rc; |
|
- | 232 | ||
- | 233 | klog_printf("debug_mem_read()"); |
|
- | 234 | uspace_dst = IPC_GET_ARG2(call->data); |
|
- | 235 | uspace_ptr = (void *)IPC_GET_ARG3(call->data); |
|
- | 236 | size = IPC_GET_ARG4(call->data); |
|
- | 237 | ||
- | 238 | buffer = malloc(size, 0); // ??? |
|
- | 239 | klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size); |
|
- | 240 | ||
- | 241 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
|
- | 242 | * be a problem */ |
|
- | 243 | rc = copy_from_uspace(buffer, uspace_ptr, size); |
|
- | 244 | if (rc) { |
|
- | 245 | IPC_SET_RETVAL(call->data, rc); |
|
- | 246 | return; |
|
- | 247 | } |
|
- | 248 | ||
- | 249 | klog_printf("first word: %u", *((unative_t *)buffer)); |
|
- | 250 | ||
- | 251 | IPC_SET_RETVAL(call->data, 0); |
|
- | 252 | /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that |
|
- | 253 | same code in process_answer() can be used |
|
- | 254 | (no way to distinguish method in answer) */ |
|
- | 255 | IPC_SET_ARG1(call->data, uspace_dst); |
|
- | 256 | IPC_SET_ARG2(call->data, size); |
|
- | 257 | call->buffer = buffer; |
|
- | 258 | ||
- | 259 | ipc_answer(&TASK->kernel_box, call); |
|
- | 260 | } |
|
- | 261 | ||
- | 262 | /** |
|
- | 263 | * Handle a debug call received on the kernel answerbox. |
|
- | 264 | * |
|
- | 265 | * This is called by the kbox servicing thread. |
|
- | 266 | */ |
|
- | 267 | void udebug_call_receive(call_t *call) |
|
- | 268 | { |
|
- | 269 | int debug_method; |
|
- | 270 | ||
- | 271 | debug_method = IPC_GET_ARG1(call->data); |
|
- | 272 | ||
- | 273 | switch (debug_method) { |
|
- | 274 | case UDEBUG_M_MEM_READ: |
|
- | 275 | udebug_receive_mem_read(call); |
|
- | 276 | break; |
|
- | 277 | } |
|
- | 278 | } |
|
224 | 279 | ||
225 | /** @} |
280 | /** @} |
226 | */ |
281 | */ |
227 | 282 |