Subversion Repositories HelenOS

Rev

Rev 2815 | 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 <errno.h>
  14. #include <ipc/ipc.h>
  15. #include <syscall/copy.h>
  16. #include <udebug/udebug.h>
  17. #include <udebug/udebug_ipc.h>
  18.  
  19. static task_t *get_lock_callee_task(phone_t *phone)
  20. {
  21.     answerbox_t *answerbox;
  22.     task_t *ta;
  23.  
  24.     // FIXME: locking!!!
  25.  
  26.     answerbox = phone->callee;
  27.     ta = answerbox->task;
  28.  
  29.     spinlock_lock(&ta->lock);
  30.  
  31.     return ta;
  32. }
  33.  
  34. static thread_t *get_task_thread_by_id(task_t *ta, thread_id_t tid)
  35. {
  36.     thread_t *t;
  37.     link_t *cur;
  38.  
  39.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  40.         t = list_get_instance(cur, thread_t, th_link);     
  41.         if (tid == t->tid) return t;
  42.     }
  43.  
  44.     return NULL;
  45. }
  46.  
  47. static int udebug_rp_begin(call_t *call, phone_t *phone)
  48. {
  49.     task_t *ta;
  50.  
  51.     klog_printf("debug_begin()");
  52.  
  53.     ta = get_lock_callee_task(phone);
  54.     klog_printf("debugging task %llu", ta->taskid);
  55.  
  56.     if (ta->being_debugged != false) {
  57.         spinlock_unlock(&ta->lock);
  58.         klog_printf("debug_begin(): busy error");
  59.         return EBUSY;
  60.     }
  61.  
  62.     ta->being_debugged = true;
  63.     ta->stop_request = true;
  64.     ta->debug_begin_call = call;
  65.  
  66.     if (ta->not_stoppable_count == 0) {
  67.         ta->debug_begin_call = NULL;
  68.         ta->stop_request = false;
  69.         spinlock_unlock(&ta->lock);
  70.         klog_printf("debug_begin(): immediate backsend");
  71.         return 1; /* actually we need backsend with 0 retval */
  72.     }
  73.  
  74.     spinlock_unlock(&ta->lock);
  75.  
  76.     klog_printf("debug_begin() done (wait for stoppability)");
  77.     return 0;
  78. }
  79.  
  80. static int udebug_rp_go(call_t *call, phone_t *phone)
  81. {
  82.     thread_t *t;
  83.     task_t *ta;
  84.  
  85.     klog_printf("debug_go()");
  86.     ta = get_lock_callee_task(phone);
  87.  
  88.     ta->debug_go_call = call;
  89.     t = get_task_thread_by_id(ta, IPC_GET_ARG2(call->data));
  90.     if (t == NULL) {
  91.         spinlock_unlock(&ta->lock);
  92.         return ENOENT;
  93.     }
  94.  
  95.     klog_printf("debug_go(): waitq_wakeup");
  96.     waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  97.  
  98.     spinlock_unlock(&ta->lock);
  99.  
  100.     return 0; /* no backsend */
  101. }
  102.  
  103. static int udebug_rp_args_read(call_t *call, phone_t *phone)
  104. {
  105.     thread_t *t;
  106.     task_t *ta;
  107.     void *uspace_buffer;
  108.     unative_t to_copy;
  109.     int rc;
  110.  
  111.     klog_printf("debug_args_read()");
  112.     // FIXME: verify task/thread state
  113.  
  114.     ta = get_lock_callee_task(phone);
  115.     klog_printf("task %llu", ta->taskid);
  116.     t = get_task_thread_by_id(ta, IPC_GET_ARG2(call->data));
  117.     if (t == NULL) {
  118.         spinlock_unlock(&ta->lock);
  119.         return ENOENT;
  120.     }
  121.  
  122.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  123.     to_copy = IPC_GET_ARG4(call->data);
  124.     if (to_copy > 6 * sizeof(unative_t)) to_copy = 6 * sizeof(unative_t);
  125.  
  126.     rc = copy_to_uspace(uspace_buffer, t->syscall_args, to_copy);
  127.     if (rc != 0) {
  128.         spinlock_unlock(&ta->lock);
  129.         klog_printf("debug_args_read() - copy failed");
  130.         return rc;
  131.     }
  132.  
  133.     spinlock_unlock(&ta->lock);
  134.  
  135.     IPC_SET_ARG1(call->data, to_copy);
  136.  
  137.     klog_printf("debug_args_read() done");
  138.     return 1; /* actually need becksend with retval 0 */
  139. }
  140.  
  141. static int udebug_rp_thread_read(call_t *call, phone_t *phone)
  142. {
  143.     thread_t *t;
  144.     link_t *cur;
  145.     task_t *ta;
  146.     unative_t *uspace_buffer;
  147.     unative_t to_copy;
  148.     int rc;
  149.     unsigned copied, total;
  150.     unsigned buf_size;
  151.     unative_t tid;
  152.  
  153.     klog_printf("debug_thread_read()");
  154.     // FIXME: verify task/thread state
  155.  
  156.     ta = get_lock_callee_task(phone);
  157.     klog_printf("task %llu", ta->taskid);
  158.    
  159.     uspace_buffer = (void *)IPC_GET_ARG2(call->data);
  160.     buf_size = IPC_GET_ARG3(call->data);
  161.  
  162.     copied = total = 0;
  163.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  164.         t = list_get_instance(cur, thread_t, th_link);
  165.  
  166.         /* Not interested in kernel threads */
  167.         if ((t->flags & THREAD_FLAG_USPACE) == 0)
  168.             continue;
  169.  
  170.         //FIXME: id cropped!!
  171.         tid = (unative_t) t->tid;
  172.  
  173.         to_copy = sizeof(unative_t);
  174.         if (copied + to_copy >= buf_size)
  175.             to_copy = buf_size - copied;
  176.  
  177.         if (to_copy > 0) {
  178.             rc = copy_to_uspace(uspace_buffer, &tid, to_copy);
  179.             if (rc != 0) {
  180.                 spinlock_unlock(&ta->lock);
  181.                 klog_printf("debug_thread_read() - copy failed");
  182.                 return rc;
  183.             }
  184.         }
  185.  
  186.         ++uspace_buffer;
  187.         total += sizeof(unative_t);
  188.         copied += to_copy;
  189.     }
  190.  
  191.     spinlock_unlock(&ta->lock);
  192.  
  193.     IPC_SET_ARG1(call->data, copied);
  194.     IPC_SET_ARG2(call->data, total);
  195.  
  196.     klog_printf("debug_thread_read() done");
  197.     return 1; /* actually need becksend with retval 0 */
  198. }
  199.  
  200. int udebug_request_preprocess(call_t *call, phone_t *phone)
  201. {
  202.     int rc;
  203.  
  204.     switch (IPC_GET_ARG1(call->data)) {
  205.     case UDEBUG_M_BEGIN:
  206.         rc = udebug_rp_begin(call, phone);
  207.         return rc;
  208.     case UDEBUG_M_GO:
  209.         rc = udebug_rp_go(call, phone);
  210.         return rc;
  211.     case UDEBUG_M_ARGS_READ:
  212.         rc = udebug_rp_args_read(call, phone);
  213.         return rc;
  214.     case UDEBUG_M_THREAD_READ:
  215.         rc = udebug_rp_thread_read(call, phone);
  216.         return rc;
  217.     default:
  218.         break;
  219.     }
  220.  
  221.     return 0;
  222. }
  223.  
  224.  
  225. /** @}
  226.  */
  227.