Subversion Repositories HelenOS

Rev

Rev 3032 | Rev 3426 | 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.     return;
  238.     ASSERT(!PREEMPTION_DISABLED);
  239.  
  240.     /*
  241.      * Prevent agains re-entering, such as when preempted inside this
  242.      * function.
  243.      */
  244.     if (atomic_get(&THREAD->udebug.int_lock) != 0)
  245.         return;
  246.  
  247.     udebug_int_lock();
  248.  
  249.     ipl = interrupts_enable();
  250.  
  251.     /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
  252.  
  253.     /* Check if we're supposed to stop */
  254.     udebug_stoppable_begin();
  255.     udebug_stoppable_end();
  256.  
  257.     interrupts_restore(ipl);
  258.  
  259.     udebug_int_unlock();
  260. }
  261.  
  262. void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
  263.     unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
  264.     bool end_variant)
  265. {
  266.     call_t *call;
  267.     udebug_event_t etype;
  268.  
  269.     etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
  270.  
  271.     udebug_int_lock();
  272.  
  273.     /* Early check for undebugged tasks */
  274.     if (!udebug_thread_precheck()) {
  275.         udebug_int_unlock();
  276.         return;
  277.     }
  278.  
  279.     mutex_lock(&TASK->udebug.lock);
  280.     mutex_lock(&THREAD->udebug.lock);
  281.  
  282.     /* Must only generate events when in debugging session and have go */
  283.     if (THREAD->udebug.debug_active != true ||
  284.         THREAD->udebug.stop == true ||
  285.         (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
  286.         mutex_unlock(&THREAD->udebug.lock);
  287.         mutex_unlock(&TASK->udebug.lock);
  288.         return;
  289.     }
  290.  
  291.     //klog_printf("udebug_syscall_event");
  292.     call = THREAD->udebug.go_call;
  293.     THREAD->udebug.go_call = NULL;
  294.  
  295.     IPC_SET_RETVAL(call->data, 0);
  296.     IPC_SET_ARG1(call->data, etype);
  297.     IPC_SET_ARG2(call->data, id);
  298.     IPC_SET_ARG3(call->data, rc);
  299.     //klog_printf("udebug_syscall_event/ipc_answer");
  300.  
  301.     THREAD->udebug.syscall_args[0] = a1;
  302.     THREAD->udebug.syscall_args[1] = a2;
  303.     THREAD->udebug.syscall_args[2] = a3;
  304.     THREAD->udebug.syscall_args[3] = a4;
  305.     THREAD->udebug.syscall_args[4] = a5;
  306.     THREAD->udebug.syscall_args[5] = a6;
  307.  
  308.     /*
  309.      * Make sure udebug.stop is true when going to sleep
  310.      * in case we get woken up by DEBUG_END. (At which
  311.      * point it must be back to the initial true value).
  312.      */
  313.     THREAD->udebug.stop = true;
  314.     THREAD->udebug.cur_event = etype;
  315.  
  316.     ipc_answer(&TASK->answerbox, call);
  317.  
  318.     mutex_unlock(&THREAD->udebug.lock);
  319.     mutex_unlock(&TASK->udebug.lock);
  320.  
  321.     udebug_wait_for_go(&THREAD->udebug.go_wq);
  322.  
  323.     udebug_int_unlock();
  324. }
  325.  
  326. void udebug_thread_b_event(struct thread *t)
  327. {
  328.     call_t *call;
  329.  
  330.     udebug_int_lock();
  331.  
  332.     mutex_lock(&TASK->udebug.lock);
  333.     mutex_lock(&THREAD->udebug.lock);
  334.  
  335.     klog_printf("udebug_thread_b_event");
  336.     klog_printf("- check state");
  337.  
  338.     /* Must only generate events when in debugging session */
  339.     if (THREAD->udebug.debug_active != true) {
  340.         klog_printf("- debug_active: %s, udebug.stop: %s",
  341.             THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
  342.             THREAD->udebug.stop ? "yes(-)" : "no(+)");
  343.         mutex_unlock(&THREAD->udebug.lock);
  344.         mutex_unlock(&TASK->udebug.lock);
  345.         return;
  346.     }
  347.  
  348.     klog_printf("- trigger event");
  349.  
  350.     call = THREAD->udebug.go_call;
  351.     THREAD->udebug.go_call = NULL;
  352.     IPC_SET_RETVAL(call->data, 0);
  353.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
  354.     IPC_SET_ARG2(call->data, (unative_t)t);
  355.  
  356.     /*
  357.      * Make sure udebug.stop is true when going to sleep
  358.      * in case we get woken up by DEBUG_END. (At which
  359.      * point it must be back to the initial true value).
  360.      */
  361.     THREAD->udebug.stop = true;
  362.     THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
  363.  
  364.     ipc_answer(&TASK->answerbox, call);
  365.  
  366.     mutex_unlock(&THREAD->udebug.lock);
  367.     mutex_unlock(&TASK->udebug.lock);
  368.  
  369.     klog_printf("- sleep");
  370.     udebug_wait_for_go(&THREAD->udebug.go_wq);
  371.  
  372.     udebug_int_unlock();
  373. }
  374.  
  375. void udebug_thread_e_event(void)
  376. {
  377.     call_t *call;
  378.  
  379.     udebug_int_lock();
  380.  
  381.     mutex_lock(&TASK->udebug.lock);
  382.     mutex_lock(&THREAD->udebug.lock);
  383.  
  384.     klog_printf("udebug_thread_e_event");
  385.     klog_printf("- check state");
  386.  
  387.     /* Must only generate events when in debugging session */
  388.     if (THREAD->udebug.debug_active != true) {
  389.         klog_printf("- debug_active: %s, udebug.stop: %s",
  390.             THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
  391.             THREAD->udebug.stop ? "yes(-)" : "no(+)");
  392.         mutex_unlock(&THREAD->udebug.lock);
  393.         mutex_unlock(&TASK->udebug.lock);
  394.         return;
  395.     }
  396.  
  397.     klog_printf("- trigger event");
  398.  
  399.     call = THREAD->udebug.go_call;
  400.     THREAD->udebug.go_call = NULL;
  401.     IPC_SET_RETVAL(call->data, 0);
  402.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
  403.  
  404.     /* Prevent any further debug activity in thread */
  405.     THREAD->udebug.debug_active = false;
  406.     THREAD->udebug.cur_event = 0;       /* none */
  407.     THREAD->udebug.stop = true; /* set to initial value */
  408.  
  409.     ipc_answer(&TASK->answerbox, call);
  410.  
  411.     mutex_unlock(&THREAD->udebug.lock);
  412.     mutex_unlock(&TASK->udebug.lock);
  413.  
  414.     /* Leave int_lock enabled */
  415.     /* This event does not sleep - debugging has finished in this thread */
  416. }
  417.  
  418. static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
  419. {
  420.     call_t *call;
  421.  
  422.     udebug_int_lock();
  423.  
  424.     mutex_lock(&TASK->udebug.lock);
  425.     mutex_lock(&THREAD->udebug.lock);
  426.  
  427.     /* Must only generate events when in debugging session and have go */
  428.     if (THREAD->udebug.debug_active != true ||
  429.         THREAD->udebug.stop == true) {
  430.         mutex_unlock(&THREAD->udebug.lock);
  431.         mutex_unlock(&TASK->udebug.lock);
  432.         udebug_int_unlock();
  433.         return;
  434.     }
  435.  
  436.     /* Verify that the event is enabled */
  437.     if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
  438.         mutex_unlock(&THREAD->udebug.lock);
  439.         mutex_unlock(&TASK->udebug.lock);
  440.         udebug_int_unlock();
  441.         return;
  442.     }
  443.  
  444.     klog_printf("udebug_breakpoint/trap_event");
  445.     call = THREAD->udebug.go_call;
  446.     THREAD->udebug.go_call = NULL;
  447.  
  448.     IPC_SET_RETVAL(call->data, 0);
  449.     IPC_SET_ARG1(call->data, etype);
  450.     IPC_SET_ARG2(call->data, addr);
  451.  
  452.     /*
  453.      * Make sure udebug.stop is true when going to sleep
  454.      * in case we get woken up by DEBUG_END. (At which
  455.      * point it must be back to the initial true value).
  456.      */
  457.     THREAD->udebug.stop = true;
  458.     THREAD->udebug.cur_event = etype;
  459.  
  460.     klog_printf("- send answer");
  461.     ipc_answer(&TASK->answerbox, call);
  462.  
  463.     mutex_unlock(&THREAD->udebug.lock);
  464.     mutex_unlock(&TASK->udebug.lock);
  465.  
  466.     udebug_wait_for_go(&THREAD->udebug.go_wq);
  467.  
  468.     udebug_int_unlock();
  469. }
  470.  
  471. void udebug_breakpoint_event(uintptr_t addr)
  472. {
  473.     breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
  474. }
  475.  
  476. void udebug_trap_event(uintptr_t addr)
  477. {
  478.     breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
  479. }
  480.  
  481. /**
  482.  * Terminate task debugging session.
  483.  *
  484.  * \param ta->udebug.lock must be already locked.
  485.  * \return Zero on success or negative error code.
  486.  */
  487. int udebug_task_cleanup(struct task *ta)
  488. {
  489.     thread_t *t;
  490.     link_t *cur;
  491.     int flags;
  492.     ipl_t ipl;
  493.  
  494.     klog_printf("udebug_task_cleanup()");
  495.     klog_printf("task %llu", ta->taskid);
  496.  
  497.     udebug_int_lock();
  498.  
  499.     if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
  500.         ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
  501.         klog_printf("udebug_task_cleanup(): task not being debugged");
  502.         return EINVAL;
  503.     }
  504.  
  505.     /* Finish debugging of all userspace threads */
  506.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  507.         t = list_get_instance(cur, thread_t, th_link);
  508.  
  509.         mutex_lock(&t->udebug.lock);
  510.  
  511.         ipl = interrupts_disable();
  512.         spinlock_lock(&t->lock);
  513.  
  514.         flags = t->flags;
  515.  
  516.         spinlock_unlock(&t->lock);
  517.         interrupts_restore(ipl);
  518.  
  519.         /* Only process userspace threads */
  520.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  521.             /* Prevent any further debug activity in thread */
  522.             t->udebug.debug_active = false;
  523.             t->udebug.cur_event = 0;    /* none */
  524.  
  525.             /* Still has go? */
  526.             if (t->udebug.stop == false) {
  527.                 /*
  528.                 * Yes, so clear go. As debug_active == false,
  529.                  * this doesn't affect anything.
  530.                  */
  531.                 t->udebug.stop = true; 
  532.  
  533.                 /* Answer GO call */
  534.                 klog_printf("answer GO call with EVENT_FINISHED");
  535.                 IPC_SET_RETVAL(t->udebug.go_call->data, 0);
  536.                 IPC_SET_ARG1(t->udebug.go_call->data, UDEBUG_EVENT_FINISHED);
  537.  
  538.                 ipc_answer(&ta->answerbox, t->udebug.go_call);
  539.                 t->udebug.go_call = NULL;
  540.             } else {
  541.                 /*
  542.                  * Debug_stop is already at initial value.
  543.                  * Yet this means the thread needs waking up.
  544.                  */
  545.  
  546.                 /*
  547.                  * t's lock must not be held when calling
  548.                  * waitq_wakeup.
  549.                  */
  550.                 waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
  551.             }
  552.         }
  553.         mutex_unlock(&t->udebug.lock);
  554.     }
  555.  
  556.     ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
  557.     ta->udebug.debugger = NULL;
  558.  
  559.     udebug_int_unlock();
  560.  
  561.     return 0;
  562. }
  563.  
  564.  
  565. /** @}
  566.  */
  567.