Subversion Repositories HelenOS

Rev

Rev 3031 | Rev 3424 | 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.  * Functions in this file are executed directly in each thread, which
  38.  * may or may not be the subject of debugging. The udebug_stoppable_begin/end()
  39.  * functions are also executed in the clock interrupt handler. To avoid
  40.  * deadlock, functions in this file are protected from the interrupt
  41.  * by locking the recursive lock THREAD->udebug.int_lock (just an atomic
  42.  * variable). This prevents udebug_stoppable_begin/end() from being
  43.  * executed in the interrupt handler (they are skipped).
  44.  *
  45.  * Functions in udebug_ops.c and udebug_ipc.c execute in different threads,
  46.  * so they needn't be protected from the (preemptible) interrupt-initiated
  47.  * code.
  48.  */
  49.  
  50. #include <synch/waitq.h>
  51. #include <console/klog.h>
  52. #include <udebug/udebug.h>
  53. #include <errno.h>
  54. #include <arch.h>
  55.  
  56. static inline void udebug_int_lock(void)
  57. {
  58.     atomic_inc(&THREAD->udebug.int_lock);
  59. }
  60.  
  61. static inline void udebug_int_unlock(void)
  62. {
  63.     atomic_dec(&THREAD->udebug.int_lock);
  64. }
  65.  
  66. void udebug_task_init(udebug_task_t *ut)
  67. {
  68.     mutex_initialize(&ut->lock);
  69.     ut->dt_state = UDEBUG_TS_INACTIVE;
  70.     ut->begin_call = NULL;
  71.     ut->not_stoppable_count = 0;
  72.     ut->evmask = 0;
  73. }
  74.  
  75. void udebug_thread_initialize(udebug_thread_t *ut)
  76. {
  77.     mutex_initialize(&ut->lock);
  78.     waitq_initialize(&ut->go_wq);
  79.  
  80.     /*
  81.      * At the beginning the thread is stoppable, so int_lock be set, too.
  82.      */
  83.     atomic_set(&ut->int_lock, 1);
  84.  
  85.     ut->go_call = NULL;
  86.     ut->uspace_state = NULL;
  87.     ut->stop = true;
  88.     ut->stoppable = true;
  89.     ut->debug_active = false;
  90.     ut->cur_event = 0; /* none */
  91. }
  92.  
  93. static void udebug_wait_for_go(waitq_t *wq)
  94. {
  95.     int rc;
  96.     ipl_t ipl;
  97.  
  98.     ipl = waitq_sleep_prepare(wq);
  99.  
  100.     wq->missed_wakeups = 0; /* Enforce blocking. */
  101.     rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
  102.  
  103.     waitq_sleep_finish(wq, rc, ipl);
  104. }
  105.  
  106. /** Do a preliminary check that a debugging session is in progress.
  107.  *
  108.  * This only requires the THREAD->udebug.lock mutex (and not
  109.  * TASK->udebug.lock mutex). For an undebugged task, this will
  110.  * never block (while there could be collisions by different threads
  111.  * on the TASK mutex), thus improving SMP perormance for undebugged tasks.
  112.  */
  113. static bool udebug_thread_precheck(void)
  114. {
  115.     bool res;
  116.  
  117.     mutex_lock(&THREAD->udebug.lock);
  118.     res = THREAD->udebug.debug_active;
  119.     mutex_unlock(&THREAD->udebug.lock);
  120.  
  121.     return res;
  122. }
  123.  
  124. void udebug_stoppable_begin(void)
  125. {
  126.     int nsc;
  127.     call_t *db_call, *go_call;
  128.  
  129.     ASSERT(THREAD);
  130.     ASSERT(TASK);
  131.  
  132.     udebug_int_lock();
  133.  
  134.     /* Early check for undebugged tasks */
  135.     if (!udebug_thread_precheck()) {
  136.         udebug_int_unlock();
  137.         return;
  138.     }
  139.  
  140.     mutex_lock(&TASK->udebug.lock);
  141.  
  142.     nsc = --TASK->udebug.not_stoppable_count;
  143.  
  144.     /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
  145.     mutex_lock(&THREAD->udebug.lock);
  146.     ASSERT(THREAD->udebug.stoppable == false);
  147.     THREAD->udebug.stoppable = true;
  148.  
  149.     if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
  150.         /*
  151.          * This was the last non-stoppable thread. Reply to
  152.          * DEBUG_BEGIN call.
  153.          */
  154.  
  155.         db_call = TASK->udebug.begin_call;
  156.         ASSERT(db_call);
  157.  
  158.         TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
  159.         TASK->udebug.begin_call = NULL;
  160.  
  161.         IPC_SET_RETVAL(db_call->data, 0);
  162.         ipc_answer(&TASK->answerbox, db_call);     
  163.  
  164.     } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
  165.         /*
  166.          * Active debugging session
  167.          */
  168.  
  169.         if (THREAD->udebug.debug_active && THREAD->udebug.stop) {
  170.             /*
  171.              * Thread was requested to stop - answer go call
  172.              */
  173.  
  174.             /* Make sure nobody takes this call away from us */
  175.             go_call = THREAD->udebug.go_call;
  176.             THREAD->udebug.go_call = NULL;
  177.             ASSERT(go_call);
  178.  
  179.             IPC_SET_RETVAL(go_call->data, 0);
  180.             IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
  181.  
  182.             THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
  183.  
  184.                 ipc_answer(&TASK->answerbox, go_call);
  185.         }
  186.     }
  187.  
  188.     mutex_unlock(&THREAD->udebug.lock);
  189.         mutex_unlock(&TASK->udebug.lock);
  190. }
  191.  
  192. void udebug_stoppable_end(void)
  193. {
  194.     /* Early check for undebugged tasks */
  195.     if (!udebug_thread_precheck()) {
  196.         udebug_int_unlock();
  197.         return;
  198.     }
  199.  
  200. restart:
  201.     mutex_lock(&TASK->udebug.lock);
  202.     mutex_lock(&THREAD->udebug.lock);
  203.  
  204.     if (THREAD->udebug.debug_active &&
  205.         THREAD->udebug.stop == true) {
  206.         TASK->udebug.begin_call = NULL;
  207.         mutex_unlock(&THREAD->udebug.lock);
  208.         mutex_unlock(&TASK->udebug.lock);
  209.  
  210.         udebug_wait_for_go(&THREAD->udebug.go_wq);
  211.  
  212.         goto restart;
  213.         /* must try again - have to lose stoppability atomically */
  214.     } else {
  215.         ++TASK->udebug.not_stoppable_count;
  216.         ASSERT(THREAD->udebug.stoppable == true);
  217.         THREAD->udebug.stoppable = false;
  218.  
  219.         mutex_unlock(&THREAD->udebug.lock);
  220.         mutex_unlock(&TASK->udebug.lock);
  221.     }
  222.  
  223.     udebug_int_unlock();
  224. }
  225.  
  226. /** Upon being scheduled to run, check if the current thread should stop.
  227.  *
  228.  * This function is called from clock(). Preemption is enabled.
  229.  * interrupts are disabled, but since this is called after
  230.  * being scheduled-in, we can enable them, if we're careful enough
  231.  * not to allow arbitrary recursion or deadlock with the thread context.
  232.  */
  233. void udebug_before_thread_runs(void)
  234. {
  235.     ipl_t ipl;
  236.  
  237.     ASSERT(!PREEMPTION_DISABLED);
  238.  
  239.     /*
  240.      * Prevent agains re-entering, such as when preempted inside this
  241.      * function.
  242.      */
  243.     if (atomic_get(&THREAD->udebug.int_lock) != 0)
  244.         return;
  245.  
  246.     udebug_int_lock();
  247.  
  248.     ipl = interrupts_enable();
  249.  
  250.     /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
  251.  
  252.     /* Check if we're supposed to stop */
  253.     udebug_stoppable_begin();
  254.     udebug_stoppable_end();
  255.  
  256.     interrupts_restore(ipl);
  257.  
  258.     udebug_int_unlock();
  259. }
  260.  
  261. void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
  262.     unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
  263.     bool end_variant)
  264. {
  265.     call_t *call;
  266.     udebug_event_t etype;
  267.  
  268.     etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
  269.  
  270.     udebug_int_lock();
  271.  
  272.     /* Early check for undebugged tasks */
  273.     if (!udebug_thread_precheck()) {
  274.         udebug_int_unlock();
  275.         return;
  276.     }
  277.  
  278.     mutex_lock(&TASK->udebug.lock);
  279.     mutex_lock(&THREAD->udebug.lock);
  280.  
  281.     /* Must only generate events when in debugging session and have go */
  282.     if (THREAD->udebug.debug_active != true ||
  283.         THREAD->udebug.stop == true ||
  284.         (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
  285.         mutex_unlock(&THREAD->udebug.lock);
  286.         mutex_unlock(&TASK->udebug.lock);
  287.         return;
  288.     }
  289.  
  290.     //klog_printf("udebug_syscall_event");
  291.     call = THREAD->udebug.go_call;
  292.     THREAD->udebug.go_call = NULL;
  293.  
  294.     IPC_SET_RETVAL(call->data, 0);
  295.     IPC_SET_ARG1(call->data, etype);
  296.     IPC_SET_ARG2(call->data, id);
  297.     IPC_SET_ARG3(call->data, rc);
  298.     //klog_printf("udebug_syscall_event/ipc_answer");
  299.  
  300.     THREAD->udebug.syscall_args[0] = a1;
  301.     THREAD->udebug.syscall_args[1] = a2;
  302.     THREAD->udebug.syscall_args[2] = a3;
  303.     THREAD->udebug.syscall_args[3] = a4;
  304.     THREAD->udebug.syscall_args[4] = a5;
  305.     THREAD->udebug.syscall_args[5] = a6;
  306.  
  307.     /*
  308.      * Make sure udebug.stop is true when going to sleep
  309.      * in case we get woken up by DEBUG_END. (At which
  310.      * point it must be back to the initial true value).
  311.      */
  312.     THREAD->udebug.stop = true;
  313.     THREAD->udebug.cur_event = etype;
  314.  
  315.     ipc_answer(&TASK->answerbox, call);
  316.  
  317.     mutex_unlock(&THREAD->udebug.lock);
  318.     mutex_unlock(&TASK->udebug.lock);
  319.  
  320.     udebug_wait_for_go(&THREAD->udebug.go_wq);
  321.  
  322.     udebug_int_unlock();
  323. }
  324.  
  325. void udebug_thread_b_event(struct thread *t)
  326. {
  327.     call_t *call;
  328.  
  329.     udebug_int_lock();
  330.  
  331.     mutex_lock(&TASK->udebug.lock);
  332.     mutex_lock(&THREAD->udebug.lock);
  333.  
  334.     klog_printf("udebug_thread_b_event");
  335.     klog_printf("- check state");
  336.  
  337.     /* Must only generate events when in debugging session */
  338.     if (THREAD->udebug.debug_active != true) {
  339.         klog_printf("- debug_active: %s, udebug.stop: %s",
  340.             THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
  341.             THREAD->udebug.stop ? "yes(-)" : "no(+)");
  342.         mutex_unlock(&THREAD->udebug.lock);
  343.         mutex_unlock(&TASK->udebug.lock);
  344.         return;
  345.     }
  346.  
  347.     klog_printf("- trigger event");
  348.  
  349.     call = THREAD->udebug.go_call;
  350.     THREAD->udebug.go_call = NULL;
  351.     IPC_SET_RETVAL(call->data, 0);
  352.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
  353.     IPC_SET_ARG2(call->data, (unative_t)t);
  354.  
  355.     /*
  356.      * Make sure udebug.stop is true when going to sleep
  357.      * in case we get woken up by DEBUG_END. (At which
  358.      * point it must be back to the initial true value).
  359.      */
  360.     THREAD->udebug.stop = true;
  361.     THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
  362.  
  363.     ipc_answer(&TASK->answerbox, call);
  364.  
  365.     mutex_unlock(&THREAD->udebug.lock);
  366.     mutex_unlock(&TASK->udebug.lock);
  367.  
  368.     klog_printf("- sleep");
  369.     udebug_wait_for_go(&THREAD->udebug.go_wq);
  370.  
  371.     udebug_int_unlock();
  372. }
  373.  
  374. void udebug_thread_e_event(void)
  375. {
  376.     call_t *call;
  377.  
  378.     udebug_int_lock();
  379.  
  380.     mutex_lock(&TASK->udebug.lock);
  381.     mutex_lock(&THREAD->udebug.lock);
  382.  
  383.     klog_printf("udebug_thread_e_event");
  384.     klog_printf("- check state");
  385.  
  386.     /* Must only generate events when in debugging session */
  387.     if (THREAD->udebug.debug_active != true) {
  388.         klog_printf("- debug_active: %s, udebug.stop: %s",
  389.             THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
  390.             THREAD->udebug.stop ? "yes(-)" : "no(+)");
  391.         mutex_unlock(&THREAD->udebug.lock);
  392.         mutex_unlock(&TASK->udebug.lock);
  393.         return;
  394.     }
  395.  
  396.     klog_printf("- trigger event");
  397.  
  398.     call = THREAD->udebug.go_call;
  399.     THREAD->udebug.go_call = NULL;
  400.     IPC_SET_RETVAL(call->data, 0);
  401.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
  402.  
  403.     /* Prevent any further debug activity in thread */
  404.     THREAD->udebug.debug_active = false;
  405.     THREAD->udebug.cur_event = 0;       /* none */
  406.     THREAD->udebug.stop = true; /* set to initial value */
  407.  
  408.     ipc_answer(&TASK->answerbox, call);
  409.  
  410.     mutex_unlock(&THREAD->udebug.lock);
  411.     mutex_unlock(&TASK->udebug.lock);
  412.  
  413.     /* Leave int_lock enabled */
  414.     /* This event does not sleep - debugging has finished in this thread */
  415. }
  416.  
  417. static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
  418. {
  419.     call_t *call;
  420.  
  421.     udebug_int_lock();
  422.  
  423.     mutex_lock(&TASK->udebug.lock);
  424.     mutex_lock(&THREAD->udebug.lock);
  425.  
  426.     /* Must only generate events when in debugging session and have go */
  427.     if (THREAD->udebug.debug_active != true ||
  428.         THREAD->udebug.stop == true) {
  429.         mutex_unlock(&THREAD->udebug.lock);
  430.         mutex_unlock(&TASK->udebug.lock);
  431.         udebug_int_unlock();
  432.         return;
  433.     }
  434.  
  435.     /* Verify that the event is enabled */
  436.     if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
  437.         mutex_unlock(&THREAD->udebug.lock);
  438.         mutex_unlock(&TASK->udebug.lock);
  439.         udebug_int_unlock();
  440.         return;
  441.     }
  442.  
  443.     klog_printf("udebug_breakpoint/trap_event");
  444.     call = THREAD->udebug.go_call;
  445.     THREAD->udebug.go_call = NULL;
  446.  
  447.     IPC_SET_RETVAL(call->data, 0);
  448.     IPC_SET_ARG1(call->data, etype);
  449.     IPC_SET_ARG2(call->data, addr);
  450.  
  451.     /*
  452.      * Make sure udebug.stop is true when going to sleep
  453.      * in case we get woken up by DEBUG_END. (At which
  454.      * point it must be back to the initial true value).
  455.      */
  456.     THREAD->udebug.stop = true;
  457.     THREAD->udebug.cur_event = etype;
  458.  
  459.     klog_printf("- send answer");
  460.     ipc_answer(&TASK->answerbox, call);
  461.  
  462.     mutex_unlock(&THREAD->udebug.lock);
  463.     mutex_unlock(&TASK->udebug.lock);
  464.  
  465.     udebug_wait_for_go(&THREAD->udebug.go_wq);
  466.  
  467.     udebug_int_unlock();
  468. }
  469.  
  470. void udebug_breakpoint_event(uintptr_t addr)
  471. {
  472.     breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
  473. }
  474.  
  475. void udebug_trap_event(uintptr_t addr)
  476. {
  477.     breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
  478. }
  479.  
  480. /**
  481.  * Terminate task debugging session.
  482.  *
  483.  * \param ta->udebug.lock must be already locked.
  484.  * \return Zero on success or negative error code.
  485.  */
  486. int udebug_task_cleanup(struct task *ta)
  487. {
  488.     thread_t *t;
  489.     link_t *cur;
  490.     int flags;
  491.     ipl_t ipl;
  492.  
  493.     klog_printf("udebug_task_cleanup()");
  494.     klog_printf("task %llu", ta->taskid);
  495.  
  496.     udebug_int_lock();
  497.  
  498.     if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
  499.         ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
  500.         klog_printf("udebug_task_cleanup(): task not being debugged");
  501.         return EINVAL;
  502.     }
  503.  
  504.     /* Finish debugging of all userspace threads */
  505.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  506.         t = list_get_instance(cur, thread_t, th_link);
  507.  
  508.         mutex_lock(&t->udebug.lock);
  509.  
  510.         ipl = interrupts_disable();
  511.         spinlock_lock(&t->lock);
  512.  
  513.         flags = t->flags;
  514.  
  515.         spinlock_unlock(&t->lock);
  516.         interrupts_restore(ipl);
  517.  
  518.         /* Only process userspace threads */
  519.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  520.             /* Prevent any further debug activity in thread */
  521.             t->udebug.debug_active = false;
  522.             t->udebug.cur_event = 0;    /* none */
  523.  
  524.             /* Still has go? */
  525.             if (t->udebug.stop == false) {
  526.                 /*
  527.                 * Yes, so clear go. As debug_active == false,
  528.                  * this doesn't affect anything.
  529.                  */
  530.                 t->udebug.stop = true; 
  531.  
  532.                 /* Answer GO call */
  533.                 klog_printf("answer GO call with EVENT_FINISHED");
  534.                 IPC_SET_RETVAL(t->udebug.go_call->data, 0);
  535.                 IPC_SET_ARG1(t->udebug.go_call->data, UDEBUG_EVENT_FINISHED);
  536.  
  537.                 ipc_answer(&ta->answerbox, t->udebug.go_call);
  538.                 t->udebug.go_call = NULL;
  539.             } else {
  540.                 /*
  541.                  * Debug_stop is already at initial value.
  542.                  * Yet this means the thread needs waking up.
  543.                  */
  544.  
  545.                 /*
  546.                  * t's lock must not be held when calling
  547.                  * waitq_wakeup.
  548.                  */
  549.                 waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
  550.             }
  551.         }
  552.         mutex_unlock(&t->udebug.lock);
  553.     }
  554.  
  555.     ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
  556.     ta->udebug.debugger = NULL;
  557.  
  558.     udebug_int_unlock();
  559.  
  560.     return 0;
  561. }
  562.  
  563.  
  564. /** @}
  565.  */
  566.