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