Subversion Repositories HelenOS

Rev

Rev 2921 | Rev 3015 | 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_task_init(udebug_task_t *ut)
  45. {
  46.     ut->dt_state = UDEBUG_TS_INACTIVE;
  47.     ut->begin_call = NULL;
  48.     ut->not_stoppable_count = 0;
  49.     ut->evmask = 0;
  50. }
  51.  
  52. static void udebug_wait_for_go(waitq_t *wq)
  53. {
  54.     int rc;
  55.     ipl_t ipl;
  56.  
  57.     ipl = waitq_sleep_prepare(wq);
  58.  
  59.     wq->missed_wakeups = 0; /* Enforce blocking. */
  60.     rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
  61.  
  62.     waitq_sleep_finish(wq, rc, ipl);
  63. }
  64.  
  65. void udebug_stoppable_begin(void)
  66. {
  67.     int nsc;
  68.     call_t *db_call, *go_call;
  69.     ipl_t ipl;
  70.  
  71.     ASSERT(THREAD);
  72.     ASSERT(TASK);
  73.  
  74.     ipl = interrupts_disable();
  75.     spinlock_lock(&TASK->lock);
  76.  
  77.     nsc = --TASK->udebug.not_stoppable_count;
  78.  
  79.     if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) {
  80.         klog_printf("udebug_stoppable_begin");
  81.         klog_printf(" - nsc := %d", nsc);
  82.     }
  83.  
  84.     if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
  85.         /*
  86.          * This was the last non-stoppable thread. Reply to
  87.          * DEBUG_BEGIN call.
  88.          */
  89.  
  90.         db_call = TASK->udebug.begin_call;
  91.         ASSERT(db_call);
  92.  
  93.         /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  94.         spinlock_lock(&THREAD->debug_lock);
  95.         THREAD->debug_stoppable = true;
  96.         spinlock_unlock(&THREAD->debug_lock);
  97.  
  98.         TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
  99.         TASK->udebug.begin_call = NULL;
  100.         spinlock_unlock(&TASK->lock);
  101.         interrupts_restore(ipl);
  102.  
  103.         IPC_SET_RETVAL(db_call->data, 0);
  104.         //klog_printf("udebug_stoppable_begin/ipc_answer");
  105.         ipc_answer(&TASK->answerbox, db_call);     
  106.  
  107.     } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
  108.         /*
  109.          * Active debugging session
  110.          */
  111.  
  112.         /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  113.         spinlock_lock(&THREAD->debug_lock);
  114.         THREAD->debug_stoppable = true;
  115.  
  116.         if (THREAD->debug_active && THREAD->debug_stop) {
  117.             /*
  118.              * Thread was requested to stop - answer go call
  119.              */
  120.  
  121.             /* Make sure nobody takes this call away from us */
  122.             go_call = THREAD->debug_go_call;
  123.             THREAD->debug_go_call = NULL;
  124.             ASSERT(go_call);
  125.  
  126.             IPC_SET_RETVAL(go_call->data, 0);
  127.             IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
  128.  
  129.             THREAD->cur_event = UDEBUG_EVENT_STOP;
  130.             spinlock_unlock(&THREAD->debug_lock);
  131.  
  132.                 ipc_answer(&TASK->answerbox, go_call);
  133.  
  134.                 spinlock_unlock(&TASK->lock);
  135.             interrupts_restore(ipl);
  136.         } else {
  137.             /*
  138.              * No stop request - nothing happens.
  139.              */
  140.             spinlock_unlock(&THREAD->debug_lock);
  141.                 spinlock_unlock(&TASK->lock);
  142.             interrupts_restore(ipl);
  143.         }
  144.     } else {
  145.         /*
  146.          * All other cases - nothing special happens.
  147.          */
  148.  
  149.         /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  150.         spinlock_lock(&THREAD->debug_lock);
  151.         THREAD->debug_stoppable = true;
  152.         spinlock_unlock(&THREAD->debug_lock);
  153.  
  154.             spinlock_unlock(&TASK->lock);
  155.         interrupts_restore(ipl);
  156.     }
  157. }
  158.  
  159. void udebug_stoppable_end(void)
  160. {
  161.     ipl_t ipl;
  162.  
  163. restart:
  164.     ipl = interrupts_disable();
  165.     spinlock_lock(&TASK->lock);
  166.  
  167.     /* Lock order OK, THREAD->debug_lock is after TASK->lock */
  168.     spinlock_lock(&THREAD->debug_lock);
  169.  
  170.     if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
  171.         //klog_printf("udebug_stoppable_end");
  172.         //klog_printf("debug_stop=%d", THREAD->debug_stop);
  173.     }
  174.  
  175.     if (THREAD->debug_active &&
  176.         THREAD->debug_stop == true) {
  177.         TASK->udebug.begin_call = NULL;
  178.         spinlock_unlock(&THREAD->debug_lock);
  179.         spinlock_unlock(&TASK->lock);
  180.         interrupts_restore(ipl);
  181.  
  182.         udebug_wait_for_go(&THREAD->go_wq);
  183.  
  184.         goto restart;
  185.         /* must try again - have to lose stoppability atomically */
  186.     } else {
  187.         ++TASK->udebug.not_stoppable_count;
  188.         THREAD->debug_stoppable = false;
  189.  
  190.         spinlock_unlock(&THREAD->debug_lock);
  191.         spinlock_unlock(&TASK->lock);
  192.         interrupts_restore(ipl);
  193.     }
  194. }
  195.  
  196. void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
  197.     unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
  198.     bool end_variant)
  199. {
  200.     call_t *call;
  201.     ipl_t ipl;
  202.     udebug_event_t etype;
  203.  
  204.     etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
  205.  
  206.     ipl = interrupts_disable();
  207.     spinlock_lock(&THREAD->debug_lock);
  208.  
  209.     /* Must only generate events when in debugging session and have go */
  210.     if (THREAD->debug_active != true ||
  211.         THREAD->debug_stop == true ||
  212.         (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
  213.         spinlock_unlock(&THREAD->debug_lock);
  214.         interrupts_restore(ipl);
  215.         return;
  216.     }
  217.  
  218.     //klog_printf("udebug_syscall_event");
  219.     call = THREAD->debug_go_call;
  220.     IPC_SET_RETVAL(call->data, 0);
  221.     IPC_SET_ARG1(call->data, etype);
  222.     IPC_SET_ARG2(call->data, id);
  223.     IPC_SET_ARG3(call->data, rc);
  224.     //klog_printf("udebug_syscall_event/ipc_answer");
  225.  
  226.     THREAD->syscall_args[0] = a1;
  227.     THREAD->syscall_args[1] = a2;
  228.     THREAD->syscall_args[2] = a3;
  229.     THREAD->syscall_args[3] = a4;
  230.     THREAD->syscall_args[4] = a5;
  231.     THREAD->syscall_args[5] = a6;
  232.  
  233.     /*
  234.      * Make sure debug_stop is true when going to sleep
  235.      * in case we get woken up by DEBUG_END. (At which
  236.      * point it must be back to the initial true value).
  237.      */
  238.     THREAD->debug_stop = true;
  239.  
  240.     THREAD->cur_event = etype;
  241.     spinlock_unlock(&THREAD->debug_lock);
  242.  
  243.     spinlock_lock(&TASK->lock);
  244.     ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
  245.     spinlock_unlock(&TASK->lock);
  246.     interrupts_restore(ipl);
  247.  
  248.     udebug_wait_for_go(&THREAD->go_wq);
  249. }
  250.  
  251. void udebug_thread_b_event(struct thread *t)
  252. {
  253.     call_t *call;
  254.     ipl_t ipl;
  255.  
  256.     ipl = interrupts_disable();
  257.     spinlock_lock(&THREAD->debug_lock);
  258.  
  259.     klog_printf("udebug_thread_b_event");
  260.     klog_printf("- check state");
  261.  
  262.     /* Must only generate events when in debugging session */
  263.     if (THREAD->debug_active != true) {
  264.         klog_printf("- debug_active: %s, debug_stop: %s",
  265.             THREAD->debug_active ? "yes(+)" : "no(-)",
  266.             THREAD->debug_stop ? "yes(-)" : "no(+)");
  267.         spinlock_unlock(&THREAD->debug_lock);
  268.         interrupts_restore(ipl);
  269.         return;
  270.     }
  271.  
  272.     klog_printf("- trigger event");
  273.  
  274.     call = THREAD->debug_go_call;
  275.     IPC_SET_RETVAL(call->data, 0);
  276.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
  277.     IPC_SET_ARG2(call->data, (unative_t)t);
  278.  
  279.     /*
  280.      * Make sure debug_stop is true when going to sleep
  281.      * in case we get woken up by DEBUG_END. (At which
  282.      * point it must be back to the initial true value).
  283.      */
  284.     THREAD->debug_stop = true;
  285.  
  286.     THREAD->cur_event = UDEBUG_EVENT_THREAD_B;
  287.     spinlock_unlock(&THREAD->debug_lock);
  288.  
  289.     spinlock_lock(&TASK->lock);
  290.     ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
  291.     spinlock_unlock(&TASK->lock);
  292.  
  293.     interrupts_restore(ipl);
  294.     klog_printf("- sleep");
  295.     udebug_wait_for_go(&THREAD->go_wq);
  296. }
  297.  
  298. void udebug_thread_e_event(void)
  299. {
  300.     call_t *call;
  301.     ipl_t ipl;
  302.  
  303.     ipl = interrupts_disable();
  304.     spinlock_lock(&THREAD->debug_lock);
  305.  
  306.     klog_printf("udebug_thread_e_event");
  307.     klog_printf("- check state");
  308.  
  309.     /* Must only generate events when in debugging session */
  310.     if (THREAD->debug_active != true) {
  311.         klog_printf("- debug_active: %s, debug_stop: %s",
  312.             THREAD->debug_active ? "yes(+)" : "no(-)",
  313.             THREAD->debug_stop ? "yes(-)" : "no(+)");
  314.         spinlock_unlock(&THREAD->debug_lock);
  315.         interrupts_restore(ipl);
  316.         return;
  317.     }
  318.  
  319.     klog_printf("- trigger event");
  320.  
  321.     call = THREAD->debug_go_call;
  322.     IPC_SET_RETVAL(call->data, 0);
  323.     IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
  324.  
  325.     /* Prevent any further debug activity in thread */
  326.     THREAD->debug_active = false;
  327.     THREAD->cur_event = 0;      /* none */
  328.     THREAD->debug_stop = true;  /* set to initial value */
  329.     spinlock_unlock(&THREAD->debug_lock);
  330.  
  331.     spinlock_lock(&TASK->lock);
  332.     ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
  333.     spinlock_unlock(&TASK->lock);
  334.  
  335.     interrupts_restore(ipl);
  336.  
  337.     /* This event does not sleep - debugging has finished in this thread */
  338. }
  339.  
  340. static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
  341. {
  342.     call_t *call;
  343.     ipl_t ipl;
  344.  
  345.     ipl = interrupts_disable();
  346.     spinlock_lock(&THREAD->debug_lock);
  347.  
  348.     /* Must only generate events when in debugging session and have go */
  349.     if (THREAD->debug_active != true ||
  350.         THREAD->debug_stop == true ||
  351.         (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
  352.         spinlock_unlock(&THREAD->debug_lock);
  353.         interrupts_restore(ipl);
  354.         return;
  355.     }
  356.  
  357.     klog_printf("udebug_breakpoint/trap_event");
  358.     call = THREAD->debug_go_call;
  359.     IPC_SET_RETVAL(call->data, 0);
  360.     IPC_SET_ARG1(call->data, etype);
  361.     IPC_SET_ARG2(call->data, addr);
  362.  
  363.     /*
  364.      * Make sure debug_stop is true when going to sleep
  365.      * in case we get woken up by DEBUG_END. (At which
  366.      * point it must be back to the initial true value).
  367.      */
  368.     THREAD->debug_stop = true;
  369.  
  370.     THREAD->cur_event = etype;
  371.     spinlock_unlock(&THREAD->debug_lock);
  372.     klog_printf("- send answer");
  373.  
  374.     spinlock_lock(&TASK->lock);
  375.     ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
  376.     spinlock_unlock(&TASK->lock);
  377.     interrupts_restore(ipl);
  378.  
  379.     udebug_wait_for_go(&THREAD->go_wq);
  380. }
  381.  
  382. void udebug_breakpoint_event(uintptr_t addr)
  383. {
  384.     breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
  385. }
  386.  
  387. void udebug_trap_event(uintptr_t addr)
  388. {
  389.     breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
  390. }
  391.  
  392. /**
  393.  * Terminate task debugging session.
  394.  *
  395.  * \param ta Must be already locked and interrupts must be disabled.
  396.  * \return Zero on success or negative error code.
  397.  */
  398. int udebug_task_cleanup(struct task *ta)
  399. {
  400.     thread_t *t;
  401.     link_t *cur;
  402.     int flags;
  403.  
  404.     klog_printf("udebug_task_cleanup()");
  405.     klog_printf("task %llu", ta->taskid);
  406.  
  407.     if (ta->udebug.dt_state == UDEBUG_TS_BEGINNING &&
  408.         ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
  409.         klog_printf("udebug_task_cleanup(): task not being debugged");
  410.         return EINVAL;
  411.     }
  412.  
  413.     /* Finish debugging of all userspace threads */
  414.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  415.         t = list_get_instance(cur, thread_t, th_link);
  416.  
  417.         spinlock_lock(&t->debug_lock);
  418.         spinlock_lock(&t->lock);
  419.  
  420.         flags = t->flags;
  421.  
  422.         spinlock_unlock(&t->lock);
  423.  
  424.         /* Only process userspace threads */
  425.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  426.             /* Prevent any further debug activity in thread */
  427.             t->debug_active = false;
  428.             t->cur_event = 0;   /* none */
  429.  
  430.             /* Still has go? */
  431.             if (t->debug_stop == false) {
  432.                 /*
  433.                 * Yes, so clear go. As debug_active == false,
  434.                  * this doesn't affect anything.
  435.                  */
  436.                 t->debug_stop = true;  
  437.  
  438.                 /* Answer GO call */
  439.                 klog_printf("answer GO call with EVENT_FINISHED");
  440.                 IPC_SET_RETVAL(t->debug_go_call->data, 0);
  441.                 IPC_SET_ARG1(t->debug_go_call->data, UDEBUG_EVENT_FINISHED);
  442.                 ipc_answer(&ta->answerbox, t->debug_go_call);
  443.             } else {
  444.                 /*
  445.                  * Debug_stop is already at initial value.
  446.                  * Yet this means the thread needs waking up.
  447.                  */
  448.  
  449.                 /*
  450.                  * t's lock must not be held when calling
  451.                  * waitq_wakeup.
  452.                  */
  453.                 waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  454.             }
  455.         }
  456.         spinlock_unlock(&t->debug_lock);
  457.     }
  458.  
  459.     ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
  460.     ta->udebug.debugger = NULL;
  461.  
  462.     return 0;
  463. }
  464.  
  465.  
  466. /** @}
  467.  */
  468.