Changeset da1bafb in mainline for kernel/generic/src/udebug/udebug.c


Ignore:
Timestamp:
2010-05-24T18:57:31Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0095368
Parents:
666f492
Message:

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/udebug/udebug.c

    r666f492 rda1bafb  
    3333/**
    3434 * @file
    35  * @brief       Udebug hooks and data structure management.
     35 * @brief Udebug hooks and data structure management.
    3636 *
    3737 * Udebug is an interface that makes userspace debuggers possible.
    3838 */
    39  
     39
    4040#include <synch/waitq.h>
    4141#include <debug.h>
     
    4545#include <arch.h>
    4646
    47 
    4847/** Initialize udebug part of task structure.
    4948 *
    5049 * Called as part of task structure initialization.
    51  * @param ut    Pointer to the structure to initialize.
     50 * @param ut Pointer to the structure to initialize.
     51 *
    5252 */
    5353void udebug_task_init(udebug_task_t *ut)
     
    6363 *
    6464 * Called as part of thread structure initialization.
    65  * @param ut    Pointer to the structure to initialize.
     65 *
     66 * @param ut Pointer to the structure to initialize.
     67 *
    6668 */
    6769void udebug_thread_initialize(udebug_thread_t *ut)
     
    7072        waitq_initialize(&ut->go_wq);
    7173        condvar_initialize(&ut->active_cv);
    72 
     74       
    7375        ut->go_call = NULL;
    7476        ut->uspace_state = NULL;
     
    7678        ut->stoppable = true;
    7779        ut->active = false;
    78         ut->cur_event = 0; /* none */
     80        ut->cur_event = 0;  /* None */
    7981}
    8082
     
    8587 * is received.
    8688 *
    87  * @param wq    The wait queue used by the thread to wait for GO messages.
     89 * @param wq The wait queue used by the thread to wait for GO messages.
     90 *
    8891 */
    8992static void udebug_wait_for_go(waitq_t *wq)
    9093{
    91         int rc;
    92         ipl_t ipl;
    93 
    94         ipl = waitq_sleep_prepare(wq);
    95 
    96         wq->missed_wakeups = 0; /* Enforce blocking. */
    97         rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
    98 
     94        ipl_t ipl = waitq_sleep_prepare(wq);
     95       
     96        wq->missed_wakeups = 0;  /* Enforce blocking. */
     97        int rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
     98       
    9999        waitq_sleep_finish(wq, rc, ipl);
    100100}
     
    102102/** Start of stoppable section.
    103103 *
    104  * A stoppable section is a section of code where if the thread can be stoped. In other words,
    105  * if a STOP operation is issued, the thread is guaranteed not to execute
    106  * any userspace instructions until the thread is resumed.
     104 * A stoppable section is a section of code where if the thread can
     105 * be stoped. In other words, if a STOP operation is issued, the thread
     106 * is guaranteed not to execute any userspace instructions until the
     107 * thread is resumed.
    107108 *
    108109 * Having stoppable sections is better than having stopping points, since
    109110 * a thread can be stopped even when it is blocked indefinitely in a system
    110111 * call (whereas it would not reach any stopping point).
     112 *
    111113 */
    112114void udebug_stoppable_begin(void)
    113115{
    114         int nsc;
    115         call_t *db_call, *go_call;
    116 
    117116        ASSERT(THREAD);
    118117        ASSERT(TASK);
    119 
     118       
    120119        mutex_lock(&TASK->udebug.lock);
    121 
    122         nsc = --TASK->udebug.not_stoppable_count;
    123 
     120       
     121        int nsc = --TASK->udebug.not_stoppable_count;
     122       
    124123        /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
    125124        mutex_lock(&THREAD->udebug.lock);
    126125        ASSERT(THREAD->udebug.stoppable == false);
    127126        THREAD->udebug.stoppable = true;
    128 
    129         if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
     127       
     128        if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
    130129                /*
    131130                 * This was the last non-stoppable thread. Reply to
    132131                 * DEBUG_BEGIN call.
     132                 *
    133133                 */
    134 
    135                 db_call = TASK->udebug.begin_call;
     134               
     135                call_t *db_call = TASK->udebug.begin_call;
    136136                ASSERT(db_call);
    137 
     137               
    138138                TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
    139139                TASK->udebug.begin_call = NULL;
    140 
     140               
    141141                IPC_SET_RETVAL(db_call->data, 0);
    142                 ipc_answer(&TASK->answerbox, db_call);         
    143 
     142                ipc_answer(&TASK->answerbox, db_call);
    144143        } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
    145144                /*
    146145                 * Active debugging session
    147146                 */
    148 
     147               
    149148                if (THREAD->udebug.active == true &&
    150149                    THREAD->udebug.go == false) {
    151150                        /*
    152151                         * Thread was requested to stop - answer go call
     152                         *
    153153                         */
    154 
     154                       
    155155                        /* Make sure nobody takes this call away from us */
    156                         go_call = THREAD->udebug.go_call;
     156                        call_t *go_call = THREAD->udebug.go_call;
    157157                        THREAD->udebug.go_call = NULL;
    158158                        ASSERT(go_call);
    159 
     159                       
    160160                        IPC_SET_RETVAL(go_call->data, 0);
    161161                        IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
    162 
     162                       
    163163                        THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
    164 
    165                         ipc_answer(&TASK->answerbox, go_call);
     164                        ipc_answer(&TASK->answerbox, go_call);
    166165                }
    167166        }
    168 
     167       
    169168        mutex_unlock(&THREAD->udebug.lock);
    170169        mutex_unlock(&TASK->udebug.lock);
     
    174173 *
    175174 * This is the point where the thread will block if it is stopped.
    176  * (As, by definition, a stopped thread must not leave its stoppable section).
     175 * (As, by definition, a stopped thread must not leave its stoppable
     176 * section).
     177 *
    177178 */
    178179void udebug_stoppable_end(void)
     
    181182        mutex_lock(&TASK->udebug.lock);
    182183        mutex_lock(&THREAD->udebug.lock);
    183 
    184         if (THREAD->udebug.active && THREAD->udebug.go == false) {
     184       
     185        if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
    185186                mutex_unlock(&THREAD->udebug.lock);
    186187                mutex_unlock(&TASK->udebug.lock);
    187 
     188               
    188189                udebug_wait_for_go(&THREAD->udebug.go_wq);
    189 
     190               
    190191                goto restart;
    191192                /* Must try again - have to lose stoppability atomically. */
     
    194195                ASSERT(THREAD->udebug.stoppable == true);
    195196                THREAD->udebug.stoppable = false;
    196 
     197               
    197198                mutex_unlock(&THREAD->udebug.lock);
    198199                mutex_unlock(&TASK->udebug.lock);
     
    203204 *
    204205 * This function is called from clock().
     206 *
    205207 */
    206208void udebug_before_thread_runs(void)
     
    215217 * Must be called before and after servicing a system call. This generates
    216218 * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
     219 *
    217220 */
    218221void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
     
    220223    bool end_variant)
    221224{
    222         call_t *call;
    223         udebug_event_t etype;
    224 
    225         etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
    226 
     225        udebug_event_t etype =
     226            end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
     227       
    227228        mutex_lock(&TASK->udebug.lock);
    228229        mutex_lock(&THREAD->udebug.lock);
    229 
     230       
    230231        /* Must only generate events when in debugging session and is go. */
    231232        if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
     
    235236                return;
    236237        }
    237 
     238       
    238239        /* Fill in the GO response. */
    239         call = THREAD->udebug.go_call;
     240        call_t *call = THREAD->udebug.go_call;
    240241        THREAD->udebug.go_call = NULL;
    241 
     242       
    242243        IPC_SET_RETVAL(call->data, 0);
    243244        IPC_SET_ARG1(call->data, etype);
    244245        IPC_SET_ARG2(call->data, id);
    245246        IPC_SET_ARG3(call->data, rc);
    246 
     247       
    247248        THREAD->udebug.syscall_args[0] = a1;
    248249        THREAD->udebug.syscall_args[1] = a2;
     
    251252        THREAD->udebug.syscall_args[4] = a5;
    252253        THREAD->udebug.syscall_args[5] = a6;
    253 
     254       
    254255        /*
    255256         * Make sure udebug.go is false when going to sleep
    256257         * in case we get woken up by DEBUG_END. (At which
    257258         * point it must be back to the initial true value).
     259         *
    258260         */
    259261        THREAD->udebug.go = false;
    260262        THREAD->udebug.cur_event = etype;
    261 
     263       
    262264        ipc_answer(&TASK->answerbox, call);
    263 
     265       
    264266        mutex_unlock(&THREAD->udebug.lock);
    265267        mutex_unlock(&TASK->udebug.lock);
    266 
     268       
    267269        udebug_wait_for_go(&THREAD->udebug.go_wq);
    268270}
     
    280282 * and get a THREAD_B event for them.
    281283 *
    282  * @param t     Structure of the thread being created. Not locked, as the
    283  *              thread is not executing yet.
    284  * @param ta    Task to which the thread should be attached.
    285  */
    286 void udebug_thread_b_event_attach(struct thread *t, struct task *ta)
    287 {
    288         call_t *call;
    289 
     284 * @param thread Structure of the thread being created. Not locked, as the
     285 *               thread is not executing yet.
     286 * @param task   Task to which the thread should be attached.
     287 *
     288 */
     289void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
     290{
    290291        mutex_lock(&TASK->udebug.lock);
    291292        mutex_lock(&THREAD->udebug.lock);
    292 
    293         thread_attach(t, ta);
    294 
     293       
     294        thread_attach(thread, task);
     295       
    295296        LOG("Check state");
    296 
     297       
    297298        /* Must only generate events when in debugging session */
    298299        if (THREAD->udebug.active != true) {
    299300                LOG("udebug.active: %s, udebug.go: %s",
    300                         THREAD->udebug.active ? "Yes(+)" : "No",
    301                         THREAD->udebug.go ? "Yes(-)" : "No");
     301                    THREAD->udebug.active ? "Yes(+)" : "No",
     302                    THREAD->udebug.go ? "Yes(-)" : "No");
     303               
    302304                mutex_unlock(&THREAD->udebug.lock);
    303305                mutex_unlock(&TASK->udebug.lock);
    304306                return;
    305307        }
    306 
     308       
    307309        LOG("Trigger event");
    308         call = THREAD->udebug.go_call;
     310       
     311        call_t *call = THREAD->udebug.go_call;
     312       
    309313        THREAD->udebug.go_call = NULL;
    310314        IPC_SET_RETVAL(call->data, 0);
    311315        IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
    312         IPC_SET_ARG2(call->data, (unative_t)t);
    313 
     316        IPC_SET_ARG2(call->data, (unative_t) thread);
     317       
    314318        /*
    315319         * Make sure udebug.go is false when going to sleep
    316320         * in case we get woken up by DEBUG_END. (At which
    317321         * point it must be back to the initial true value).
     322         *
    318323         */
    319324        THREAD->udebug.go = false;
    320325        THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
    321 
     326       
    322327        ipc_answer(&TASK->answerbox, call);
    323 
     328       
    324329        mutex_unlock(&THREAD->udebug.lock);
    325330        mutex_unlock(&TASK->udebug.lock);
    326 
     331       
    327332        LOG("Wait for Go");
    328333        udebug_wait_for_go(&THREAD->udebug.go_wq);
     
    333338 * Must be called when the current thread is terminating.
    334339 * Generates a THREAD_E event.
     340 *
    335341 */
    336342void udebug_thread_e_event(void)
    337343{
    338         call_t *call;
    339 
    340344        mutex_lock(&TASK->udebug.lock);
    341345        mutex_lock(&THREAD->udebug.lock);
    342 
     346       
    343347        LOG("Check state");
    344 
     348       
    345349        /* Must only generate events when in debugging session. */
    346350        if (THREAD->udebug.active != true) {
    347351                LOG("udebug.active: %s, udebug.go: %s",
    348                         THREAD->udebug.active ? "Yes" : "No",
    349                         THREAD->udebug.go ? "Yes" : "No");
     352                    THREAD->udebug.active ? "Yes" : "No",
     353                    THREAD->udebug.go ? "Yes" : "No");
     354               
    350355                mutex_unlock(&THREAD->udebug.lock);
    351356                mutex_unlock(&TASK->udebug.lock);
    352357                return;
    353358        }
    354 
     359       
    355360        LOG("Trigger event");
    356         call = THREAD->udebug.go_call;
     361       
     362        call_t *call = THREAD->udebug.go_call;
     363       
    357364        THREAD->udebug.go_call = NULL;
    358365        IPC_SET_RETVAL(call->data, 0);
    359366        IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
    360 
     367       
    361368        /* Prevent any further debug activity in thread. */
    362369        THREAD->udebug.active = false;
    363         THREAD->udebug.cur_event = 0;           /* none */
    364         THREAD->udebug.go = false;      /* set to initial value */
    365 
     370        THREAD->udebug.cur_event = 0;   /* None */
     371        THREAD->udebug.go = false;      /* Set to initial value */
     372       
    366373        ipc_answer(&TASK->answerbox, call);
    367 
     374       
    368375        mutex_unlock(&THREAD->udebug.lock);
    369376        mutex_unlock(&TASK->udebug.lock);
    370 
    371         /* 
     377       
     378        /*
    372379         * This event does not sleep - debugging has finished
    373380         * in this thread.
     381         *
    374382         */
    375383}
    376384
    377 /**
    378  * Terminate task debugging session.
    379  *
    380  * Gracefully terminates the debugging session for a task. If the debugger
     385/** Terminate task debugging session.
     386 *
     387 * Gracefully terminate the debugging session for a task. If the debugger
    381388 * is still waiting for events on some threads, it will receive a
    382389 * FINISHED event for each of them.
    383390 *
    384  * @param ta    Task structure. ta->udebug.lock must be already locked.
    385  * @return      Zero on success or negative error code.
    386  */
    387 int udebug_task_cleanup(struct task *ta)
    388 {
    389         thread_t *t;
     391 * @param task Task structure. ta->udebug.lock must be already locked.
     392 *
     393 * @return Zero on success or negative error code.
     394 *
     395 */
     396int udebug_task_cleanup(struct task *task)
     397{
     398        if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
     399            (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
     400                return EINVAL;
     401        }
     402       
     403        LOG("Task %" PRIu64, task->taskid);
     404       
     405        /* Finish debugging of all userspace threads */
    390406        link_t *cur;
    391         int flags;
    392         ipl_t ipl;
    393 
    394         if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
    395             ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
    396                 return EINVAL;
    397         }
    398 
    399         LOG("Task %" PRIu64, ta->taskid);
    400 
    401         /* Finish debugging of all userspace threads */
    402         for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
    403                 t = list_get_instance(cur, thread_t, th_link);
    404 
    405                 mutex_lock(&t->udebug.lock);
    406 
    407                 ipl = interrupts_disable();
    408                 spinlock_lock(&t->lock);
    409 
    410                 flags = t->flags;
    411 
    412                 spinlock_unlock(&t->lock);
    413                 interrupts_restore(ipl);
    414 
     407        for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
     408                thread_t *thread = list_get_instance(cur, thread_t, th_link);
     409               
     410                mutex_lock(&thread->udebug.lock);
     411                unsigned int flags = thread->flags;
     412               
    415413                /* Only process userspace threads. */
    416414                if ((flags & THREAD_FLAG_USPACE) != 0) {
    417415                        /* Prevent any further debug activity in thread. */
    418                         t->udebug.active = false;
    419                         t->udebug.cur_event = 0;        /* none */
    420 
     416                        thread->udebug.active = false;
     417                        thread->udebug.cur_event = 0;   /* None */
     418                       
    421419                        /* Is the thread still go? */
    422                         if (t->udebug.go == true) {
     420                        if (thread->udebug.go == true) {
    423421                                /*
    424                                 * Yes, so clear go. As active == false,
     422                                 * Yes, so clear go. As active == false,
    425423                                 * this doesn't affect anything.
     424                                 (
    426425                                 */
    427                                 t->udebug.go = false;   
    428 
     426                                thread->udebug.go = false;
     427                               
    429428                                /* Answer GO call */
    430429                                LOG("Answer GO call with EVENT_FINISHED.");
    431                                 IPC_SET_RETVAL(t->udebug.go_call->data, 0);
    432                                 IPC_SET_ARG1(t->udebug.go_call->data,
     430                               
     431                                IPC_SET_RETVAL(thread->udebug.go_call->data, 0);
     432                                IPC_SET_ARG1(thread->udebug.go_call->data,
    433433                                    UDEBUG_EVENT_FINISHED);
    434 
    435                                 ipc_answer(&ta->answerbox, t->udebug.go_call);
    436                                 t->udebug.go_call = NULL;
     434                               
     435                                ipc_answer(&task->answerbox, thread->udebug.go_call);
     436                                thread->udebug.go_call = NULL;
    437437                        } else {
    438438                                /*
    439439                                 * Debug_stop is already at initial value.
    440440                                 * Yet this means the thread needs waking up.
     441                                 *
    441442                                 */
    442 
     443                               
    443444                                /*
    444                                  * t's lock must not be held when calling
     445                                 * thread's lock must not be held when calling
    445446                                 * waitq_wakeup.
     447                                 *
    446448                                 */
    447                                 waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
     449                                waitq_wakeup(&thread->udebug.go_wq, WAKEUP_FIRST);
    448450                        }
    449                         mutex_unlock(&t->udebug.lock);
    450                         condvar_broadcast(&t->udebug.active_cv);
    451                 } else {
    452                         mutex_unlock(&t->udebug.lock);
    453                 }
    454         }
    455 
    456         ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
    457         ta->udebug.debugger = NULL;
    458 
     451                       
     452                        mutex_unlock(&thread->udebug.lock);
     453                        condvar_broadcast(&thread->udebug.active_cv);
     454                } else
     455                        mutex_unlock(&thread->udebug.lock);
     456        }
     457       
     458        task->udebug.dt_state = UDEBUG_TS_INACTIVE;
     459        task->udebug.debugger = NULL;
     460       
    459461        return 0;
    460462}
     
    466468 * a chance to examine the faulting thead/task. When the debugging session
    467469 * is over, this function returns (so that thread/task cleanup can continue).
     470 *
    468471 */
    469472void udebug_thread_fault(void)
    470473{
    471474        udebug_stoppable_begin();
    472 
     475       
    473476        /* Wait until a debugger attends to us. */
    474477        mutex_lock(&THREAD->udebug.lock);
     
    476479                condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
    477480        mutex_unlock(&THREAD->udebug.lock);
    478 
     481       
    479482        /* Make sure the debugging session is over before proceeding. */
    480483        mutex_lock(&THREAD->udebug.lock);
     
    482485                condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
    483486        mutex_unlock(&THREAD->udebug.lock);
    484 
     487       
    485488        udebug_stoppable_end();
    486489}
Note: See TracChangeset for help on using the changeset viewer.