Subversion Repositories HelenOS

Rev

Rev 2896 | Rev 2898 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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
  35.  * @brief   Udebug operations.
  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 buf_size, size_t *n)
  239. {
  240.     thread_t *t;
  241.     link_t *cur;
  242.     unative_t tid;
  243.     unsigned copied_ids;
  244.     ipl_t ipl;
  245.     unative_t *id_buffer;
  246.     int flags;
  247.     size_t max_ids;
  248.  
  249.     klog_printf("udebug_thread_read()");
  250.  
  251.     /* Allocate a buffer to hold thread IDs */
  252.     id_buffer = malloc(buf_size, 0);
  253.     if (!id_buffer) return ENOMEM;
  254.  
  255.     ipl = interrupts_disable();
  256.     spinlock_lock(&TASK->lock);
  257.  
  258.     /* Verify task state */
  259.     if (TASK->dt_state != UDEBUG_TS_ACTIVE) {
  260.         spinlock_unlock(&TASK->lock);
  261.         interrupts_restore(ipl);
  262.  
  263.         return EINVAL;
  264.     }
  265.  
  266.     /* Copy down the thread IDs */
  267.  
  268.     max_ids = buf_size / sizeof(unative_t);
  269.     copied_ids = 0;
  270.  
  271.     for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
  272.         /* Do not write past end of buffer */
  273.         if (copied_ids >= max_ids) break;
  274.  
  275.         t = list_get_instance(cur, thread_t, th_link);
  276.  
  277.         spinlock_lock(&t->lock);
  278.         flags = t->flags;
  279.         spinlock_unlock(&t->lock);
  280.  
  281.         /* Not interested in kernel threads */
  282.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  283.             /* Using thread struct pointer as identification hash */
  284.             tid = (unative_t) t;
  285.             id_buffer[copied_ids++] = tid;
  286.         }
  287.     }
  288.  
  289.     spinlock_unlock(&TASK->lock);
  290.     interrupts_restore(ipl);
  291.  
  292.     *buffer = id_buffer;
  293.     *n = copied_ids * sizeof(unative_t);
  294.  
  295.     return 0;
  296. }
  297.  
  298. int udebug_args_read(thread_t *t, void **buffer)
  299. {
  300.     int rc;
  301.     ipl_t ipl;
  302.     unative_t *arg_buffer;
  303.  
  304.     klog_printf("udebug_args_read()");
  305.  
  306.     /* Prepare a buffer to hold the arguments */
  307.     arg_buffer = malloc(6 * sizeof(unative_t), 0);
  308.     if (!arg_buffer) return ENOMEM;
  309.  
  310.     ipl = interrupts_disable();
  311.  
  312.     /* On success, this will lock t->debug_lock */
  313.     rc = _thread_op_begin(t);
  314.     if (rc != EOK) {
  315.         interrupts_restore(ipl);
  316.         return rc;
  317.     }
  318.  
  319.     /* Additionally we need to verify that we are inside a syscall */
  320.     if (t->cur_event != UDEBUG_EVENT_SYSCALL) {
  321.         _thread_op_end(t);
  322.         interrupts_restore(ipl);
  323.  
  324.         return EINVAL;
  325.     }
  326.  
  327.     /* Copy to a local buffer before releasing the lock */
  328.     memcpy(arg_buffer, t->syscall_args, 6 * sizeof(unative_t));
  329.  
  330.     _thread_op_end(t);
  331.     interrupts_restore(ipl);
  332.  
  333.     *buffer = arg_buffer;
  334.     return 0;
  335. }
  336.  
  337. int udebug_regs_read(thread_t *t, void **buffer, size_t *n)
  338. {
  339.     istate_t *state;
  340.     void *regs_buffer;
  341.     int rc;
  342.     ipl_t ipl;
  343.  
  344.     klog_printf("udebug_regs_read()");
  345.  
  346.     /* Prepare a buffer to hold the registers */
  347.     regs_buffer = malloc(sizeof(istate_t), 0);
  348.     if (!regs_buffer) return ENOMEM;
  349.  
  350.     ipl = interrupts_disable();
  351.  
  352.     /* On success, this will lock t->debug_lock */
  353.     rc = _thread_op_begin(t);
  354.     if (rc != EOK) {
  355.         interrupts_restore(ipl);
  356.         return rc;
  357.     }
  358.  
  359.     state = t->uspace_state;
  360.     if (state == NULL) {
  361.         _thread_op_end(t);
  362.         interrupts_restore(ipl);
  363.         klog_printf("udebug_regs_read() - istate not available");
  364.         return EBUSY;
  365.     }
  366.  
  367.     /* Copy to the allocated buffer */
  368.     memcpy(regs_buffer, state, sizeof(istate_t));
  369.  
  370.     _thread_op_end(t);
  371.     interrupts_restore(ipl);
  372.  
  373.     *buffer = regs_buffer;
  374.     *n = sizeof(istate_t);
  375.  
  376.     return 0;
  377. }
  378.  
  379. int udebug_regs_write(thread_t *t, void *buffer)
  380. {
  381.     int rc;
  382.     istate_t *state;
  383.     ipl_t ipl;
  384.  
  385.     klog_printf("udebug_regs_write()");
  386.  
  387.     /* Try to change the thread's uspace_state */
  388.  
  389.     ipl = interrupts_disable();
  390.  
  391.     /* On success, this will lock t->debug_lock */
  392.     rc = _thread_op_begin(t);
  393.     if (rc != EOK) {
  394.         interrupts_restore(ipl);
  395.         return rc;
  396.     }
  397.  
  398.     state = t->uspace_state;
  399.     if (state == NULL) {
  400.         _thread_op_end(t);
  401.         interrupts_restore(ipl);
  402.         klog_printf("udebug_regs_write() - istate not available");
  403.  
  404.         return EBUSY;
  405.     }
  406.  
  407.     memcpy(t->uspace_state, buffer, sizeof(t->uspace_state));
  408.  
  409.     _thread_op_end(t);
  410.     interrupts_restore(ipl);
  411.  
  412.     return 0;
  413. }
  414.  
  415.  
  416. int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
  417. {
  418.     void *data_buffer;
  419.     int rc;
  420.  
  421.     klog_printf("udebug_mem_read()");
  422.  
  423.     data_buffer = malloc(n, 0);
  424.     if (!data_buffer) return ENOMEM;
  425.  
  426.     klog_printf("udebug_mem_read: src=%u, size=%u", uspace_addr, n);
  427.  
  428.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  429.      * be a problem */
  430.     rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
  431.     if (rc) return rc;
  432.  
  433.     *buffer = data_buffer;
  434.     return 0;
  435. }
  436.  
  437. int udebug_mem_write(unative_t uspace_addr, void *data, size_t n)
  438. {
  439.     int rc;
  440.     udebug_task_state_t dts;
  441.  
  442.     klog_printf("udebug_mem_write()");
  443.  
  444.     /* Verify task state */
  445.     spinlock_lock(&TASK->lock);
  446.     dts = TASK->dt_state;
  447.     spinlock_unlock(&TASK->lock);
  448.  
  449.     if (dts != UDEBUG_TS_ACTIVE)
  450.         return EBUSY;
  451.    
  452.     klog_printf("dst=%u, size=%u", uspace_addr, n);
  453.  
  454.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  455.      * be a problem */
  456.     rc = copy_to_uspace((void *)uspace_addr, data, n);
  457.     if (rc) return rc;
  458.  
  459.     return 0;
  460. }
  461.  
  462. /** @}
  463.  */
  464.