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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/*
* jit-debugger.c - Helper routines for single-step debugging of programs.
*
* Copyright (C) 2004 Southern Storm Software, Pty Ltd.
*
* This file is part of the libjit library.
*
* The libjit library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 2.1 of
* the License, or (at your option) any later version.
*
* The libjit library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the libjit library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/*@
@cindex jit-debugger.h
The @code{libjit} library provides support routines for breakpoint-based
single-step debugging. It isn't a full debugger, but provides the
infrastructure necessary to support one.
The front end virtual machine is responsible for inserting "potential
breakpoints" into the code when functions are built and compiled. This
is performed using @code{jit_insn_mark_breakpoint}:
@deftypefun int jit_insn_mark_breakpoint (jit_function_t @var{func}, jit_nint @var{data1}, jit_nint @var{data2})
Mark the current position in @var{func} as corresponding to a breakpoint
location. When a break occurs, the debugging routines are passed
@var{func}, @var{data1}, and @var{data2} as arguments. By convention,
@var{data1} is the type of breakpoint (source line, function entry,
function exit, etc).
@end deftypefun
There are two ways for a front end to receive notification about breakpoints.
The bulk of this chapter describes the @code{jit_debugger_t} interface,
which handles most of the ugly details. In addition, a low-level "debug hook
mechanism" is provided for front ends that wish more control over the
process. The debug hook mechanism is described below, under the
@code{jit_debugger_set_hook} function.
This debugger implementation requires a threading system to work
successfully. At least two threads are required, in addition to those of
the program being debugged:
@enumerate
@item
Event thread which calls @code{jit_debugger_wait_event} to receive
notifications of breakpoints and other interesting events.
@item
User interface thread which calls functions like @code{jit_debugger_run},
@code{jit_debugger_step}, etc, to control the debug process.
@end enumerate
These two threads should be set to "unbreakable" with a call to
@code{jit_debugger_set_breakable}. This prevents them from accidentally
stopping at a breakpoint, which would cause a system deadlock.
Other housekeeping threads, such as a finalization thread, should
also be set to "unbreakable" for the same reason.
@noindent
Events have the following members:
@table @code
@item type
The type of event (see the next table for details).
@item thread
The thread that the event occurred on.
@item function
The function that the breakpoint occurred within.
@item data1
@itemx data2
The data values at the breakpoint. These values are inserted into
the function's code with @code{jit_insn_mark_breakpoint}.
@item id
The identifier for the breakpoint.
@item trace
The stack trace corresponding to the location where the breakpoint
occurred. This value is automatically freed upon the next call
to @code{jit_debugger_wait_event}. If you wish to preserve the
value, then you must call @code{jit_stack_trace_copy}.
@end table
@noindent
The following event types are currently supported:
@table @code
@item JIT_DEBUGGER_TYPE_QUIT
A thread called @code{jit_debugger_quit}, indicating that it wanted the
event thread to terminate.
@item JIT_DEBUGGER_TYPE_HARD_BREAKPOINT
A thread stopped at a hard breakpoint. That is, a breakpoint defined
by a call to @code{jit_debugger_add_breakpoint}.
@item JIT_DEBUGGER_TYPE_SOFT_BREAKPOINT
A thread stopped at a breakpoint that wasn't explicitly defined by
a call to @code{jit_debugger_add_breakpoint}. This typicaly results
from a call to a "step" function like @code{jit_debugger_step}, where
execution stopped at the next line but there isn't an explicit breakpoint
on that line.
@item JIT_DEBUGGER_TYPE_USER_BREAKPOINT
A thread stopped because of a call to @code{jit_debugger_break}.
@item JIT_DEBUGGER_TYPE_ATTACH_THREAD
A thread called @code{jit_debugger_attach_self}. The @code{data1} field
of the event is set to the value of @code{stop_immediately} for the call.
@item JIT_DEBUGGER_TYPE_DETACH_THREAD
A thread called @code{jit_debugger_detach_self}.
@end table
@deftypefun int jit_insn_mark_breakpoint_variable (jit_function_t @var{func}, jit_value_t @var{data1}, jit_value_t @var{data2})
This function is similar to @code{jit_insn_mark_breakpoint} except that values
in @var{data1} and @var{data2} can be computed at runtime. You can use this
function for example to get address of local variable.
@end deftypefun
@*/
/*
* Linked event, for the debugger event queue.
*/
typedef struct jit_debugger_linked_event
jit_debugger_linked_event_t;
/*
* Run types.
*/
/*
* Information about a thread that is under the control of the debugger.
*/
typedef struct jit_debugger_thread
*jit_debugger_thread_t;
/*
* Structure of a debugger instance.
*/
;
/*
* Lock the debugger object.
*/
/*
* Unlock the debugger object.
*/
/*
* Suspend the current thread until it is marked as running again.
* It is assumed that the debugger's monitor lock has been acquired.
*/
static void
/*
* Wake all threads that are waiting on the debugger's monitor.
*/
/*
* Get the information block for the current thread.
*/
static jit_debugger_thread_t
/*
* Get the information block for a specific thread.
*/
static jit_debugger_thread_t
/*
* Allocate space for a new event on the event queue.
*/
/*
* Add an event that was previously allocated with "alloc_event"
* to a debugger's event queue.
*/
static void
/*@
* @deftypefun int jit_debugging_possible (void)
* Determine if debugging is possible. i.e. that threading is available
* and compatible with the debugger's requirements.
* @end deftypefun
@*/
int
/*@
* @deftypefun jit_debugger_t jit_debugger_create (jit_context_t @var{context})
* Create a new debugger instance and attach it to a JIT @var{context}.
* If the context already has a debugger associated with it, then this
* function will return the previous debugger.
* @end deftypefun
@*/
jit_debugger_t
/*@
* @deftypefun void jit_debugger_destroy (jit_debugger_t @var{dbg})
* Destroy a debugger instance.
* @end deftypefun
@*/
void
/*@
* @deftypefun jit_context_t jit_debugger_get_context (jit_debugger_t @var{dbg})
* Get the JIT context that is associated with a debugger instance.
* @end deftypefun
@*/
jit_context_t
/*@
* @deftypefun jit_debugger_t jit_debugger_from_context (jit_context_t @var{context})
* Get the debugger that is currently associated with a JIT @var{context},
* or NULL if there is no debugger associated with the context.
* @end deftypefun
@*/
jit_debugger_t
/*@
* @deftypefun jit_debugger_thread_id_t jit_debugger_get_self (jit_debugger_t @var{dbg})
* Get the thread identifier associated with the current thread.
* The return values are normally values like 1, 2, 3, etc, allowing
* the user interface to report messages like "thread 3 has stopped
* at a breakpoint".
* @end deftypefun
@*/
jit_debugger_thread_id_t
/*@
* @deftypefun jit_debugger_thread_id_t jit_debugger_get_thread (jit_debugger_t @var{dbg}, const void *@var{native_thread})
* Get the thread identifier for a specific native thread. The
* @var{native_thread} pointer is assumed to point at a block
* of memory containing a native thread handle. This would be a
* @code{pthread_t} on Pthreads platforms or a @code{HANDLE}
* on Win32 platforms. If the native thread has not been seen
* previously, then a new thread identifier is allocated.
* @end deftypefun
@*/
jit_debugger_thread_id_t
/*@
* @deftypefun int jit_debugger_get_native_thread (jit_debugger_t @var{dbg}, jit_debugger_thread_id_t @var{thread}, void *@var{native_thread})
* Get the native thread handle associated with a debugger thread identifier.
* Returns non-zero if OK, or zero if the debugger thread identifier is not
* yet associated with a native thread handle.
* @end deftypefun
@*/
int
/*@
* @deftypefun void jit_debugger_set_breakable (jit_debugger_t @var{dbg}, const void *@var{native_thread}, int @var{flag})
* Set a flag that indicates if a native thread can stop at breakpoints.
* If set to 1 (the default), breakpoints will be active on the thread.
* If set to 0, breakpoints will be ignored on the thread. Typically
* this is used to mark threads associated with the debugger's user
* interface, or the virtual machine's finalization thread, so that they
* aren't accidentally suspended by the debugger (which might cause a
* deadlock).
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_attach_self (jit_debugger_t @var{dbg}, int @var{stop_immediately})
* Attach the current thread to a debugger. If @var{stop_immediately}
* is non-zero, then the current thread immediately suspends, waiting for
* the user to start it with @code{jit_debugger_run}. This function is
* typically called in a thread's startup code just before any "real work"
* is performed.
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_detach_self (jit_debugger_t @var{dbg})
* Detach the current thread from the debugger. This is typically
* called just before the thread exits.
* @end deftypefun
@*/
void
/*@
* @deftypefun int jit_debugger_wait_event (jit_debugger_t @var{dbg}, jit_debugger_event_t *@var{event}, jit_nint @var{timeout})
* Wait for the next debugger event to arrive. Debugger events typically
* indicate breakpoints that have occurred. The @var{timeout} is in
* milliseconds, or -1 for an infinite timeout period. Returns non-zero
* if an event has arrived, or zero on timeout.
* @end deftypefun
@*/
int
/*@
* @deftypefun jit_debugger_breakpoint_id_t jit_debugger_add_breakpoint (jit_debugger_t @var{dbg}, jit_debugger_breakpoint_info_t @var{info})
* Add a hard breakpoint to a debugger instance. The @var{info} structure
* defines the conditions under which the breakpoint should fire.
* The fields of @var{info} are as follows:
*
* @table @code
* @item flags
* Flags that indicate which of the following fields should be matched.
* If a flag is not present, then all possible values of the field will match.
* Valid flags are @code{JIT_DEBUGGER_FLAG_THREAD},
* @code{JIT_DEBUGGER_FLAG_FUNCTION}, @code{JIT_DEBUGGER_FLAG_DATA1},
* and @code{JIT_DEBUGGER_FLAG_DATA2}.
*
* @item thread
* The thread to match against, if @code{JIT_DEBUGGER_FLAG_THREAD} is set.
*
* @item function
* The function to match against, if @code{JIT_DEBUGGER_FLAG_FUNCTION} is set.
*
* @item data1
* The @code{data1} value to match against, if @code{JIT_DEBUGGER_FLAG_DATA1}
* is set.
*
* @item data2
* The @code{data2} value to match against, if @code{JIT_DEBUGGER_FLAG_DATA2}
* is set.
* @end table
*
* The following special values for @code{data1} are recommended for marking
* breakpoint locations with @code{jit_insn_mark_breakpoint}:
*
* @table @code
* @item JIT_DEBUGGER_DATA1_LINE
* Breakpoint location that corresponds to a source line. This is used
* to determine where to continue to upon a "step".
*
* @item JIT_DEBUGGER_DATA1_ENTER
* Breakpoint location that corresponds to the start of a function.
*
* @item JIT_DEBUGGER_DATA1_LEAVE
* Breakpoint location that corresponds to the end of a function, just
* prior to a @code{return} statement. This is used to determine where
* to continue to upon a "finish".
*
* @item JIT_DEBUGGER_DATA1_THROW
* Breakpoint location that corresponds to an exception throw.
* @end table
* @end deftypefun
@*/
jit_debugger_breakpoint_id_t
/*@
* @deftypefun void jit_debugger_remove_breakpoint (jit_debugger_t @var{dbg}, jit_debugger_breakpoint_id_t @var{id})
* Remove a previously defined breakpoint from a debugger instance.
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_remove_all_breakpoints (jit_debugger_t @var{dbg})
* Remove all breakpoints from a debugger instance.
* @end deftypefun
@*/
void
/*@
* @deftypefun int jit_debugger_is_alive (jit_debugger_t @var{dbg}, jit_debugger_thread_id_t @var{thread})
* Determine if a particular thread is still alive.
* @end deftypefun
@*/
int
/*@
* @deftypefun int jit_debugger_is_running (jit_debugger_t @var{dbg}, jit_debugger_thread_id_t @var{thread})
* Determine if a particular thread is currently running (non-zero) or
* stopped (zero).
* @end deftypefun
@*/
int
/*@
* @deftypefun void jit_debugger_run (jit_debugger_t @var{dbg}, jit_debugger_thread_id_t @var{thread})
* Start the specified thread running, or continue from the last breakpoint.
*
* This function, and the others that follow, sends a request to the specified
* thread and then returns to the caller immediately.
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_step (jit_debugger_t @var{dbg}, jit_debugger_thread_id_t @var{thread})
* Step over a single line of code. If the line performs a method call,
* then this will step into the call. The request will be ignored if
* the thread is currently running.
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_next (jit_debugger_t @var{dbg}, jit_debugger_thread_id_t @var{thread})
* Step over a single line of code but do not step into method calls.
* The request will be ignored if the thread is currently running.
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_finish (jit_debugger_t @var{dbg}, jit_debugger_thread_id_t @var{thread})
* Keep running until the end of the current function. The request will
* be ignored if the thread is currently running.
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_break (jit_debugger_t @var{dbg})
* Force an explicit user breakpoint at the current location within the
* current thread. Control returns to the caller when the debugger
* calls one of the above "run" or "step" functions in another thread.
* @end deftypefun
@*/
void
/*@
* @deftypefun void jit_debugger_quit (jit_debugger_t @var{dbg})
* Sends a request to the thread that called @code{jit_debugger_wait_event}
* indicating that the debugger should quit.
* @end deftypefun
@*/
void
/*@
* @deftypefun jit_debugger_hook_func jit_debugger_set_hook (jit_context_t @var{context}, jit_debugger_hook_func @var{hook})
* Set a debugger hook on a JIT context. Returns the previous hook.
*
* Debug hooks are a very low-level breakpoint mechanism. Upon reaching each
* breakpoint in a function, a user-supplied hook function is called.
* It is up to the hook function to decide whether to stop execution
* or to ignore the breakpoint. The hook function has the following
* prototype:
*
* @example
* void hook(jit_function_t func, jit_nint data1, jit_nint data2);
* @end example
*
* The @code{func} argument indicates the function that the breakpoint
* occurred within. The @code{data1} and @code{data2} arguments are
* those supplied to @code{jit_insn_mark_breakpoint}. The debugger can use
* these values to indicate information about the breakpoint's type
* and location.
*
* Hook functions can be used for other purposes besides breakpoint
* debugging. For example, a program could be instrumented with hooks
* that tally up the number of times that each function is called,
* or which profile the amount of time spent in each function.
*
* By convention, @code{data1} values less than 10000 are intended for
* use by user-defined hook functions. Values of 10000 and greater are
* reserved for the full-blown debugger system described earlier.
* @end deftypefun
@*/
jit_debugger_hook_func
void