Rev 2887 | Rev 2896 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2894 | svoboda | 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 | |||
| 2887 | svoboda | 29 | /** @addtogroup generic |
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @file |
||
| 2894 | svoboda | 35 | * @brief Udebug operations. |
| 2887 | svoboda | 36 | */ |
| 37 | |||
| 38 | #include <console/klog.h> |
||
| 39 | #include <proc/task.h> |
||
| 40 | #include <proc/thread.h> |
||
| 41 | #include <arch.h> |
||
| 42 | #include <errno.h> |
||
| 43 | #include <syscall/copy.h> |
||
| 44 | #include <ipc/ipc.h> |
||
| 45 | #include <udebug/udebug.h> |
||
| 46 | #include <udebug/udebug_ops.h> |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Prepare a thread for a debugging operation. |
||
| 50 | * |
||
| 51 | * Simply put, return thread t with t->debug_lock held, |
||
| 52 | * but only if it verifies all conditions. |
||
| 53 | * |
||
| 54 | * Specifically, verifies that thread t exists, is a userspace thread, |
||
| 55 | * and belongs to the current task (TASK). It also locks t->debug_lock, |
||
| 56 | * making sure that t->debug_active is true - that the thread is |
||
| 57 | * in a valid debugging session. |
||
| 58 | * |
||
| 59 | * Returns EOK if all went well, or an error code otherwise. |
||
| 60 | * Interrupts must be already disabled when calling this function. |
||
| 61 | * |
||
| 62 | * Note: This function sports complicated locking. |
||
| 63 | */ |
||
| 64 | static int _thread_op_begin(thread_t *t) |
||
| 65 | { |
||
| 66 | int rc; |
||
| 67 | task_id_t taskid; |
||
| 68 | |||
| 69 | taskid = TASK->taskid; |
||
| 70 | |||
| 71 | /* Must lock threads_lock to ensure continued existence of the thread */ |
||
| 72 | spinlock_lock(&threads_lock); |
||
| 73 | |||
| 74 | if (!thread_exists(t)) { |
||
| 75 | spinlock_unlock(&threads_lock); |
||
| 76 | return ENOENT; |
||
| 77 | } |
||
| 78 | |||
| 79 | spinlock_lock(&t->debug_lock); |
||
| 80 | spinlock_lock(&t->lock); |
||
| 81 | |||
| 82 | /* Now verify that it's the current task */ |
||
| 83 | if (t->task != TASK) { |
||
| 84 | /* No such thread belonging to callee */ |
||
| 85 | rc = ENOENT; |
||
| 86 | goto error_exit; |
||
| 87 | } |
||
| 88 | |||
| 89 | /* Verify that 't' is a userspace thread */ |
||
| 90 | if ((t->flags & THREAD_FLAG_USPACE) == 0) { |
||
| 91 | /* It's not, deny its existence */ |
||
| 92 | rc = ENOENT; |
||
| 93 | goto error_exit; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ((t->debug_active != true) || (t->debug_stop != true)) { |
||
| 97 | /* Not in debugging session or already has GO */ |
||
| 98 | rc = ENOENT; |
||
| 99 | goto error_exit; |
||
| 100 | } |
||
| 101 | |||
| 102 | spinlock_unlock(&threads_lock); |
||
| 103 | spinlock_unlock(&t->lock); |
||
| 104 | |||
| 105 | /* Only t->debug_lock left */ |
||
| 106 | |||
| 107 | return EOK; /* All went well */ |
||
| 108 | |||
| 109 | |||
| 110 | /* Executed when a check on the thread fails */ |
||
| 111 | error_exit: |
||
| 112 | spinlock_unlock(&t->lock); |
||
| 113 | spinlock_unlock(&t->debug_lock); |
||
| 114 | spinlock_unlock(&threads_lock); |
||
| 115 | |||
| 116 | /* No locks left here */ |
||
| 117 | return rc; /* Some errors occured */ |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | static void _thread_op_end(thread_t *t) |
||
| 122 | { |
||
| 123 | spinlock_unlock(&t->debug_lock); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * \return 0 (ok, but not done yet), 1 (done) or negative error code. |
||
| 128 | */ |
||
| 129 | int udebug_begin(call_t *call) |
||
| 130 | { |
||
| 131 | ipl_t ipl; |
||
| 132 | int reply; |
||
| 133 | |||
| 134 | thread_t *t; |
||
| 135 | link_t *cur; |
||
| 136 | |||
| 137 | klog_printf("udebug_begin()"); |
||
| 138 | |||
| 139 | ipl = interrupts_disable(); |
||
| 140 | klog_printf("debugging task %llu", TASK->taskid); |
||
| 141 | |||
| 142 | spinlock_lock(&TASK->lock); |
||
| 143 | |||
| 144 | if (TASK->dt_state != UDEBUG_TS_INACTIVE) { |
||
| 145 | spinlock_unlock(&TASK->lock); |
||
| 146 | interrupts_restore(ipl); |
||
| 147 | klog_printf("udebug_begin(): busy error"); |
||
| 148 | |||
| 149 | return EBUSY; |
||
| 150 | } |
||
| 151 | |||
| 152 | TASK->dt_state = UDEBUG_TS_BEGINNING; |
||
| 153 | TASK->debug_begin_call = call; |
||
| 154 | TASK->debugger = call->sender; |
||
| 155 | |||
| 156 | if (TASK->not_stoppable_count == 0) { |
||
| 157 | TASK->dt_state = UDEBUG_TS_ACTIVE; |
||
| 158 | TASK->debug_begin_call = NULL; |
||
| 159 | reply = 1; /* immediate reply */ |
||
| 160 | } else { |
||
| 161 | reply = 0; /* no reply */ |
||
| 162 | } |
||
| 163 | |||
| 164 | /* Set debug_active on all of the task's userspace threads */ |
||
| 165 | |||
| 166 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
||
| 167 | t = list_get_instance(cur, thread_t, th_link); |
||
| 168 | |||
| 169 | spinlock_lock(&t->debug_lock); |
||
| 170 | if ((t->flags & THREAD_FLAG_USPACE) != 0) |
||
| 171 | t->debug_active = true; |
||
| 172 | spinlock_unlock(&t->debug_lock); |
||
| 173 | } |
||
| 174 | |||
| 175 | spinlock_unlock(&TASK->lock); |
||
| 176 | interrupts_restore(ipl); |
||
| 177 | |||
| 178 | klog_printf("udebug_begin() done (%s)", |
||
| 179 | reply ? "reply" : "stoppability wait"); |
||
| 180 | |||
| 181 | return reply; |
||
| 182 | } |
||
| 183 | |||
| 184 | int udebug_end(void) |
||
| 185 | { |
||
| 186 | ipl_t ipl; |
||
| 187 | int rc; |
||
| 188 | |||
| 189 | klog_printf("udebug_end()"); |
||
| 190 | |||
| 191 | ipl = interrupts_disable(); |
||
| 192 | spinlock_lock(&TASK->lock); |
||
| 193 | |||
| 194 | rc = udebug_task_cleanup(TASK); |
||
| 195 | |||
| 196 | klog_printf("task %llu", TASK->taskid); |
||
| 197 | |||
| 198 | spinlock_unlock(&TASK->lock); |
||
| 199 | interrupts_restore(ipl); |
||
| 200 | |||
| 201 | if (rc < 0) return EINVAL; |
||
| 202 | |||
| 203 | return 0; |
||
| 204 | } |
||
| 205 | |||
| 206 | int udebug_go(thread_t *t, call_t *call) |
||
| 207 | { |
||
| 208 | ipl_t ipl; |
||
| 209 | int rc; |
||
| 210 | |||
| 211 | klog_printf("udebug_go()"); |
||
| 212 | |||
| 213 | ipl = interrupts_disable(); |
||
| 214 | |||
| 215 | /* On success, this will lock t->debug_lock */ |
||
| 216 | rc = _thread_op_begin(t); |
||
| 217 | if (rc != EOK) { |
||
| 218 | interrupts_restore(ipl); |
||
| 219 | return rc; |
||
| 220 | } |
||
| 221 | |||
| 222 | t->debug_go_call = call; |
||
| 223 | t->debug_stop = false; |
||
| 224 | t->cur_event = 0; /* none */ |
||
| 225 | |||
| 226 | /* |
||
| 227 | * Neither t's lock nor threads_lock may be held during wakeup |
||
| 228 | */ |
||
| 229 | waitq_wakeup(&t->go_wq, WAKEUP_FIRST); |
||
| 230 | |||
| 231 | _thread_op_end(t); |
||
| 232 | interrupts_restore(ipl); |
||
| 233 | |||
| 234 | return 0; |
||
| 235 | } |
||
| 236 | |||
| 237 | |||
| 238 | int udebug_thread_read(void **buffer, size_t *n) |
||
| 239 | { |
||
| 240 | thread_t *t; |
||
| 241 | link_t *cur; |
||
| 242 | unative_t tid; |
||
| 243 | unsigned num_threads, copied_ids; |
||
| 244 | ipl_t ipl; |
||
| 245 | unative_t *id_buffer; |
||
| 246 | int flags; |
||
| 247 | |||
| 248 | klog_printf("udebug_thread_read()"); |
||
| 249 | |||
| 250 | ipl = interrupts_disable(); |
||
| 251 | spinlock_lock(&TASK->lock); |
||
| 252 | |||
| 253 | /* Verify task state */ |
||
| 254 | if (TASK->dt_state != UDEBUG_TS_ACTIVE) { |
||
| 255 | spinlock_unlock(&TASK->lock); |
||
| 256 | interrupts_restore(ipl); |
||
| 257 | |||
| 258 | return EINVAL; |
||
| 259 | } |
||
| 260 | |||
| 261 | /* Count the threads first */ |
||
| 262 | |||
| 263 | num_threads = 0; |
||
| 264 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
||
| 265 | /* Count all threads, to be on the safe side */ |
||
| 266 | ++num_threads; |
||
| 267 | } |
||
| 268 | |||
| 269 | /* Allocate a buffer and copy down the threads' ids */ |
||
| 270 | id_buffer = malloc(num_threads * sizeof(unative_t), 0); // ??? |
||
| 271 | |||
| 272 | copied_ids = 0; |
||
| 273 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
||
| 274 | t = list_get_instance(cur, thread_t, th_link); |
||
| 275 | |||
| 276 | spinlock_lock(&t->lock); |
||
| 277 | flags = t->flags; |
||
| 278 | spinlock_unlock(&t->lock); |
||
| 279 | |||
| 280 | /* Not interested in kernel threads */ |
||
| 281 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
||
| 282 | /* Using thread struct pointer for identification */ |
||
| 283 | tid = (unative_t) t; |
||
| 284 | id_buffer[copied_ids++] = tid; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | spinlock_unlock(&TASK->lock); |
||
| 289 | interrupts_restore(ipl); |
||
| 290 | |||
| 291 | *buffer = id_buffer; |
||
| 292 | *n = copied_ids * sizeof(unative_t); |
||
| 293 | |||
| 294 | return 0; |
||
| 295 | } |
||
| 296 | |||
| 297 | int udebug_args_read(thread_t *t, void **buffer) |
||
| 298 | { |
||
| 299 | int rc; |
||
| 300 | ipl_t ipl; |
||
| 301 | unative_t *arg_buffer; |
||
| 302 | |||
| 303 | klog_printf("udebug_args_read()"); |
||
| 304 | |||
| 305 | ipl = interrupts_disable(); |
||
| 306 | |||
| 307 | /* On success, this will lock t->debug_lock */ |
||
| 308 | rc = _thread_op_begin(t); |
||
| 309 | if (rc != EOK) { |
||
| 310 | interrupts_restore(ipl); |
||
| 311 | return rc; |
||
| 312 | } |
||
| 313 | |||
| 314 | /* Additionally we need to verify that we are inside a syscall */ |
||
| 315 | if (t->cur_event != UDEBUG_EVENT_SYSCALL) { |
||
| 316 | _thread_op_end(t); |
||
| 317 | interrupts_restore(ipl); |
||
| 318 | |||
| 319 | return EINVAL; |
||
| 320 | } |
||
| 321 | |||
| 322 | /* Copy to a local buffer before releasing the lock */ |
||
| 323 | arg_buffer = malloc(6 * sizeof(unative_t), 0); // ??? |
||
| 324 | memcpy(arg_buffer, t->syscall_args, 6 * sizeof(unative_t)); |
||
| 325 | |||
| 326 | _thread_op_end(t); |
||
| 327 | interrupts_restore(ipl); |
||
| 328 | |||
| 329 | *buffer = arg_buffer; |
||
| 330 | return 0; |
||
| 331 | } |
||
| 332 | |||
| 333 | int udebug_regs_read(thread_t *t, void **buffer, size_t *n) |
||
| 334 | { |
||
| 335 | istate_t *state; |
||
| 336 | void *regs_buffer; |
||
| 337 | int rc; |
||
| 338 | ipl_t ipl; |
||
| 339 | |||
| 340 | klog_printf("udebug_regs_read()"); |
||
| 341 | |||
| 342 | ipl = interrupts_disable(); |
||
| 343 | |||
| 344 | /* On success, this will lock t->debug_lock */ |
||
| 345 | rc = _thread_op_begin(t); |
||
| 346 | if (rc != EOK) { |
||
| 347 | interrupts_restore(ipl); |
||
| 348 | return rc; |
||
| 349 | } |
||
| 350 | |||
| 351 | state = t->uspace_state; |
||
| 352 | if (state == NULL) { |
||
| 353 | _thread_op_end(t); |
||
| 354 | interrupts_restore(ipl); |
||
| 355 | klog_printf("udebug_regs_read() - istate not available"); |
||
| 356 | return EBUSY; |
||
| 357 | } |
||
| 358 | |||
| 359 | /* Copy to an allocated buffer */ |
||
| 360 | regs_buffer = malloc(sizeof(istate_t), 0); // ??? |
||
| 361 | memcpy(regs_buffer, state, sizeof(istate_t)); |
||
| 362 | |||
| 363 | _thread_op_end(t); |
||
| 364 | interrupts_restore(ipl); |
||
| 365 | |||
| 366 | *buffer = regs_buffer; |
||
| 367 | *n = sizeof(istate_t); |
||
| 368 | |||
| 369 | return 0; |
||
| 370 | } |
||
| 371 | |||
| 372 | int udebug_regs_write(thread_t *t, void *buffer) |
||
| 373 | { |
||
| 374 | int rc; |
||
| 375 | istate_t *state; |
||
| 376 | ipl_t ipl; |
||
| 377 | |||
| 378 | klog_printf("udebug_regs_write()"); |
||
| 379 | |||
| 380 | /* Try to change the thread's uspace_state */ |
||
| 381 | |||
| 382 | ipl = interrupts_disable(); |
||
| 383 | |||
| 384 | /* On success, this will lock t->debug_lock */ |
||
| 385 | rc = _thread_op_begin(t); |
||
| 386 | if (rc != EOK) { |
||
| 387 | interrupts_restore(ipl); |
||
| 388 | return rc; |
||
| 389 | } |
||
| 390 | |||
| 391 | state = t->uspace_state; |
||
| 392 | if (state == NULL) { |
||
| 393 | _thread_op_end(t); |
||
| 394 | interrupts_restore(ipl); |
||
| 395 | klog_printf("udebug_regs_write() - istate not available"); |
||
| 396 | |||
| 397 | return EBUSY; |
||
| 398 | } |
||
| 399 | |||
| 400 | memcpy(t->uspace_state, buffer, sizeof(t->uspace_state)); |
||
| 401 | |||
| 402 | _thread_op_end(t); |
||
| 403 | interrupts_restore(ipl); |
||
| 404 | |||
| 405 | return 0; |
||
| 406 | } |
||
| 407 | |||
| 408 | |||
| 409 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
||
| 410 | { |
||
| 411 | void *data_buffer; |
||
| 412 | int rc; |
||
| 413 | |||
| 414 | klog_printf("udebug_mem_read()"); |
||
| 415 | |||
| 416 | data_buffer = malloc(n, 0); // ??? |
||
| 417 | klog_printf("udebug_mem_read: src=%u, size=%u", uspace_addr, n); |
||
| 418 | |||
| 419 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
||
| 420 | * be a problem */ |
||
| 421 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
||
| 422 | if (rc) return rc; |
||
| 423 | |||
| 424 | *buffer = data_buffer; |
||
| 425 | return 0; |
||
| 426 | } |
||
| 427 | |||
| 428 | int udebug_mem_write(unative_t uspace_addr, void *data, size_t n) |
||
| 429 | { |
||
| 430 | int rc; |
||
| 431 | udebug_task_state_t dts; |
||
| 432 | |||
| 433 | klog_printf("udebug_mem_write()"); |
||
| 434 | |||
| 435 | /* Verify task state */ |
||
| 436 | spinlock_lock(&TASK->lock); |
||
| 437 | dts = TASK->dt_state; |
||
| 438 | spinlock_unlock(&TASK->lock); |
||
| 439 | |||
| 440 | if (dts != UDEBUG_TS_ACTIVE) |
||
| 441 | return EBUSY; |
||
| 442 | |||
| 443 | klog_printf("dst=%u, size=%u", uspace_addr, n); |
||
| 444 | |||
| 445 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
||
| 446 | * be a problem */ |
||
| 447 | rc = copy_to_uspace((void *)uspace_addr, data, n); |
||
| 448 | if (rc) return rc; |
||
| 449 | |||
| 450 | return 0; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** @} |
||
| 454 | */ |