aboutsummaryrefslogtreecommitdiffstats
path: root/tools/debugger/pdb/pdb_caml_process.c
blob: 0f911fa754458ffb5ed5508df0b518ce12f6ca53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * pdb_caml_process.c
 *
 * http://www.cl.cam.ac.uk/netos/pdb
 *
 * PDB's OCaml interface library for debugging processes
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>

#include <xc.h>
#include <xen/xen.h>
#include <xen/io/domain_controller.h>
#include <xen/linux/privcmd.h>
#include "pdb_module.h"
#include "pdb_caml_xen.h"

typedef struct
{
    int domain;
    int process;
    int evtchn;
    pdb_front_ring_t *ring;
} context_t;

#define decode_context(_ctx, _ocaml)   \
{  \
    (_ctx)->domain  = Int_val(Field((_ocaml),0));  \
    (_ctx)->process = Int_val(Field((_ocaml),1));  \
    (_ctx)->evtchn  = Int_val(Field((_ocaml),2));  \
    (_ctx)->ring    =  (pdb_front_ring_t *)Int32_val(Field((_ocaml),3));  \
}

#define encode_context(_ctx, _ocaml)  \
{  \
    (_ocaml) = caml_alloc_tuple(2);  \
    Store_field((_ocaml), 0, Val_int((_ctx)->domain));  \
    Store_field((_ocaml), 1, Val_int((_ctx)->process));  \
}

/*
 * send a request to a pdb domain backend.
 *
 * puts the request on a ring and kicks the backend using an event channel.
 */
static void
send_request (pdb_front_ring_t *pdb_ring, int evtchn, pdb_request_t *request)
{
    pdb_request_t    *req;

    req = RING_GET_REQUEST(pdb_ring, pdb_ring->req_prod_pvt);

    memcpy(req, request, sizeof(pdb_request_t));

    pdb_ring->req_prod_pvt++;

    RING_PUSH_REQUESTS(pdb_ring);
    xc_evtchn_send(xc_handle, evtchn);
}

/*
 * process_handle_response : int32 -> int * int * string
 *
 * A backend domain has notified pdb (via an event channel)
 * that a command has finished.
 * We read the result from the channel and formulate a response
 * as a single string.  Also return the domain and process.
 */

static inline unsigned int
_flip (unsigned int orig)
{
    return (((orig << 24) & 0xff000000) | ((orig <<  8) & 0x00ff0000) |
            ((orig >>  8) & 0x0000ff00) | ((orig >> 24) & 0x000000ff));
}

value
process_handle_response (value ring)
{
    CAMLparam1(ring);
    CAMLlocal2(result, str);

    RING_IDX rp;
    pdb_response_p resp;
    pdb_front_ring_t *my_ring = (pdb_front_ring_t *)Int32_val(ring);
    char msg[2048];
    int msglen;

    memset(msg, 0, sizeof(msg));

    rp = my_ring->sring->rsp_prod;
    rmb();                     /* Ensure we see queued responses up to 'rp'. */

    /* default response is OK unless the command has something 
       more interesting to say */
    sprintf(msg, "OK");

    if (my_ring->rsp_cons != rp)
    {
        resp = RING_GET_RESPONSE(my_ring, my_ring->rsp_cons);

        switch (resp->operation)
        {
        case PDB_OPCODE_PAUSE :
        case PDB_OPCODE_ATTACH :
        case PDB_OPCODE_DETACH :
            break;
            
        case PDB_OPCODE_RD_REGS :
        {
            int loop;
            pdb_op_rd_regs_p regs = &resp->u.rd_regs;
            
            for (loop = 0; loop < GDB_REGISTER_FRAME_SIZE * 8; loop += 8)
            {
                sprintf(&msg[loop], "%08x", _flip(regs->reg[loop >> 3]));
            }
                
            break;
        }
        case PDB_OPCODE_WR_REG :
        {
            /* should check the return status */
            break;
        }

        case PDB_OPCODE_RD_MEM :
        {
            int loop;
            pdb_op_rd_mem_resp_p mem = &resp->u.rd_mem;

            for (loop = 0; loop < mem->length; loop ++)
            {
                sprintf(&msg[loop * 2], "%02x", mem->data[loop]);
            }
            break;
        }
        case PDB_OPCODE_WR_MEM :
        {
            /* should check the return status */
            break;
        }

        /* this is equivalent to process_xen_virq */
        case PDB_OPCODE_CONTINUE :
        {
            sprintf(msg, "S05");
            break;
        }
        case PDB_OPCODE_STEP :
        {
            sprintf(msg, "S05");
            break;
        }

        case PDB_OPCODE_SET_BKPT :
        {
            break;
        }
        case PDB_OPCODE_CLR_BKPT :
        {
            break;
        }

        default :
            printf("(linux) UNKNOWN MESSAGE TYPE IN RESPONSE\n");
            break;
        }

        my_ring->rsp_cons++;
    }

    msglen = strlen(msg);
    result = caml_alloc(3,0);
    str = alloc_string(msglen);
    memmove(&Byte(str,0), msg, msglen);

    Store_field(result, 0, Val_int(resp->domain));
    Store_field(result, 1, Val_int(resp->process));
    Store_field(result, 2, str);

    CAMLreturn(result);
}

/*
 * proc_attach_debugger : context_t -> unit
 */
value
proc_attach_debugger (value context)
{
    CAMLparam1(context);
    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_ATTACH;
    req.u.attach.domain  = ctx.domain;
    req.process = ctx.process;

    send_request (ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}


/*
 * proc_detach_debugger : context_t -> unit
 */
value
proc_detach_debugger (value context)
{
    CAMLparam1(context);
    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    printf("(pdb) detach process [%d.%d] %d %p\n", ctx.domain, ctx.process,
           ctx.evtchn, ctx.ring);
    fflush(stdout);

    req.operation = PDB_OPCODE_DETACH;
    req.process = ctx.process;

    send_request (ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}


/*
 * proc_pause_target : int -> unit
 */
value
proc_pause_target (value context)
{
    CAMLparam1(context);
    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    printf("(pdb) pause target %d %d\n", ctx.domain, ctx.process);
    fflush(stdout);

    req.operation = PDB_OPCODE_PAUSE;
    req.process = ctx.process;

    send_request (ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}


/*
 * proc_read_registers : context_t -> unit
 */
value
proc_read_registers (value context)
{
    CAMLparam1(context);

    pdb_request_t req;
    context_t ctx;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_RD_REGS;
    req.process = ctx.process;

    send_request (ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}


/*
 * proc_write_register : context_t -> register -> int32 -> unit
 */
value
proc_write_register (value context, value reg, value newval)
{
    CAMLparam3(context, reg, newval);

    int my_reg = Int_val(reg);
    unsigned long my_newval = Int32_val(newval);

    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_WR_REG;
    req.process = ctx.process;
    req.u.wr_reg.value = my_newval;

    switch (my_reg)
    {
    case GDB_EAX: req.u.wr_reg.reg = LINUX_EAX; break;
    case GDB_ECX: req.u.wr_reg.reg = LINUX_ECX; break;
    case GDB_EDX: req.u.wr_reg.reg = LINUX_EDX; break;
    case GDB_EBX: req.u.wr_reg.reg = LINUX_EBX; break;

    case GDB_ESP: req.u.wr_reg.reg = LINUX_ESP; break;
    case GDB_EBP: req.u.wr_reg.reg = LINUX_EBP; break;
    case GDB_ESI: req.u.wr_reg.reg = LINUX_ESI; break;
    case GDB_EDI: req.u.wr_reg.reg = LINUX_EDI; break;

    case GDB_EIP: req.u.wr_reg.reg = LINUX_EIP; break;
    case GDB_EFL: req.u.wr_reg.reg = LINUX_EFL; break;
 
    case GDB_CS:  req.u.wr_reg.reg = LINUX_CS; break;
    case GDB_SS:  req.u.wr_reg.reg = LINUX_SS; break;
    case GDB_DS:  req.u.wr_reg.reg = LINUX_DS; break;
    case GDB_ES:  req.u.wr_reg.reg = LINUX_ES; break;
    case GDB_FS:  req.u.wr_reg.reg = LINUX_FS; break;
    case GDB_GS:  req.u.wr_reg.reg = LINUX_GS; break;
    }

    send_request(ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}


/*
 * proc_read_memory : context_t -> int32 -> int -> unit
 */
value
proc_read_memory (value context, value address, value length)
{
    CAMLparam3(context, address, length);

    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_RD_MEM;
    req.process = ctx.process;
    req.u.rd_mem.address = Int32_val(address);
    req.u.rd_mem.length  = Int_val(length);

    send_request(ctx.ring, ctx.evtchn, &req);
    
    CAMLreturn(Val_unit);
}


/*
 * proc_write_memory : context_t -> int32 -> int list -> unit
 */
value
proc_write_memory (value context, value address, value val_list)
{
    CAMLparam3(context, address, val_list);
    CAMLlocal1(node);

    context_t ctx;
    pdb_request_t req;
    u32 length = 0;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_WR_MEM;
    req.process = ctx.process;

    node = val_list;
    if ( Int_val(node) == 0 )       /* gdb functionalty test uses empty list */
    {
        req.u.wr_mem.address = Int32_val(address);
        req.u.wr_mem.length  = 0;
    }
    else
    {
        while ( Int_val(Field(node,1)) != 0 )
        {
            req.u.wr_mem.data[length++] = Int_val(Field(node, 0));
            node = Field(node,1);
        }
        req.u.wr_mem.data[length++] = Int_val(Field(node, 0));
        
        req.u.wr_mem.address = Int32_val(address);
        req.u.wr_mem.length  = length;
    }
 
    send_request(ctx.ring, ctx.evtchn, &req);
   
    CAMLreturn(Val_unit);
}


/*
 * proc_continue_target : context_t -> unit
 */
value
proc_continue_target (value context)
{
    CAMLparam1(context);

    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_CONTINUE;
    req.process = ctx.process;
 
    send_request(ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}

/*
 * proc_step_target : context_t -> unit
 */
value
proc_step_target (value context)
{
    CAMLparam1(context);

    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_STEP;
    req.process = ctx.process;
 
    send_request(ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}



/*
 * proc_insert_memory_breakpoint : context_t -> int32 -> int list -> unit
 */
value
proc_insert_memory_breakpoint (value context, value address, value length)
{
    CAMLparam3(context, address, length);

    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_SET_BKPT;
    req.process = ctx.process;
    req.u.bkpt.address = (memory_t) Int32_val(address);
    req.u.bkpt.length  =  Int_val(length);

    send_request(ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}

/*
 * proc_remove_memory_breakpoint : context_t -> int32 -> int list -> unit
 */
value
proc_remove_memory_breakpoint (value context, value address, value length)
{
    CAMLparam3(context, address, length);

    context_t ctx;
    pdb_request_t req;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_CLR_BKPT;
    req.process = ctx.process;
    req.u.bkpt.address = (memory_t) Int32_val(address);
    req.u.bkpt.length  =  Int_val(length);

    send_request(ctx.ring, ctx.evtchn, &req);

    CAMLreturn(Val_unit);
}


/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */