Subversion Repositories HelenOS

Rev

Rev 2815 | Rev 2817 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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