Subversion Repositories HelenOS

Rev

Rev 3014 | Rev 3018 | 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). Verifies, that the thread
  56.  * has (or hasn't) go according to having_go (typically false).
  57.  * It also locks t->debug_lock, making sure that t->debug_active is true
  58.  * - that the thread is in a valid debugging session.
  59.  *
  60.  * Returns EOK if all went well, or an error code otherwise.
  61.  * Interrupts must be already disabled when calling this function.
  62.  *
  63.  * Note: This function sports complicated locking.
  64.  */
  65. static int _thread_op_begin(thread_t *t, bool having_go)
  66. {
  67.     int rc;
  68.     task_id_t taskid;
  69.  
  70.     taskid = TASK->taskid;
  71.  
  72.     /* Must lock threads_lock to ensure continued existence of the thread */
  73.     spinlock_lock(&threads_lock);
  74.  
  75.     if (!thread_exists(t)) {
  76.         spinlock_unlock(&threads_lock);
  77.         return ENOENT;
  78.     }
  79.  
  80.     spinlock_lock(&t->debug_lock);
  81.     spinlock_lock(&t->lock);
  82.    
  83.     /* Now verify that it's the current task */
  84.     if (t->task != TASK) {
  85.         /* No such thread belonging to callee */
  86.         rc = ENOENT;
  87.         goto error_exit;
  88.     }
  89.  
  90.     /* Verify that 't' is a userspace thread */
  91.     if ((t->flags & THREAD_FLAG_USPACE) == 0) {
  92.         /* It's not, deny its existence */
  93.         rc = ENOENT;
  94.         goto error_exit;
  95.     }
  96.  
  97.     if ((t->debug_active != true) || (!t->debug_stop != having_go)) {
  98.         /* Not in debugging session or undesired GO state */
  99.         rc = EINVAL;
  100.         goto error_exit;
  101.     }
  102.  
  103.     spinlock_unlock(&threads_lock);
  104.     spinlock_unlock(&t->lock);
  105.  
  106.     /* Only t->debug_lock left */
  107.  
  108.     return EOK; /* All went well */
  109.  
  110.  
  111.     /* Executed when a check on the thread fails */
  112. error_exit:
  113.     spinlock_unlock(&t->lock);
  114.     spinlock_unlock(&t->debug_lock);
  115.     spinlock_unlock(&threads_lock);
  116.  
  117.     /* No locks left here */
  118.     return rc;  /* Some errors occured */
  119. }
  120.  
  121.  
  122. static void _thread_op_end(thread_t *t)
  123. {
  124.     spinlock_unlock(&t->debug_lock);
  125. }
  126.  
  127. /**
  128.  * \return 0 (ok, but not done yet), 1 (done) or negative error code.
  129.  */
  130. int udebug_begin(call_t *call)
  131. {
  132.     ipl_t ipl;
  133.     int reply;
  134.  
  135.     thread_t *t;
  136.     link_t *cur;
  137.  
  138.     klog_printf("udebug_begin()");
  139.  
  140.     mutex_lock(&TASK->udebug.lock);
  141.     klog_printf("debugging task %llu", TASK->taskid);
  142.  
  143.     if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
  144.         mutex_unlock(&TASK->udebug.lock);
  145.         klog_printf("udebug_begin(): busy error");
  146.  
  147.         return EBUSY;
  148.     }
  149.  
  150.     TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
  151.     TASK->udebug.begin_call = call;
  152.     TASK->udebug.debugger = call->sender;
  153.  
  154.     if (TASK->udebug.not_stoppable_count == 0) {
  155.         TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
  156.         TASK->udebug.begin_call = NULL;
  157.         reply = 1; /* immediate reply */
  158.     } else {
  159.         reply = 0; /* no reply */
  160.     }
  161.    
  162.     /* Set debug_active on all of the task's userspace threads */
  163.  
  164.     for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
  165.         t = list_get_instance(cur, thread_t, th_link);
  166.  
  167.         ipl = interrupts_disable();
  168.         spinlock_lock(&t->debug_lock);
  169.         if ((t->flags & THREAD_FLAG_USPACE) != 0)
  170.             t->debug_active = true;
  171.         spinlock_unlock(&t->debug_lock);
  172.         interrupts_restore(ipl);
  173.     }
  174.  
  175.     mutex_unlock(&TASK->udebug.lock);
  176.  
  177.     klog_printf("udebug_begin() done (%s)",
  178.         reply ? "reply" : "stoppability wait");
  179.  
  180.     return reply;
  181. }
  182.  
  183. int udebug_end(void)
  184. {
  185.     int rc;
  186.  
  187.     klog_printf("udebug_end()");
  188.  
  189.     mutex_lock(&TASK->udebug.lock);
  190.     klog_printf("task %llu", TASK->taskid);
  191.  
  192.     rc = udebug_task_cleanup(TASK);
  193.  
  194.     mutex_unlock(&TASK->udebug.lock);
  195.  
  196.     return rc;
  197. }
  198.  
  199. int udebug_set_evmask(udebug_evmask_t mask)
  200. {
  201.     klog_printf("udebug_set_mask()");
  202.  
  203.     klog_printf("debugging task %llu", TASK->taskid);
  204.  
  205.     mutex_lock(&TASK->udebug.lock);
  206.  
  207.     if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
  208.         mutex_unlock(&TASK->udebug.lock);
  209.         klog_printf("udebug_set_mask(): not active debuging session");
  210.  
  211.         return EINVAL;
  212.     }
  213.  
  214.     TASK->udebug.evmask = mask;
  215.  
  216.     mutex_unlock(&TASK->udebug.lock);
  217.  
  218.     return 0;
  219. }
  220.  
  221.  
  222. int udebug_go(thread_t *t, call_t *call)
  223. {
  224.     ipl_t ipl;
  225.     int rc;
  226.  
  227. //  klog_printf("udebug_go()");
  228.  
  229.     ipl = interrupts_disable();
  230.  
  231.     /* On success, this will lock t->debug_lock */
  232.     rc = _thread_op_begin(t, false);
  233.     if (rc != EOK) {
  234.         interrupts_restore(ipl);
  235.         return rc;
  236.     }
  237.  
  238.     t->debug_go_call = call;
  239.     t->debug_stop = false;
  240.     t->cur_event = 0;   /* none */
  241.  
  242.     /*
  243.      * Neither t's lock nor threads_lock may be held during wakeup
  244.      */
  245.     waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  246.  
  247.     _thread_op_end(t);
  248.     interrupts_restore(ipl);
  249.  
  250.     return 0;
  251. }
  252.  
  253. int udebug_stop(thread_t *t, call_t *call)
  254. {
  255.     ipl_t ipl;
  256.     int rc;
  257.  
  258.     klog_printf("udebug_stop()");
  259.     mutex_lock(&TASK->udebug.lock);
  260.  
  261.     ipl = interrupts_disable();
  262.  
  263.     /*
  264.      * On success, this will lock t->debug_lock. Note that this makes sure
  265.      * the thread is not stopped.
  266.      */
  267.     rc = _thread_op_begin(t, true);
  268.     if (rc != EOK) {
  269.         interrupts_restore(ipl);
  270.         return rc;
  271.     }
  272.  
  273.     /* Take GO away from the thread */
  274.     t->debug_stop = true;
  275.  
  276.     if (!t->debug_stoppable) {
  277.         /* Answer will be sent when the thread becomes stoppable */
  278.         _thread_op_end(t);
  279.         interrupts_restore(ipl);
  280.         return 0;
  281.     }
  282.  
  283.     /*
  284.      * Answer GO call
  285.      */
  286.     klog_printf("udebug_stop - answering go call");
  287.  
  288.     /* Make sure nobody takes this call away from us */
  289.     call = t->debug_go_call;
  290.     t->debug_go_call = NULL;
  291.  
  292.     IPC_SET_RETVAL(call->data, 0);
  293.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
  294.     klog_printf("udebug_stop/ipc_answer");
  295.  
  296.     THREAD->cur_event = UDEBUG_EVENT_STOP;
  297.  
  298.     _thread_op_end(t);
  299.     interrupts_restore(ipl);
  300.  
  301.     ipc_answer(&TASK->answerbox, call);
  302.     mutex_unlock(&TASK->udebug.lock);
  303.  
  304.     klog_printf("udebog_stop/done");
  305.     return 0;
  306. }
  307.  
  308. int udebug_thread_read(void **buffer, size_t buf_size, size_t *n)
  309. {
  310.     thread_t *t;
  311.     link_t *cur;
  312.     unative_t tid;
  313.     unsigned copied_ids;
  314.     ipl_t ipl;
  315.     unative_t *id_buffer;
  316.     int flags;
  317.     size_t max_ids;
  318.  
  319.     klog_printf("udebug_thread_read()");
  320.  
  321.     /* Allocate a buffer to hold thread IDs */
  322.     id_buffer = malloc(buf_size, 0);
  323.  
  324.     mutex_lock(&TASK->udebug.lock);
  325.  
  326.     /* Verify task state */
  327.     if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
  328.         mutex_unlock(&TASK->udebug.lock);
  329.         return EINVAL;
  330.     }
  331.  
  332.     ipl = interrupts_disable();
  333.     spinlock_lock(&TASK->lock);
  334.     /* Copy down the thread IDs */
  335.  
  336.     max_ids = buf_size / sizeof(unative_t);
  337.     copied_ids = 0;
  338.  
  339.     for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
  340.         /* Do not write past end of buffer */
  341.         if (copied_ids >= max_ids) break;
  342.  
  343.         t = list_get_instance(cur, thread_t, th_link);
  344.  
  345.         spinlock_lock(&t->lock);
  346.         flags = t->flags;
  347.         spinlock_unlock(&t->lock);
  348.  
  349.         /* Not interested in kernel threads */
  350.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  351.             /* Using thread struct pointer as identification hash */
  352.             tid = (unative_t) t;
  353.             id_buffer[copied_ids++] = tid;
  354.         }
  355.     }
  356.  
  357.     spinlock_unlock(&TASK->lock);
  358.     interrupts_restore(ipl);
  359.  
  360.     mutex_unlock(&TASK->udebug.lock);
  361.  
  362.     *buffer = id_buffer;
  363.     *n = copied_ids * sizeof(unative_t);
  364.  
  365.     return 0;
  366. }
  367.  
  368. int udebug_args_read(thread_t *t, void **buffer)
  369. {
  370.     int rc;
  371.     ipl_t ipl;
  372.     unative_t *arg_buffer;
  373.  
  374.     klog_printf("udebug_args_read()");
  375.  
  376.     /* Prepare a buffer to hold the arguments */
  377.     arg_buffer = malloc(6 * sizeof(unative_t), 0);
  378.  
  379.     ipl = interrupts_disable();
  380.  
  381.     /* On success, this will lock t->debug_lock */
  382.     rc = _thread_op_begin(t, false);
  383.     if (rc != EOK) {
  384.         interrupts_restore(ipl);
  385.         return rc;
  386.     }
  387.  
  388.     /* Additionally we need to verify that we are inside a syscall */
  389.     if (t->cur_event != UDEBUG_EVENT_SYSCALL_B &&
  390.         t->cur_event != UDEBUG_EVENT_SYSCALL_E) {
  391.         _thread_op_end(t);
  392.         interrupts_restore(ipl);
  393.  
  394.         return EINVAL;
  395.     }
  396.  
  397.     /* Copy to a local buffer before releasing the lock */
  398.     memcpy(arg_buffer, t->syscall_args, 6 * sizeof(unative_t));
  399.  
  400.     _thread_op_end(t);
  401.     interrupts_restore(ipl);
  402.  
  403.     *buffer = arg_buffer;
  404.     return 0;
  405. }
  406.  
  407. int udebug_regs_read(thread_t *t, void *buffer)
  408. {
  409.     istate_t *state;
  410.     int rc;
  411.     ipl_t ipl;
  412.  
  413.     klog_printf("udebug_regs_read()");
  414.  
  415.     ipl = interrupts_disable();
  416.  
  417.     /* On success, this will lock t->debug_lock */
  418.     rc = _thread_op_begin(t, false);
  419.     if (rc != EOK) {
  420.         interrupts_restore(ipl);
  421.         return rc;
  422.     }
  423.  
  424.     state = t->uspace_state;
  425.     if (state == NULL) {
  426.         _thread_op_end(t);
  427.         interrupts_restore(ipl);
  428.         klog_printf("udebug_regs_read() - istate not available");
  429.         return EBUSY;
  430.     }
  431.  
  432.     /* Copy to the allocated buffer */
  433.     memcpy(buffer, state, sizeof(istate_t));
  434.  
  435.     _thread_op_end(t);
  436.     interrupts_restore(ipl);
  437.  
  438.     return 0;
  439. }
  440.  
  441. int udebug_regs_write(thread_t *t, void *buffer)
  442. {
  443.     int rc;
  444.     istate_t *state;
  445.     ipl_t ipl;
  446.  
  447.     klog_printf("udebug_regs_write()");
  448.  
  449.     /* Try to change the thread's uspace_state */
  450.  
  451.     ipl = interrupts_disable();
  452.  
  453.     /* On success, this will lock t->debug_lock */
  454.     rc = _thread_op_begin(t, false);
  455.     if (rc != EOK) {
  456.         klog_printf("error locking thread");
  457.         interrupts_restore(ipl);
  458.         return rc;
  459.     }
  460.  
  461.     state = t->uspace_state;
  462.     if (state == NULL) {
  463.         _thread_op_end(t);
  464.         interrupts_restore(ipl);
  465.         klog_printf("udebug_regs_write() - istate not available");
  466.  
  467.         return EBUSY;
  468.     }
  469.  
  470.     memcpy(t->uspace_state, buffer, sizeof(istate_t));
  471.  
  472.     _thread_op_end(t);
  473.     interrupts_restore(ipl);
  474.  
  475.     return 0;
  476. }
  477.  
  478.  
  479. int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
  480. {
  481.     void *data_buffer;
  482.     int rc;
  483.  
  484.     /* Verify task state */
  485.     mutex_lock(&TASK->udebug.lock);
  486.  
  487.     if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
  488.         mutex_unlock(&TASK->udebug.lock);
  489.         return EBUSY;
  490.     }
  491.  
  492.     data_buffer = malloc(n, 0);
  493.  
  494. //  klog_printf("udebug_mem_read: src=%u, size=%u", uspace_addr, n);
  495.  
  496.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  497.      * be a problem */
  498.     rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
  499.     mutex_unlock(&TASK->udebug.lock);
  500.  
  501.     if (rc != 0) return rc;
  502.  
  503.     *buffer = data_buffer;
  504.     return 0;
  505. }
  506.  
  507. int udebug_mem_write(unative_t uspace_addr, void *data, size_t n)
  508. {
  509.     int rc;
  510.  
  511.     klog_printf("udebug_mem_write()");
  512.  
  513.     /* Verify task state */
  514.     mutex_lock(&TASK->udebug.lock);
  515.  
  516.     if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
  517.         mutex_unlock(&TASK->udebug.lock);
  518.         return EBUSY;
  519.     }
  520.    
  521.     klog_printf("dst=%u, size=%u", uspace_addr, n);
  522.  
  523.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  524.      * be a problem */
  525. //  rc = copy_to_uspace((void *)uspace_addr, data, n);
  526. //  if (rc) return rc;
  527.  
  528.     rc = as_debug_write(uspace_addr, data, n);
  529.  
  530.     mutex_unlock(&TASK->udebug.lock);
  531.  
  532.     return rc;
  533. }
  534.  
  535. /** @}
  536.  */
  537.