Subversion Repositories HelenOS

Rev

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