Subversion Repositories HelenOS

Rev

Rev 2815 | Rev 2817 | 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. static task_t *get_lock_callee_task(phone_t *phone)
  21. {
  22.     answerbox_t *answerbox;
  23.     task_t *ta;
  24.  
  25.     // FIXME: locking!!!
  26.  
  27.     answerbox = phone->callee;
  28.     ta = answerbox->task;
  29.  
  30.     spinlock_lock(&ta->lock);
  31.  
  32.     return ta;
  33. }
  34.  
  35. static int udebug_rp_begin(call_t *call, phone_t *phone)
  36. {
  37.     task_t *ta;
  38.  
  39.     klog_printf("debug_begin()");
  40.  
  41.     ta = get_lock_callee_task(phone);
  42.     klog_printf("debugging task %llu", ta->taskid);
  43.  
  44.     if (ta->being_debugged != false) {
  45.         spinlock_unlock(&ta->lock);
  46.         klog_printf("debug_begin(): busy error");
  47.         return EBUSY;
  48.     }
  49.  
  50.     ta->being_debugged = true;
  51.     ta->stop_request = true;
  52.     ta->debug_begin_call = call;
  53.  
  54.     if (ta->not_stoppable_count == 0) {
  55.         ta->debug_begin_call = NULL;
  56.         ta->stop_request = false;
  57.         spinlock_unlock(&ta->lock);
  58.         klog_printf("debug_begin(): immediate backsend");
  59.         return 1; /* actually we need backsend with 0 retval */
  60.     }
  61.  
  62.     spinlock_unlock(&ta->lock);
  63.  
  64.     klog_printf("debug_begin() done (wait for stoppability)");
  65.     return 0;
  66. }
  67.  
  68. static int udebug_rp_go(call_t *call, phone_t *phone)
  69. {
  70.     thread_t *t;
  71.     task_t *ta;
  72.  
  73.     klog_printf("debug_go()");
  74.     ta = get_lock_callee_task(phone);
  75.  
  76.     // FIXME: must save this in thread struct, not task struct!!!
  77.     ta->debug_go_call = call;
  78.     t = (thread_t *) IPC_GET_ARG2(call->data);
  79.     if (!thread_exists(t)) {
  80.         spinlock_unlock(&ta->lock);
  81.         return ENOENT;
  82.     }
  83.  
  84.     klog_printf("debug_go(): waitq_wakeup");
  85.     waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  86.  
  87.     spinlock_unlock(&ta->lock);
  88.  
  89.     return 0; /* no backsend */
  90. }
  91.  
  92. static int udebug_rp_args_read(call_t *call, phone_t *phone)
  93. {
  94.     thread_t *t;
  95.     task_t *ta;
  96.     void *uspace_buffer;
  97.     unative_t to_copy;
  98.     int rc;
  99.  
  100.     klog_printf("debug_args_read()");
  101.     // FIXME: verify task/thread state
  102.  
  103.     ta = get_lock_callee_task(phone);
  104.     klog_printf("task %llu", ta->taskid);
  105.     t = (thread_t *) IPC_GET_ARG2(call->data);
  106.     if (!thread_exists(t)) {
  107.         spinlock_unlock(&ta->lock);
  108.         return ENOENT;
  109.     }
  110.  
  111.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  112.     to_copy = IPC_GET_ARG4(call->data);
  113.     if (to_copy > 6 * sizeof(unative_t)) to_copy = 6 * sizeof(unative_t);
  114.  
  115.     rc = copy_to_uspace(uspace_buffer, t->syscall_args, to_copy);
  116.     if (rc != 0) {
  117.         spinlock_unlock(&ta->lock);
  118.         klog_printf("debug_args_read() - copy failed");
  119.         return rc;
  120.     }
  121.  
  122.     spinlock_unlock(&ta->lock);
  123.  
  124.     IPC_SET_ARG1(call->data, to_copy);
  125.  
  126.     klog_printf("debug_args_read() done");
  127.     return 1; /* actually need becksend with retval 0 */
  128. }
  129.  
  130. static int udebug_rp_thread_read(call_t *call, phone_t *phone)
  131. {
  132.     thread_t *t;
  133.     link_t *cur;
  134.     task_t *ta;
  135.     unative_t *uspace_buffer;
  136.     unative_t to_copy;
  137.     int rc;
  138.     unsigned copied, total;
  139.     unsigned buf_size;
  140.     unative_t tid;
  141.  
  142.     klog_printf("debug_thread_read()");
  143.     // FIXME: verify task/thread state
  144.  
  145.     ta = get_lock_callee_task(phone);
  146.     klog_printf("task %llu", ta->taskid);
  147.    
  148.     uspace_buffer = (void *)IPC_GET_ARG2(call->data);
  149.     buf_size = IPC_GET_ARG3(call->data);
  150.  
  151.     copied = total = 0;
  152.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  153.         t = list_get_instance(cur, thread_t, th_link);
  154.  
  155.         /* Not interested in kernel threads */
  156.         if ((t->flags & THREAD_FLAG_USPACE) == 0)
  157.             continue;
  158.  
  159.         /* Using thread struct pointer for identification */
  160.         tid = (unative_t) t;
  161.  
  162.         to_copy = sizeof(unative_t);
  163.         if (copied + to_copy >= buf_size)
  164.             to_copy = buf_size - copied;
  165.  
  166.         if (to_copy > 0) {
  167.             rc = copy_to_uspace(uspace_buffer, &tid, to_copy);
  168.             if (rc != 0) {
  169.                 spinlock_unlock(&ta->lock);
  170.                 klog_printf("debug_thread_read() - copy failed");
  171.                 return rc;
  172.             }
  173.         }
  174.  
  175.         ++uspace_buffer;
  176.         total += sizeof(unative_t);
  177.         copied += to_copy;
  178.     }
  179.  
  180.     spinlock_unlock(&ta->lock);
  181.  
  182.     IPC_SET_ARG1(call->data, copied);
  183.     IPC_SET_ARG2(call->data, total);
  184.  
  185.     klog_printf("debug_thread_read() done");
  186.     return 1; /* actually need becksend with retval 0 */
  187. }
  188.  
  189. int udebug_request_preprocess(call_t *call, phone_t *phone)
  190. {
  191.     int rc;
  192.  
  193.     switch (IPC_GET_ARG1(call->data)) {
  194.     case UDEBUG_M_BEGIN:
  195.         rc = udebug_rp_begin(call, phone);
  196.         return rc;
  197.     case UDEBUG_M_GO:
  198.         rc = udebug_rp_go(call, phone);
  199.         return rc;
  200.     case UDEBUG_M_ARGS_READ:
  201.         rc = udebug_rp_args_read(call, phone);
  202.         return rc;
  203.     case UDEBUG_M_THREAD_READ:
  204.         rc = udebug_rp_thread_read(call, phone);
  205.         return rc;
  206.     default:
  207.         break;
  208.     }
  209.  
  210.     return 0;
  211. }
  212.  
  213. static void udebug_receive_mem_read(call_t *call)
  214. {
  215.     unative_t uspace_dst;
  216.     void *uspace_ptr;
  217.     unsigned size;
  218.     void *buffer;
  219.     int rc;
  220.  
  221.     klog_printf("debug_mem_read()");
  222.     uspace_dst = IPC_GET_ARG2(call->data);
  223.     uspace_ptr = (void *)IPC_GET_ARG3(call->data);
  224.     size = IPC_GET_ARG4(call->data);
  225.  
  226.     buffer = malloc(size, 0); // ???
  227.     klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size);
  228.  
  229.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  230.      * be a problem */
  231.     rc = copy_from_uspace(buffer, uspace_ptr, size);
  232.     if (rc) {
  233.         IPC_SET_RETVAL(call->data, rc);
  234.         return;
  235.     }
  236.  
  237.     klog_printf("first word: %u", *((unative_t *)buffer));
  238.  
  239.     IPC_SET_RETVAL(call->data, 0);
  240.     /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  241.        same code in process_answer() can be used
  242.        (no way to distinguish method in answer) */
  243.     IPC_SET_ARG1(call->data, uspace_dst);
  244.     IPC_SET_ARG2(call->data, size);
  245.     call->buffer = buffer;
  246.  
  247.     ipc_answer(&TASK->kernel_box, call);
  248. }
  249.  
  250. /**
  251.  * Handle a debug call received on the kernel answerbox.
  252.  *
  253.  * This is called by the kbox servicing thread.
  254.  */
  255. void udebug_call_receive(call_t *call)
  256. {
  257.     int debug_method;
  258.  
  259.     debug_method = IPC_GET_ARG1(call->data);
  260.  
  261.     switch (debug_method) {
  262.     case UDEBUG_M_MEM_READ:
  263.         udebug_receive_mem_read(call);
  264.         break;
  265.     }
  266. }
  267.  
  268. /** @}
  269.  */
  270.