source: mainline/kernel/generic/src/udebug/udebug_ops.c

Last change on this file was dfa4be62, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 17 months ago

Thread lock is no longer necessary

  • Property mode set to 100644
File size: 15.4 KB
Line 
1/*
2 * Copyright (c) 2008 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup kernel_generic
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Udebug operations.
36 *
37 * Udebug operations on tasks and threads are implemented here. The
38 * functions defined here are called from the udebug_ipc module
39 * when servicing udebug IPC messages.
40 */
41
42#include <debug.h>
43#include <proc/task.h>
44#include <proc/thread.h>
45#include <arch.h>
46#include <errno.h>
47#include <stdbool.h>
48#include <str.h>
49#include <syscall/copy.h>
50#include <ipc/ipc.h>
51#include <udebug/udebug.h>
52#include <udebug/udebug_ops.h>
53#include <memw.h>
54#include <stdlib.h>
55
56/** Prepare a thread for a debugging operation.
57 *
58 * Simply put, return thread t with t->udebug.lock held,
59 * but only if it verifies all conditions.
60 *
61 * Specifically, verifies that thread t exists, is a userspace thread,
62 * and belongs to the current task (TASK). Verifies, that the thread
63 * is (or is not) go according to being_go (typically false).
64 * It also locks t->udebug.lock, making sure that t->udebug.active
65 * is true - that the thread is in a valid debugging session.
66 *
67 * With this verified and the t->udebug.lock mutex held, it is ensured
68 * that the thread cannot leave the debugging session, let alone cease
69 * to exist.
70 *
71 * In this function, holding the TASK->udebug.lock mutex prevents the
72 * thread from leaving the debugging session, while relaxing from
73 * the t->lock spinlock to the t->udebug.lock mutex.
74 *
75 * @param thread Pointer, need not at all be valid.
76 * @param being_go Required thread state.
77 *
78 * Returns EOK if all went well, or an error code otherwise.
79 *
80 */
81static errno_t _thread_op_begin(thread_t *thread, bool being_go)
82{
83 mutex_lock(&TASK->udebug.lock);
84
85 thread = thread_try_get(thread);
86
87 if (!thread) {
88 mutex_unlock(&TASK->udebug.lock);
89 return ENOENT;
90 }
91
92 /* Verify that 'thread' is a userspace thread. */
93 if (!thread->uspace) {
94 mutex_unlock(&TASK->udebug.lock);
95 return ENOENT;
96 }
97
98 /*
99 * Now we need to grab the thread's debug lock for synchronization
100 * of the threads stoppability/stop state.
101 *
102 */
103 mutex_lock(&thread->udebug.lock);
104
105 /* Verify debugging state. */
106 if (thread->udebug.active != true) {
107 /* Not in debugging session or undesired GO state */
108 mutex_unlock(&thread->udebug.lock);
109 mutex_unlock(&TASK->udebug.lock);
110 return ENOENT;
111 }
112
113 /* Now verify that the thread belongs to the current task. */
114 if (thread->task != TASK) {
115 /* No such thread belonging this task */
116 mutex_unlock(&thread->udebug.lock);
117 mutex_unlock(&TASK->udebug.lock);
118 return ENOENT;
119 }
120
121 /* The big task mutex is no longer needed. */
122 mutex_unlock(&TASK->udebug.lock);
123
124 if (thread->udebug.go != being_go) {
125 /* Not in debugging session or undesired GO state. */
126 mutex_unlock(&thread->udebug.lock);
127 return EINVAL;
128 }
129
130 /* Only thread->udebug.lock left. */
131
132 return EOK; /* All went well. */
133}
134
135/** End debugging operation on a thread. */
136static void _thread_op_end(thread_t *thread)
137{
138 mutex_unlock(&thread->udebug.lock);
139
140 /* Drop reference from _thread_op_begin() */
141 thread_put(thread);
142}
143
144/** Begin debugging the current task.
145 *
146 * Initiates a debugging session for the current task (and its threads).
147 * When the debugging session has started a reply should be sent to the
148 * UDEBUG_BEGIN call. This may happen immediately in this function if
149 * all the threads in this task are stoppable at the moment and in this
150 * case the function sets @a *active to @c true.
151 *
152 * Otherwise the function sets @a *active to false and the resonse should
153 * be sent as soon as all the threads become stoppable (i.e. they can be
154 * considered stopped).
155 *
156 * @param call The BEGIN call we are servicing.
157 * @param active Place to store @c true iff we went directly to active state,
158 * @c false if we only went to beginning state
159 *
160 * @return EOK on success, EBUSY if the task is already has an active
161 * debugging session.
162 */
163errno_t udebug_begin(call_t *call, bool *active)
164{
165 LOG("Debugging task %" PRIu64, TASK->taskid);
166
167 mutex_lock(&TASK->udebug.lock);
168
169 if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
170 mutex_unlock(&TASK->udebug.lock);
171 return EBUSY;
172 }
173
174 TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
175 TASK->udebug.begin_call = call;
176 TASK->udebug.debugger = call->sender;
177
178 if (TASK->udebug.not_stoppable_count == 0) {
179 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
180 TASK->udebug.begin_call = NULL;
181 *active = true; /* directly to active state */
182 } else
183 *active = false; /* only in beginning state */
184
185 /* Set udebug.active on all of the task's userspace threads. */
186
187 list_foreach(TASK->threads, th_link, thread_t, thread) {
188 mutex_lock(&thread->udebug.lock);
189 if (thread->uspace) {
190 thread->udebug.active = true;
191 mutex_unlock(&thread->udebug.lock);
192 condvar_broadcast(&thread->udebug.active_cv);
193 } else
194 mutex_unlock(&thread->udebug.lock);
195 }
196
197 mutex_unlock(&TASK->udebug.lock);
198 return EOK;
199}
200
201/** Finish debugging the current task.
202 *
203 * Closes the debugging session for the current task.
204 *
205 * @return Zero on success or an error code.
206 *
207 */
208errno_t udebug_end(void)
209{
210 LOG("Task %" PRIu64, TASK->taskid);
211
212 mutex_lock(&TASK->udebug.lock);
213 errno_t rc = udebug_task_cleanup(TASK);
214 mutex_unlock(&TASK->udebug.lock);
215
216 return rc;
217}
218
219/** Set the event mask.
220 *
221 * Sets the event mask that determines which events are enabled.
222 *
223 * @param mask Or combination of events that should be enabled.
224 *
225 * @return Zero on success or an error code.
226 *
227 */
228errno_t udebug_set_evmask(udebug_evmask_t mask)
229{
230 LOG("mask = 0x%x", mask);
231
232 mutex_lock(&TASK->udebug.lock);
233
234 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
235 mutex_unlock(&TASK->udebug.lock);
236 return EINVAL;
237 }
238
239 TASK->udebug.evmask = mask;
240 mutex_unlock(&TASK->udebug.lock);
241
242 return EOK;
243}
244
245/** Give thread GO.
246 *
247 * Upon recieving a go message, the thread is given GO. Being GO
248 * means the thread is allowed to execute userspace code (until
249 * a debugging event or STOP occurs, at which point the thread loses GO.
250 *
251 * @param thread The thread to operate on (unlocked and need not be valid).
252 * @param call The GO call that we are servicing.
253 *
254 */
255errno_t udebug_go(thread_t *thread, call_t *call)
256{
257 /* On success, this will lock thread->udebug.lock. */
258 errno_t rc = _thread_op_begin(thread, false);
259 if (rc != EOK)
260 return rc;
261
262 thread->udebug.go_call = call;
263 thread->udebug.go = true;
264 thread->udebug.cur_event = 0; /* none */
265
266 /*
267 * Neither thread's lock nor threads_lock may be held during wakeup.
268 *
269 */
270 waitq_wake_all(&thread->udebug.go_wq);
271
272 _thread_op_end(thread);
273
274 return EOK;
275}
276
277/** Stop a thread (i.e. take its GO away)
278 *
279 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
280 * can be considered stopped).
281 *
282 * @param thread The thread to operate on (unlocked and need not be valid).
283 * @param call The GO call that we are servicing.
284 *
285 */
286errno_t udebug_stop(thread_t *thread, call_t *call)
287{
288 LOG("udebug_stop()");
289
290 /*
291 * On success, this will lock thread->udebug.lock. Note that this
292 * makes sure the thread is not stopped.
293 *
294 */
295 errno_t rc = _thread_op_begin(thread, true);
296 if (rc != EOK)
297 return rc;
298
299 /* Take GO away from the thread. */
300 thread->udebug.go = false;
301
302 if (thread->udebug.stoppable != true) {
303 /* Answer will be sent when the thread becomes stoppable. */
304 _thread_op_end(thread);
305 return EOK;
306 }
307
308 /*
309 * Answer GO call.
310 *
311 */
312
313 /* Make sure nobody takes this call away from us. */
314 call = thread->udebug.go_call;
315 thread->udebug.go_call = NULL;
316
317 ipc_set_retval(&call->data, 0);
318 ipc_set_arg1(&call->data, UDEBUG_EVENT_STOP);
319
320 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
321
322 _thread_op_end(thread);
323
324 mutex_lock(&TASK->udebug.lock);
325 ipc_answer(&TASK->answerbox, call);
326 mutex_unlock(&TASK->udebug.lock);
327
328 return EOK;
329}
330
331/** Read the list of userspace threads in the current task.
332 *
333 * The list takes the form of a sequence of thread hashes (i.e. the pointers
334 * to thread structures). A buffer of size @a buf_size is allocated and
335 * a pointer to it written to @a buffer. The sequence of hashes is written
336 * into this buffer.
337 *
338 * If the sequence is longer than @a buf_size bytes, only as much hashes
339 * as can fit are copied. The number of bytes copied is stored in @a stored.
340 * The total number of thread bytes that could have been saved had there been
341 * enough space is stored in @a needed.
342 *
343 * The rationale for having @a buf_size is that this function is only
344 * used for servicing the THREAD_READ message, which always specifies
345 * a maximum size for the userspace buffer.
346 *
347 * @param buffer The buffer for storing thread hashes.
348 * @param buf_size Buffer size in bytes.
349 * @param stored The actual number of bytes copied will be stored here.
350 * @param needed Total number of hashes that could have been saved.
351 *
352 */
353errno_t udebug_thread_read(void **buffer, size_t buf_size, size_t *stored,
354 size_t *needed)
355{
356 LOG("udebug_thread_read()");
357
358 /* Allocate a buffer to hold thread IDs */
359 sysarg_t *id_buffer = malloc(buf_size + 1);
360 if (!id_buffer)
361 return ENOMEM;
362
363 mutex_lock(&TASK->udebug.lock);
364
365 /* Verify task state */
366 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
367 mutex_unlock(&TASK->udebug.lock);
368 free(id_buffer);
369 return EINVAL;
370 }
371
372 irq_spinlock_lock(&TASK->lock, true);
373
374 /* Copy down the thread IDs */
375
376 size_t max_ids = buf_size / sizeof(sysarg_t);
377 size_t copied_ids = 0;
378 size_t extra_ids = 0;
379
380 /* FIXME: make sure the thread isn't past debug shutdown... */
381 list_foreach(TASK->threads, th_link, thread_t, thread) {
382 bool uspace = thread->uspace;
383
384 /* Not interested in kernel threads. */
385 if (!uspace)
386 continue;
387
388 if (copied_ids < max_ids) {
389 /* Using thread struct pointer as identification hash */
390 id_buffer[copied_ids++] = (sysarg_t) thread;
391 } else
392 extra_ids++;
393 }
394
395 irq_spinlock_unlock(&TASK->lock, true);
396
397 mutex_unlock(&TASK->udebug.lock);
398
399 *buffer = id_buffer;
400 *stored = copied_ids * sizeof(sysarg_t);
401 *needed = (copied_ids + extra_ids) * sizeof(sysarg_t);
402
403 return EOK;
404}
405
406/** Read task name.
407 *
408 * Returns task name as non-terminated string in a newly allocated buffer.
409 * Also returns the size of the data.
410 *
411 * @param data Place to store pointer to newly allocated block.
412 * @param data_size Place to store size of the data.
413 *
414 * @return EOK on success, ENOMEM if memory allocation failed.
415 *
416 */
417errno_t udebug_name_read(char **data, size_t *data_size)
418{
419 size_t name_size = str_size(TASK->name) + 1;
420
421 *data = malloc(name_size);
422 if (!*data)
423 return ENOMEM;
424 *data_size = name_size;
425
426 memcpy(*data, TASK->name, name_size);
427
428 return EOK;
429}
430
431/** Read the arguments of a system call.
432 *
433 * The arguments of the system call being being executed are copied
434 * to an allocated buffer and a pointer to it is written to @a buffer.
435 * The size of the buffer is exactly such that it can hold the maximum number
436 * of system-call arguments.
437 *
438 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
439 * this function will fail with an EINVAL error code.
440 *
441 * @param thread Thread where call arguments are to be read.
442 * @param buffer Place to store pointer to new buffer.
443 *
444 * @return EOK on success, ENOENT if @a t is invalid, EINVAL
445 * if thread state is not valid for this operation.
446 *
447 */
448errno_t udebug_args_read(thread_t *thread, void **buffer)
449{
450 /* On success, this will lock t->udebug.lock. */
451 errno_t rc = _thread_op_begin(thread, false);
452 if (rc != EOK)
453 return rc;
454
455 /* Additionally we need to verify that we are inside a syscall. */
456 if ((thread->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B) &&
457 (thread->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E)) {
458 _thread_op_end(thread);
459 return EINVAL;
460 }
461
462 /* Prepare a buffer to hold the arguments. */
463 sysarg_t *arg_buffer = malloc(6 * sizeof(sysarg_t));
464 if (!arg_buffer) {
465 _thread_op_end(thread);
466 return ENOMEM;
467 }
468
469 /* Copy to a local buffer before releasing the lock. */
470 memcpy(arg_buffer, thread->udebug.syscall_args, 6 * sizeof(sysarg_t));
471
472 _thread_op_end(thread);
473
474 *buffer = arg_buffer;
475 return EOK;
476}
477
478/** Read the register state of the thread.
479 *
480 * The contents of the thread's istate structure are copied to a newly
481 * allocated buffer and a pointer to it is written to @a buffer. The size of
482 * the buffer will be sizeof(istate_t).
483 *
484 * Currently register state cannot be read if the thread is inside a system
485 * call (as opposed to an exception). This is an implementation limit.
486 *
487 * @param thread Thread whose state is to be read.
488 * @param buffer Place to store pointer to new buffer.
489 *
490 * @return EOK on success, ENOENT if @a t is invalid, EINVAL
491 * if thread is not in valid state, EBUSY if istate
492 * is not available.
493 *
494 */
495errno_t udebug_regs_read(thread_t *thread, void **buffer)
496{
497 /* On success, this will lock t->udebug.lock */
498 errno_t rc = _thread_op_begin(thread, false);
499 if (rc != EOK)
500 return rc;
501
502 istate_t *state = thread->udebug.uspace_state;
503 if (state == NULL) {
504 _thread_op_end(thread);
505 return EBUSY;
506 }
507
508 /* Prepare a buffer to hold the data. */
509 istate_t *state_buf = malloc(sizeof(istate_t));
510 if (!state_buf) {
511 _thread_op_end(thread);
512 return ENOMEM;
513 }
514
515 /* Copy to the allocated buffer */
516 memcpy(state_buf, state, sizeof(istate_t));
517
518 _thread_op_end(thread);
519
520 *buffer = (void *) state_buf;
521 return EOK;
522}
523
524/** Read the memory of the debugged task.
525 *
526 * Reads @a n bytes from the address space of the debugged task, starting
527 * from @a uspace_addr. The bytes are copied into an allocated buffer
528 * and a pointer to it is written into @a buffer.
529 *
530 * @param uspace_addr Address from where to start reading.
531 * @param n Number of bytes to read.
532 * @param buffer For storing a pointer to the allocated buffer.
533 *
534 */
535errno_t udebug_mem_read(uspace_addr_t uspace_addr, size_t n, void **buffer)
536{
537 /* Verify task state */
538 mutex_lock(&TASK->udebug.lock);
539
540 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
541 mutex_unlock(&TASK->udebug.lock);
542 return EBUSY;
543 }
544
545 void *data_buffer = malloc(n);
546 if (!data_buffer) {
547 mutex_unlock(&TASK->udebug.lock);
548 return ENOMEM;
549 }
550
551 /*
552 * NOTE: this is not strictly from a syscall... but that shouldn't
553 * be a problem
554 *
555 */
556 errno_t rc = copy_from_uspace(data_buffer, uspace_addr, n);
557 mutex_unlock(&TASK->udebug.lock);
558
559 if (rc != EOK)
560 return rc;
561
562 *buffer = data_buffer;
563 return EOK;
564}
565
566/** @}
567 */
Note: See TracBrowser for help on using the repository browser.