Subversion Repositories HelenOS

Rev

Rev 2838 | Rev 2842 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /** @addtogroup generic
  2.  * @{
  3.  */
  4.  
  5. /**
  6.  * @file
  7.  * @brief   Tdebug.
  8.  */
  9.  
  10. #include <console/klog.h>
  11. #include <proc/task.h>
  12. #include <proc/thread.h>
  13. #include <arch.h>
  14. #include <errno.h>
  15. #include <ipc/ipc.h>
  16. #include <syscall/copy.h>
  17. #include <udebug/udebug.h>
  18. #include <udebug/udebug_ipc.h>
  19.  
  20. /**
  21.  * Get and lock a phone's callee task.
  22.  *
  23.  * This will return a pointer to the task to which the phone
  24.  * is connected. It will lock the task, making sure it exists.
  25.  * (TODO: make sure the udebug-cleanup of the task hasn't
  26.  * started yet)
  27.  */
  28. static task_t *get_lock_callee_task(phone_t *phone)
  29. {
  30.     answerbox_t *box;
  31.     task_t *ta;
  32.     task_id_t taskid;
  33.     ipl_t ipl;
  34.  
  35.     ipl = interrupts_disable();
  36.     spinlock_lock(&phone->lock);
  37.     if (phone->state != IPC_PHONE_CONNECTED) {
  38.         spinlock_unlock(&phone->lock);
  39.         interrupts_restore(ipl);
  40.         return NULL;
  41.     }
  42.  
  43.     box = phone->callee;
  44.    
  45.     spinlock_lock(&box->lock);
  46.     ta = box->task;
  47.     taskid = ta->taskid;
  48.     spinlock_unlock(&box->lock);
  49.     spinlock_unlock(&phone->lock);
  50.  
  51.     /* Locking decoupled using taskid */
  52.    
  53.     spinlock_lock(&tasks_lock);
  54.     ta = task_find_by_id(taskid);
  55.     if (ta == NULL) {
  56.         spinlock_unlock(&tasks_lock);
  57.         interrupts_restore(ipl);
  58.         return NULL;
  59.     }
  60.  
  61.     spinlock_lock(&ta->lock);
  62.     spinlock_unlock(&tasks_lock);
  63.     interrupts_restore(ipl);
  64.  
  65.     return ta;
  66. }
  67.  
  68. static int verify_thread(thread_t *t)
  69. {
  70.     /* Verify that 't' exists and belongs to task 'ta' */
  71.     if (!thread_exists(t) || (t->task != ta)) {
  72.         spinlock_unlock(&threads_lock);
  73.         interrupts_restore(ipl);
  74.         return ENOENT;
  75.     }
  76.  
  77.     /* Verify that 't' is a userspace thread */
  78.     if ((t->flags & THREAD_FLAG_USPACE) == 0) {
  79.         /* It's not, deny its existence */
  80.         return ENOENT;
  81.     }
  82.  
  83.     if ((t->debug_active != true) || (t->debug_stop != true)) {
  84.         /* Not in debugging session or already has GO */
  85.         spinlock_unlock(&threads_lock);
  86.         interrupts_restore(ipl);       
  87.         return EBUSY;
  88.     }
  89.  
  90.     return EOK;
  91. }
  92.  
  93. static int udebug_rp_begin(call_t *call, phone_t *phone)
  94. {
  95.     task_t *ta;
  96.     ipl_t ipl;
  97.     int rc;
  98.  
  99.     thread_t *t;
  100.     link_t *cur;
  101.  
  102.     klog_printf("debug_begin()");
  103.  
  104.     ipl = interrupts_disable();
  105.     ta = get_lock_callee_task(phone);
  106.     klog_printf("debugging task %llu", ta->taskid);
  107.  
  108.     if (ta->dt_state != UDEBUG_TS_INACTIVE) {
  109.         spinlock_unlock(&ta->lock);
  110.         interrupts_restore(ipl);
  111.         klog_printf("debug_begin(): busy error");
  112.         return EBUSY;
  113.     }
  114.  
  115.     ta->dt_state = UDEBUG_TS_BEGINNING;
  116.     ta->debug_begin_call = call;
  117.  
  118.     if (ta->not_stoppable_count == 0) {
  119.         ta->dt_state = UDEBUG_TS_ACTIVE;
  120.         ta->debug_begin_call = NULL;
  121.         rc = 1; /* actually we need backsend with 0 retval */
  122.     } else {
  123.         rc = 0; /* no backsend */
  124.     }
  125.    
  126.     /* Set debug_active on all of the task's userspace threads */
  127.  
  128.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  129.         t = list_get_instance(cur, thread_t, th_link);
  130.  
  131.         spinlock_lock(&t->lock);
  132.         if ((t->flags & THREAD_FLAG_USPACE) != 0)
  133.             t->debug_active = true;
  134.         spinlock_unlock(&t->lock);
  135.     }
  136.  
  137.     spinlock_unlock(&ta->lock);
  138.     interrupts_restore(ipl);
  139.  
  140.     klog_printf("debug_begin() done (%s)",
  141.         rc ? "backsend" : "stoppability wait");
  142.  
  143.     return rc;
  144. }
  145.  
  146. static int udebug_rp_end(call_t *call, phone_t *phone)
  147. {
  148.     task_t *ta;
  149.     ipl_t ipl;
  150.  
  151.     thread_t *t;
  152.     link_t *cur;
  153.  
  154.     klog_printf("udebug_rp_end()");
  155.  
  156.     ipl = interrupts_disable();
  157.     ta = get_lock_callee_task(phone);
  158.     klog_printf("task %llu", ta->taskid);
  159.  
  160.     if (ta->dt_state == UDEBUG_TS_BEGINNING &&
  161.         ta->dt_state != UDEBUG_TS_ACTIVE) {
  162.         spinlock_unlock(&ta->lock);
  163.         interrupts_restore(ipl);
  164.         klog_printf("udebug_rp_begin(): task not being debugged");
  165.         return EINVAL;
  166.     }
  167.  
  168.     /* Finish debugging of all userspace threads */
  169.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  170.         t = list_get_instance(cur, thread_t, th_link);
  171.  
  172.         spinlock_lock(&t->lock);
  173.  
  174.         /* Only process userspace threads */
  175.         if ((t->flags & THREAD_FLAG_USPACE) != 0) {
  176.             /* Prevent any further debug activity in thread */
  177.             t->debug_active = false;
  178.  
  179.             /* Still has go? */
  180.             if (t->debug_stop == false) {
  181.                 /*
  182.                 * Yes, so clear go. As debug_active == false,
  183.                  * this doesn't affect anything.
  184.                  */
  185.                 t->debug_stop = true;  
  186.  
  187.                 /* Answer GO call */
  188.                 klog_printf("answer GO call with EVENT_FINISHED");
  189.                 IPC_SET_RETVAL(t->debug_go_call->data, 0);
  190.                 IPC_SET_ARG1(t->debug_go_call->data, UDEBUG_EVENT_FINISHED);
  191.                 ipc_answer(&ta->answerbox, t->debug_go_call);
  192.             } else {
  193.                 /*
  194.                  * Debug_stop is already at initial value.
  195.                  * Yet this means the thread needs waking up.
  196.                  */
  197.                 waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  198.             }
  199.         }
  200.  
  201.         spinlock_unlock(&t->lock);
  202.     }
  203.  
  204.     ta->dt_state = UDEBUG_TS_INACTIVE;
  205.  
  206.     spinlock_unlock(&ta->lock);
  207.     interrupts_restore(ipl);
  208.  
  209.     IPC_SET_RETVAL(call->data, 0);
  210.  
  211.     klog_printf("udebug_rp_end() done\n");
  212.  
  213.     return 1;
  214. }
  215.  
  216.  
  217. static int udebug_rp_go(call_t *call, phone_t *phone)
  218. {
  219.     thread_t *t;
  220.     task_t *ta;
  221.     ipl_t ipl;
  222.  
  223.     klog_printf("debug_go()");
  224.     ta = get_lock_callee_task(phone);
  225.     spinlock_unlock(&ta->lock);
  226.     // TODO: don't lock ta
  227.  
  228.     t = (thread_t *) IPC_GET_ARG2(call->data);
  229.  
  230.     ipl = interrupts_disable();
  231.     spinlock_lock(&threads_lock);
  232.  
  233.  
  234.     t->debug_go_call = call;
  235.     t->debug_stop = false;
  236.     waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  237.  
  238.     spinlock_unlock(&threads_lock);
  239.     interrupts_restore(ipl);
  240.  
  241.     return 0; /* no backsend */
  242. }
  243.  
  244. static int udebug_rp_args_read(call_t *call, phone_t *phone)
  245. {
  246.     thread_t *t;
  247.     task_t *ta;
  248.     void *uspace_buffer;
  249.     int rc;
  250.     ipl_t ipl;
  251.     unative_t buffer[6];
  252.  
  253.     klog_printf("debug_args_read()");
  254.  
  255.     ta = get_lock_callee_task(phone);
  256.     klog_printf("task %llu", ta->taskid);
  257.     spinlock_unlock(&ta->lock);
  258.  
  259.     t = (thread_t *) IPC_GET_ARG2(call->data);
  260.  
  261.     ipl = interrupts_disable();
  262.     spinlock_lock(&threads_lock);
  263.  
  264.     /* Verify that thread t exists and may be operated on */
  265.     rc = verify_thread(t);
  266.     if (rc != EOK) {
  267.         spinlock_unlock(&threads_lock);
  268.         interrupts_restore(ipl);
  269.         return rc;
  270.     }
  271.  
  272.     //FIXME: additionally we need to verify that we are inside a syscall
  273.  
  274.     /* Copy to a local buffer before releasing the lock */
  275.     memcpy(buffer, t->syscall_args, 6 * sizeof(unative_t));
  276.  
  277.     spinlock_unlock(&threads_lock);
  278.     interrupts_restore(ipl);
  279.  
  280.     /* Now copy to userspace */
  281.  
  282.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  283.  
  284.     rc = copy_to_uspace(uspace_buffer, buffer, 6 * sizeof(unative_t));
  285.     if (rc != 0) {
  286.         spinlock_unlock(&ta->lock);
  287.         klog_printf("debug_args_read() - copy failed");
  288.         return rc;
  289.     }
  290.  
  291.     klog_printf("debug_args_read() done");
  292.     return 1; /* actually need becksend with retval 0 */
  293. }
  294.  
  295. static int udebug_rp_regs_read(call_t *call, phone_t *phone)
  296. {
  297.     thread_t *t;
  298.     task_t *ta;
  299.     void *uspace_buffer;
  300.     unative_t to_copy;
  301.     int rc;
  302.     istate_t *state;
  303.     istate_t state_copy;
  304.     ipl_t ipl;
  305.  
  306.     klog_printf("debug_regs_read()");
  307.  
  308.     ta = get_lock_callee_task(phone);
  309.     spinlock_unlock(&ta->lock);
  310.     //FIXME: don't lock ta
  311.  
  312.     ipl = interrupts_disable();
  313.     spinlock_lock(&threads_lock);
  314.  
  315.     t = (thread_t *) IPC_GET_ARG2(call->data);
  316.  
  317.     /* Verify that thread t exists and may be operated on */
  318.     rc = verify_thread(t);
  319.     if (rc != EOK) {
  320.         spinlock_unlock(&threads_lock);
  321.         interrupts_restore(ipl);
  322.         return rc;
  323.     }
  324.  
  325.     state = t->uspace_state;
  326.     if (state == NULL) {
  327.         spinlock_unlock(&threads_lock);
  328.         interrupts_restore(ipl);
  329.         klog_printf("debug_regs_read() - istate not available");
  330.         return EBUSY;
  331.     }
  332.  
  333.     /* Copy to a local buffer so that we can release the lock */
  334.     memcpy(&state_copy, state, sizeof(state_copy));
  335.     spinlock_unlock(&threads_lock);
  336.     interrupts_restore(ipl);
  337.  
  338.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  339.     to_copy = IPC_GET_ARG4(call->data);
  340.     if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
  341.  
  342.     rc = copy_to_uspace(uspace_buffer, &state_copy, to_copy);
  343.     if (rc != 0) {
  344.         spinlock_unlock(&ta->lock);
  345.         klog_printf("debug_regs_read() - copy failed");
  346.         return rc;
  347.     }
  348.  
  349.     IPC_SET_ARG1(call->data, to_copy);
  350.     IPC_SET_ARG2(call->data, sizeof(istate_t));
  351.  
  352.     klog_printf("debug_regs_read() done");
  353.     return 1; /* actually need becksend with retval 0 */
  354. }
  355.  
  356. static int udebug_rp_regs_write(call_t *call, phone_t *phone)
  357. {
  358.     thread_t *t;
  359.     task_t *ta;
  360.     void *uspace_data;
  361.     unative_t to_copy;
  362.     int rc;
  363.     istate_t *state;
  364.     istate_t data_copy;
  365.     ipl_t ipl;
  366.  
  367.     klog_printf("debug_regs_write()");
  368.  
  369.     /* First copy to a local buffer */
  370.  
  371.     uspace_data = (void *)IPC_GET_ARG3(call->data);
  372.     to_copy = IPC_GET_ARG4(call->data);
  373.     if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
  374.  
  375.     rc = copy_from_uspace(&data_copy, uspace_data, to_copy);
  376.     if (rc != 0) {
  377.         klog_printf("debug_regs_write() - copy failed");
  378.         return rc;
  379.     }
  380.  
  381.     ta = get_lock_callee_task(phone);
  382.     spinlock_unlock(&ta->lock);
  383.     //FIXME: don't lock ta
  384.  
  385.     /* Now try to change the thread's uspace_state */
  386.  
  387.     ipl = interrupts_disable();
  388.     spinlock_lock(&threads_lock);
  389.  
  390.     t = (thread_t *) IPC_GET_ARG2(call->data);
  391.  
  392.     /* Verify that thread t exists and may be operated on */
  393.     rc = verify_thread(t);
  394.     if (rc != EOK) {
  395.         spinlock_unlock(&threads_lock);
  396.         interrupts_restore(ipl);
  397.         return rc;
  398.     }
  399.  
  400.     state = t->uspace_state;
  401.     if (state == NULL) {
  402.         spinlock_unlock(&threads_lock);
  403.         interrupts_restore(ipl);
  404.         klog_printf("debug_regs_write() - istate not available");
  405.         return EBUSY;
  406.     }
  407.  
  408.     memcpy(t->uspace_state, &data_copy, sizeof(t->uspace_state));
  409.  
  410.     spinlock_unlock(&threads_lock);
  411.     interrupts_restore(ipl);
  412.  
  413.     /* Set answer values */
  414.  
  415.     IPC_SET_ARG1(call->data, to_copy);
  416.     IPC_SET_ARG2(call->data, sizeof(istate_t));
  417.  
  418.     klog_printf("debug_regs_write() done");
  419.     return 1; /* actually need becksend with retval 0 */
  420. }
  421.  
  422. static int udebug_rp_thread_read(call_t *call, phone_t *phone)
  423. {
  424.     thread_t *t;
  425.     link_t *cur;
  426.     task_t *ta;
  427.     unative_t *uspace_buffer;
  428.     unative_t to_copy;
  429.     int rc;
  430.     unsigned total_bytes;
  431.     unsigned buf_size;
  432.     unative_t tid;
  433.     unsigned num_threads, copied_ids;
  434.     ipl_t ipl;
  435.     unative_t *buffer;
  436.     int flags;
  437.  
  438.     klog_printf("debug_thread_read()");
  439.  
  440.     ipl = interrupts_disable();
  441.     ta = get_lock_callee_task(phone);
  442.  
  443.     /* Verify task state */
  444.     if (ta->dt_state != UDEBUG_TS_ACTIVE) {
  445.         spinlock_unlock(&ta->lock);
  446.         interrupts_restore(ipl);
  447.         return EBUSY;
  448.     }
  449.  
  450.     /* Count the threads first */
  451.  
  452.     num_threads = 0;
  453.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  454.         /* Count all threads, to be on the safe side */
  455.         ++num_threads;
  456.     }
  457.  
  458.     /* Allocate a buffer and copy down the threads' ids */
  459.     buffer = malloc(num_threads * sizeof(unative_t), 0); // ???
  460.  
  461.     copied_ids = 0;
  462.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  463.         t = list_get_instance(cur, thread_t, th_link);
  464.  
  465.         spinlock_lock(&t->lock);
  466.         flags = t->flags;
  467.         spinlock_unlock(&t->lock);
  468.  
  469.         /* Not interested in kernel threads */
  470.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  471.             /* Using thread struct pointer for identification */
  472.             tid = (unative_t) t;
  473.             buffer[copied_ids++] = tid;
  474.         }
  475.     }
  476.  
  477.     spinlock_unlock(&ta->lock);
  478.     interrupts_restore(ipl);
  479.  
  480.     /* Now copy to userspace */
  481.  
  482.     uspace_buffer = (void *)IPC_GET_ARG2(call->data);
  483.     buf_size = IPC_GET_ARG3(call->data);
  484.  
  485.     total_bytes = copied_ids * sizeof(unative_t);
  486.  
  487.     if (buf_size > total_bytes)
  488.         to_copy = total_bytes;
  489.     else
  490.         to_copy = buf_size;
  491.  
  492.     rc = copy_to_uspace(uspace_buffer, buffer, to_copy);
  493.     free(buffer);
  494.  
  495.     if (rc != 0) {
  496.         klog_printf("debug_thread_read() - copy failed");
  497.         return rc;
  498.     }
  499.  
  500.     IPC_SET_ARG1(call->data, to_copy);
  501.     IPC_SET_ARG2(call->data, total_bytes);
  502.  
  503.     klog_printf("debug_thread_read() done");
  504.     return 1; /* actually need becksend with retval 0 */
  505. }
  506.  
  507. static int udebug_rp_mem_write(call_t *call, phone_t *phone)
  508. {
  509.     void *uspace_data;
  510.     unative_t to_copy;
  511.     int rc;
  512.     void *buffer;
  513.  
  514.     klog_printf("udebug_rp_mem_write()");
  515.  
  516.     uspace_data = (void *)IPC_GET_ARG2(call->data);
  517.     to_copy = IPC_GET_ARG4(call->data);
  518.  
  519.     buffer = malloc(to_copy, 0); // ???
  520.  
  521.     rc = copy_from_uspace(buffer, uspace_data, to_copy);
  522.     if (rc != 0) {
  523.         klog_printf(" - copy failed");
  524.         return rc;
  525.     }
  526.  
  527.     call->buffer = buffer;
  528.  
  529.     klog_printf(" - done");
  530.     return 1; /* actually need becksend with retval 0 */
  531. }
  532.  
  533.  
  534. int udebug_request_preprocess(call_t *call, phone_t *phone)
  535. {
  536.     int rc;
  537.  
  538.     switch (IPC_GET_ARG1(call->data)) {
  539.     case UDEBUG_M_BEGIN:
  540.         rc = udebug_rp_begin(call, phone);
  541.         return rc;
  542.     case UDEBUG_M_END:
  543.         rc = udebug_rp_end(call, phone);
  544.         return rc;
  545.     case UDEBUG_M_GO:
  546.         rc = udebug_rp_go(call, phone);
  547.         return rc;
  548.     case UDEBUG_M_ARGS_READ:
  549.         rc = udebug_rp_args_read(call, phone);
  550.         return rc;
  551.     case UDEBUG_M_REGS_READ:
  552.         rc = udebug_rp_regs_read(call, phone);
  553.         return rc;
  554.     case UDEBUG_M_REGS_WRITE:
  555.         rc = udebug_rp_regs_write(call, phone);
  556.         return rc;
  557.     case UDEBUG_M_THREAD_READ:
  558.         rc = udebug_rp_thread_read(call, phone);
  559.         return rc;
  560.     case UDEBUG_M_MEM_WRITE:
  561.         rc = udebug_rp_mem_write(call, phone);
  562.         return rc;
  563.     default:
  564.         break;
  565.     }
  566.  
  567.     return 0;
  568. }
  569.  
  570. static void udebug_receive_mem_read(call_t *call)
  571. {
  572.     unative_t uspace_dst;
  573.     void *uspace_ptr;
  574.     unsigned size;
  575.     void *buffer;
  576.     int rc;
  577.  
  578.     klog_printf("debug_mem_read()");
  579.     uspace_dst = IPC_GET_ARG2(call->data);
  580.     uspace_ptr = (void *)IPC_GET_ARG3(call->data);
  581.     size = IPC_GET_ARG4(call->data);
  582.  
  583.     buffer = malloc(size, 0); // ???
  584.     klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size);
  585.  
  586.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  587.      * be a problem */
  588.     rc = copy_from_uspace(buffer, uspace_ptr, size);
  589.     if (rc) {
  590.         IPC_SET_RETVAL(call->data, rc);
  591.         return;
  592.     }
  593.  
  594.     klog_printf("first word: %u", *((unative_t *)buffer));
  595.  
  596.     IPC_SET_RETVAL(call->data, 0);
  597.     /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  598.        same code in process_answer() can be used
  599.        (no way to distinguish method in answer) */
  600.     IPC_SET_ARG1(call->data, uspace_dst);
  601.     IPC_SET_ARG2(call->data, size);
  602.     call->buffer = buffer;
  603.  
  604.     ipc_answer(&TASK->kernel_box, call);
  605. }
  606.  
  607. static void udebug_receive_mem_write(call_t *call)
  608. {
  609.     void *uspace_dst;
  610.     unsigned size;
  611.     void *buffer;
  612.     int rc;
  613.     udebug_task_state_t dts;
  614.  
  615.     klog_printf("udebug_receive_mem_write()");
  616.  
  617.     /* Verify task state */
  618.     spinlock_lock(&TASK->lock);
  619.     dts = TASK->dt_state;
  620.     spinlock_unlock(&TASK->lock);
  621.  
  622.     if (dts != UDEBUG_TS_ACTIVE) {
  623.         IPC_SET_RETVAL(call->data, EBUSY);
  624.         ipc_answer(&TASK->kernel_box, call);
  625.         return;
  626.     }
  627.    
  628.     uspace_dst = (void *)IPC_GET_ARG3(call->data);
  629.     size = IPC_GET_ARG4(call->data);
  630.  
  631.     buffer = call->buffer;
  632.     klog_printf("dst=%u, size=%u", uspace_dst, size);
  633.  
  634.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  635.      * be a problem */
  636.     rc = copy_to_uspace(uspace_dst, buffer, size);
  637.     if (rc) {
  638.         IPC_SET_RETVAL(call->data, rc);
  639.         ipc_answer(&TASK->kernel_box, call);
  640.         return;
  641.     }
  642.  
  643.     IPC_SET_RETVAL(call->data, 0);
  644.  
  645.     free(call->buffer);
  646.     call->buffer = NULL;
  647.  
  648.     ipc_answer(&TASK->kernel_box, call);
  649. }
  650.  
  651.  
  652. /**
  653.  * Handle a debug call received on the kernel answerbox.
  654.  *
  655.  * This is called by the kbox servicing thread.
  656.  */
  657. void udebug_call_receive(call_t *call)
  658. {
  659.     int debug_method;
  660.  
  661.     debug_method = IPC_GET_ARG1(call->data);
  662.  
  663.     switch (debug_method) {
  664.     case UDEBUG_M_MEM_READ:
  665.         udebug_receive_mem_read(call);
  666.         break;
  667.     case UDEBUG_M_MEM_WRITE:
  668.         udebug_receive_mem_write(call);
  669.         break;
  670.     }
  671. }
  672.  
  673. /** @}
  674.  */
  675.