aboutsummaryrefslogtreecommitdiffstats
path: root/tools/debugger/pdb/pdb_caml_process.c
blob: e8506c771119de20c16d023f8c4f3d0caf2d3eef (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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * 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"

/* this order comes from linux-2.6.11/include/asm-i386/ptrace.h */
enum x86_registers { LINUX_EBX, LINUX_ECX, LINUX_EDX, LINUX_ESI, LINUX_EDI,
                     LINUX_EBP, LINUX_EAX, LINUX_DS,  LINUX_ES,  LINUX_FS,
                     LINUX_GS,  LINUX_ORIG_EAX, LINUX_EIP, LINUX_CS, LINUX_EFL,
                     LINUX_ESP, LINUX_SS };
#define FRAME_SIZE 17

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);
}

/*
 * read a response from a pdb domain backend.
 *
 * grabs the response off a ring.
 */
static void
read_response (pdb_front_ring_t *pdb_ring, pdb_response_p response)
{
    RING_IDX loop, rp;

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

    for ( loop = pdb_ring->rsp_cons; loop != rp; loop++ )
    {
        pdb_response_p resp;

        resp = RING_GET_RESPONSE(pdb_ring, loop);
        memcpy(response, resp, sizeof(pdb_response_t));

        /*        
        printf ("got response %x %x %x\n", response->operation, 
                response->status, response->value);
        */
    }
    pdb_ring->rsp_cons = loop;
}

/*
 * process_handle_response : int32 -> unit
 */

value
process_handle_response (value ring)
{
    CAMLparam1(ring);

    pdb_front_ring_t *my_ring = (pdb_front_ring_t *)Int32_val(ring);
    pdb_response_t resp;

    if ( my_ring )
        read_response(my_ring, &resp);

    CAMLreturn(Val_unit);
}

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

    decode_context(&ctx, context);

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

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

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

    printf("awaiting response\n");
    fflush(stdout);

    read_response (ctx.ring, &resp);

    printf("response %d %d\n", resp.operation, resp.status);
    fflush(stdout);

    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.domain  = ctx.domain;
    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;

    decode_context(&ctx, context);

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

    CAMLreturn(Val_unit);
}


/*
 * proc_read_registers : context_t -> int32
 */
value
proc_read_registers (value context)
{
    CAMLparam1(context);
    CAMLlocal1(result);

    u32 regs[FRAME_SIZE];

    pdb_request_t req;
    context_t ctx;
    int loop;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_RD_REG;
    req.domain  = ctx.domain;
    req.process = ctx.process;

    for (loop = 0; loop < FRAME_SIZE; loop++)
    {
        pdb_response_t resp;

        req.u.rd_reg.reg = loop;
        send_request(ctx.ring, ctx.evtchn, &req);
        read_response(ctx.ring, &resp);
        regs[loop] = resp.value;
    }

    result = caml_alloc_tuple(16);

    Store_field(result,  0, caml_copy_int32(regs[LINUX_EAX]));
    Store_field(result,  1, caml_copy_int32(regs[LINUX_ECX]));
    Store_field(result,  2, caml_copy_int32(regs[LINUX_EDX]));
    Store_field(result,  3, caml_copy_int32(regs[LINUX_EBX]));
    Store_field(result,  4, caml_copy_int32(regs[LINUX_ESP]));
    Store_field(result,  5, caml_copy_int32(regs[LINUX_EBP]));
    Store_field(result,  6, caml_copy_int32(regs[LINUX_ESI]));
    Store_field(result,  7, caml_copy_int32(regs[LINUX_EDI]));
    Store_field(result,  8, caml_copy_int32(regs[LINUX_EIP]));
    Store_field(result,  9, caml_copy_int32(regs[LINUX_EFL]));
    Store_field(result, 10, caml_copy_int32(regs[LINUX_CS]));          /* 16 */
    Store_field(result, 11, caml_copy_int32(regs[LINUX_SS]));          /* 16 */
    Store_field(result, 12, caml_copy_int32(regs[LINUX_DS]));          /* 16 */
    Store_field(result, 13, caml_copy_int32(regs[LINUX_ES]));          /* 16 */
    Store_field(result, 14, caml_copy_int32(regs[LINUX_FS]));          /* 16 */
    Store_field(result, 15, caml_copy_int32(regs[LINUX_GS]));          /* 16 */

    CAMLreturn(result);
}


/*
 * 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;
    pdb_response_t resp;

    decode_context(&ctx, context);

    req.operation = PDB_OPCODE_WR_REG;
    req.domain = ctx.domain;
    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);
    read_response(ctx.ring, &resp);

    CAMLreturn(Val_unit);
}


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

    context_t ctx;
    int loop;
    char *buffer;
    /*    memory_t my_address = Int32_val(address); */
    u32 my_length = Int_val(length);

    printf ("(pdb) read memory\n");

    decode_context(&ctx, context);

    buffer = malloc(my_length);
    if ( buffer == NULL )
    {
        printf("(pdb) read memory: malloc failed.\n");  fflush(stdout);
        failwith("read memory error");
    }

    /*
    if ( xendebug_read_memory(xc_handle, ctx.domain, ctx.vcpu, 
                              my_address, my_length, buffer) )
    {
        printf("(pdb) read memory error!\n");  fflush(stdout);
        failwith("read memory error");
    }
    */

    memset(buffer, 0xff, my_length);

    result = caml_alloc(2,0);
    if ( my_length > 0 )                                              /* car */
    {
        Store_field(result, 0, Val_int(buffer[my_length - 1] & 0xff));
    }
    else

    {
        Store_field(result, 0, Val_int(0));                    
    }
    Store_field(result, 1, Val_int(0));                               /* cdr */

    for (loop = 1; loop < my_length; loop++)
    {
        temp = result;
        result = caml_alloc(2,0);
        Store_field(result, 0, Val_int(buffer[my_length - loop - 1] & 0xff));
        Store_field(result, 1, temp);
    }

    CAMLreturn(result);
}

/*
 * 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;

    char buffer[4096];  /* a big buffer */
    memory_t  my_address;
    u32 length = 0;

    printf ("(pdb) write memory\n");

    decode_context(&ctx, context);

    node = val_list;
    if ( Int_val(node) == 0 )       /* gdb functionalty test uses empty list */
    {
        CAMLreturn(Val_unit);
    }

    while ( Int_val(Field(node,1)) != 0 )
    {
        buffer[length++] = Int_val(Field(node, 0));
        node = Field(node,1);
    }
    buffer[length++] = Int_val(Field(node, 0));

    my_address = (memory_t) Int32_val(address);

    /*
    if ( xendebug_write_memory(xc_handle, ctx.domain, ctx.vcpu,
                               my_address, length, buffer) )
    {
        printf("(pdb) write memory error!\n");  fflush(stdout);
        failwith("write memory error");
    }
    */
    {
        int loop;
        for (loop = 0; loop < length; loop++)
        {
            printf (" %02x", buffer[loop]);
        }
        printf ("\n");
    }

    CAMLreturn(Val_unit);
}



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

    context_t ctx;

    decode_context(&ctx, context);

    /*
    if ( xendebug_continue(xc_handle, ctx.domain, ctx.vcpu) )
    {
        printf("(pdb) continue\n");  fflush(stdout);
        failwith("continue");
    }
    */
    printf ("CONTINUE\n");

    CAMLreturn(Val_unit);
}

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

    context_t ctx;

    decode_context(&ctx, context);

    /*
    if ( xendebug_step(xc_handle, ctx.domain, ctx.vcpu) )
    {
        printf("(pdb) step\n");  fflush(stdout);
        failwith("step");
    }
    */
    printf ("STEP\n");

    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;
    memory_t my_address = (memory_t) Int32_val(address);
    int my_length = Int_val(length);

    decode_context(&ctx, context);

    printf ("(pdb) insert memory breakpoint 0x%lx %d\n",
            my_address, my_length);

    /*
    if ( xendebug_insert_memory_breakpoint(xc_handle, ctx.domain, ctx.vcpu,
                                           my_address, my_length) )
    {
        printf("(pdb) error: insert memory breakpoint\n");  fflush(stdout);
        failwith("insert memory breakpoint");
    }
    */

    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;

    memory_t my_address = (memory_t) Int32_val(address);
    int my_length = Int_val(length);

    printf ("(pdb) remove memory breakpoint 0x%lx %d\n",
            my_address, my_length);

    decode_context(&ctx, context);

    /*
    if ( xendebug_remove_memory_breakpoint(xc_handle, 
                                           ctx.domain, ctx.vcpu,
                                           my_address, my_length) )
    {
        printf("(pdb) error: remove memory breakpoint\n");  fflush(stdout);
        failwith("remove memory breakpoint");
    }
    */

    CAMLreturn(Val_unit);
}


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