Subversion Repositories HelenOS

Rev

Rev 2894 | Rev 2899 | 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.
  36.  */
  37.  
  38. #include <synch/waitq.h>
  39. #include <console/klog.h>
  40. #include <udebug/udebug.h>
  41. #include <errno.h>
  42. #include <arch.h>
  43.  
  44. void udebug_stoppable_begin(void)
  45. {
  46.     int nsc;
  47.     call_t *db_call, *go_call;
  48.     ipl_t ipl;
  49.  
  50.     ipl = interrupts_disable();
  51.     spinlock_lock(&TASK->lock);
  52.  
  53.     nsc = --TASK->not_stoppable_count;
  54.     db_call = TASK->debug_begin_call;
  55.  
  56.     if (TASK->dt_state == UDEBUG_TS_BEGINNING) {
  57.         klog_printf("udebug_stoppable_begin");
  58.         klog_printf(" - nsc := %d", nsc);
  59.     }
  60.  
  61.     if (TASK->dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
  62.         /*
  63.          * This was the last non-stoppable thread. Reply to
  64.          * DEBUG_BEGIN call.
  65.          */
  66.  
  67.         /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  68.         spinlock_lock(&THREAD->debug_lock);
  69.         THREAD->debug_stoppable = true;
  70.         spinlock_unlock(&THREAD->debug_lock);
  71.  
  72.         TASK->dt_state = UDEBUG_TS_ACTIVE;
  73.         TASK->debug_begin_call = NULL;
  74.         spinlock_unlock(&TASK->lock);
  75.         interrupts_restore(ipl);
  76.  
  77.         IPC_SET_RETVAL(db_call->data, 0);
  78.         klog_printf("udebug_stoppable_begin/ipc_answer");
  79.         ipc_answer(&TASK->answerbox, db_call);     
  80.  
  81.     } else if (TASK->dt_state == UDEBUG_TS_ACTIVE) {
  82.         /*
  83.          * Active debugging session
  84.          */
  85.  
  86.         /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  87.         spinlock_lock(&THREAD->debug_lock);
  88.         THREAD->debug_stoppable = true;
  89.  
  90.         if (THREAD->debug_stop) {
  91.             /*
  92.              * Thread was requested to stop - answer go call
  93.              */
  94.  
  95.             /* Make sure nobody takes this call away from us */
  96.             go_call = THREAD->debug_go_call;
  97.             THREAD->debug_go_call = NULL;
  98.  
  99.             IPC_SET_RETVAL(go_call->data, 0);
  100.             IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
  101.  
  102.             THREAD->cur_event = UDEBUG_EVENT_STOP;
  103.             spinlock_unlock(&THREAD->debug_lock);
  104.  
  105.                 ipc_answer(&TASK->answerbox, go_call);
  106.  
  107.                 spinlock_unlock(&TASK->lock);
  108.             interrupts_restore(ipl);
  109.         } else {
  110.             /*
  111.              * No stop request - nothing happens.
  112.              */
  113.             spinlock_unlock(&THREAD->debug_lock);
  114.                 spinlock_unlock(&TASK->lock);
  115.             interrupts_restore(ipl);
  116.         }
  117.     } else {
  118.         /*
  119.          * All other cases - nothing special happens.
  120.          */
  121.  
  122.         /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  123.         spinlock_lock(&THREAD->debug_lock);
  124.         THREAD->debug_stoppable = true;
  125.         spinlock_unlock(&THREAD->debug_lock);
  126.  
  127.             spinlock_unlock(&TASK->lock);
  128.         interrupts_restore(ipl);
  129.     }
  130. }
  131.  
  132. void udebug_stoppable_end(void)
  133. {
  134.     ipl_t ipl;
  135.  
  136. restart:
  137.     ipl = interrupts_disable();
  138.     spinlock_lock(&TASK->lock);
  139.  
  140.     /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  141.     spinlock_lock(&THREAD->debug_lock);
  142.  
  143.     if (TASK->dt_state == UDEBUG_TS_ACTIVE) {
  144.         klog_printf("udebug_stoppable_end");
  145.         klog_printf("debug_stop=%d", THREAD->debug_stop);
  146.     }
  147.  
  148.     if ((TASK->dt_state == UDEBUG_TS_BEGINNING ||
  149.         TASK->dt_state == UDEBUG_TS_ACTIVE) &&
  150.         THREAD->debug_stop == true) {
  151.         TASK->debug_begin_call = NULL;
  152.         spinlock_unlock(&THREAD->debug_lock);
  153.         spinlock_unlock(&TASK->lock);
  154.         interrupts_restore(ipl);
  155.  
  156.         klog_printf("udebug_stoppable_end: waitq_sleep");
  157.         waitq_sleep(&THREAD->go_wq);
  158.         goto restart;
  159.         /* must try again - have to lose stoppability atomically */
  160.     } else {
  161.         ++TASK->not_stoppable_count;
  162.         THREAD->debug_stoppable = false;
  163.  
  164.         spinlock_unlock(&THREAD->debug_lock);
  165.         spinlock_unlock(&TASK->lock);
  166.         interrupts_restore(ipl);
  167.     }
  168. }
  169.  
  170. void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
  171.     unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc)
  172. {
  173.     call_t *call;
  174.     ipl_t ipl;
  175.  
  176.     ipl = interrupts_disable();
  177.     spinlock_lock(&THREAD->debug_lock);
  178.  
  179.     /* Must only generate events when in debugging session and have go */
  180.     if (THREAD->debug_active != true ||
  181.         THREAD->debug_stop == true) {
  182.         spinlock_unlock(&THREAD->debug_lock);
  183.         interrupts_restore(ipl);
  184.         return;
  185.     }
  186.  
  187.     klog_printf("udebug_syscall_event");
  188.     call = THREAD->debug_go_call;
  189.     IPC_SET_RETVAL(call->data, 0);
  190.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_SYSCALL);
  191.     IPC_SET_ARG2(call->data, id);
  192.     IPC_SET_ARG3(call->data, rc);
  193.     klog_printf("udebug_syscall_event/ipc_answer");
  194.  
  195.     THREAD->syscall_args[0] = a1;
  196.     THREAD->syscall_args[1] = a2;
  197.     THREAD->syscall_args[2] = a3;
  198.     THREAD->syscall_args[3] = a4;
  199.     THREAD->syscall_args[4] = a5;
  200.     THREAD->syscall_args[5] = a6;
  201.  
  202.     /*
  203.      * Make sure debug_stop is true when going to sleep
  204.      * in case we get woken up by DEBUG_END. (At which
  205.      * point it must be back to the initial true value).
  206.      */
  207.     THREAD->debug_stop = true;
  208.  
  209.     THREAD->cur_event = UDEBUG_EVENT_SYSCALL;
  210.     spinlock_unlock(&THREAD->debug_lock);
  211.  
  212.     spinlock_lock(&TASK->lock);
  213.     ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
  214.     spinlock_unlock(&TASK->lock);
  215.  
  216.     interrupts_restore(ipl);
  217.  
  218.     waitq_sleep(&THREAD->go_wq);
  219. }
  220.  
  221. void udebug_new_thread_event(struct thread *t)
  222. {
  223.     call_t *call;
  224.     ipl_t ipl;
  225.  
  226.     ipl = interrupts_disable();
  227.     spinlock_lock(&THREAD->debug_lock);
  228.  
  229.     klog_printf("udebug_new_thread_event");
  230.     klog_printf("- check state");
  231.  
  232.     /* Must only generate events when in debugging session */
  233.     if (THREAD->debug_active != true) {
  234.         klog_printf("- debug_active: %s, debug_stop: %s",
  235.             THREAD->debug_active ? "yes(+)" : "no(-)",
  236.             THREAD->debug_stop ? "yes(-)" : "no(+)");
  237.         spinlock_unlock(&THREAD->debug_lock);
  238.         interrupts_restore(ipl);
  239.         return;
  240.     }
  241.  
  242.     klog_printf("- trigger event");
  243.  
  244.     call = THREAD->debug_go_call;
  245.     IPC_SET_RETVAL(call->data, 0);
  246.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_NEW_THREAD);
  247.     IPC_SET_ARG2(call->data, (unative_t)t);
  248.  
  249.     /*
  250.      * Make sure debug_stop is true when going to sleep
  251.      * in case we get woken up by DEBUG_END. (At which
  252.      * point it must be back to the initial true value).
  253.      */
  254.     THREAD->debug_stop = true;
  255.  
  256.     THREAD->cur_event = UDEBUG_EVENT_NEW_THREAD;
  257.     spinlock_unlock(&THREAD->debug_lock);
  258.  
  259.     spinlock_lock(&TASK->lock);
  260.     ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
  261.     spinlock_unlock(&TASK->lock);
  262.  
  263.     interrupts_restore(ipl);
  264.     klog_printf("- sleep");
  265.  
  266.     waitq_sleep(&THREAD->go_wq);
  267. }
  268.  
  269. /**
  270.  * Terminate task debugging session.
  271.  *
  272.  * \param ta Must be already locked and interrupts must be disabled.
  273.  * \return Zero on success or negative error code.
  274.  */
  275. int udebug_task_cleanup(struct task *ta)
  276. {
  277.     thread_t *t;
  278.     link_t *cur;
  279.     int flags;
  280.  
  281.     klog_printf("udebug_task_cleanup()");
  282.     klog_printf("task %llu", ta->taskid);
  283.  
  284.     if (ta->dt_state == UDEBUG_TS_BEGINNING &&
  285.         ta->dt_state != UDEBUG_TS_ACTIVE) {
  286.         klog_printf("udebug_task_cleanup(): task not being debugged");
  287.         return EINVAL;
  288.     }
  289.  
  290.     /* Finish debugging of all userspace threads */
  291.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  292.         t = list_get_instance(cur, thread_t, th_link);
  293.  
  294.         spinlock_lock(&t->debug_lock);
  295.         spinlock_lock(&t->lock);
  296.  
  297.         flags = t->flags;
  298.  
  299.         spinlock_unlock(&t->lock);
  300.  
  301.         /* Only process userspace threads */
  302.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  303.             /* Prevent any further debug activity in thread */
  304.             t->debug_active = false;
  305.             t->cur_event = 0;   /* none */
  306.  
  307.             /* Still has go? */
  308.             if (t->debug_stop == false) {
  309.                 /*
  310.                 * Yes, so clear go. As debug_active == false,
  311.                  * this doesn't affect anything.
  312.                  */
  313.                 t->debug_stop = true;  
  314.  
  315.                 /* Answer GO call */
  316.                 klog_printf("answer GO call with EVENT_FINISHED");
  317.                 IPC_SET_RETVAL(t->debug_go_call->data, 0);
  318.                 IPC_SET_ARG1(t->debug_go_call->data, UDEBUG_EVENT_FINISHED);
  319.                 ipc_answer(&ta->answerbox, t->debug_go_call);
  320.             } else {
  321.                 /*
  322.                  * Debug_stop is already at initial value.
  323.                  * Yet this means the thread needs waking up.
  324.                  */
  325.  
  326.                 /*
  327.                  * t's lock must not be held when calling
  328.                  * waitq_wakeup.
  329.                  */
  330.                 waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  331.             }
  332.         }
  333.         spinlock_unlock(&t->debug_lock);
  334.     }
  335.  
  336.     ta->dt_state = UDEBUG_TS_INACTIVE;
  337.     ta->debugger = NULL;
  338.  
  339.     return 0;
  340. }
  341.  
  342.  
  343. /** @}
  344.  */
  345.