src/Pure/Tools/debugger.ML
author wenzelm
Fri, 17 Mar 2017 09:33:58 +0100
changeset 65282 f4c5f10829a0
parent 65222 fb8253564483
child 67381 146757999c8d
permissions -rw-r--r--
clarified name;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Tools/debugger.ML
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     3
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     4
Interactive debugger for Isabelle/ML.
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     5
*)
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     6
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
     7
signature DEBUGGER =
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
     8
sig
60834
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
     9
  val writeln_message: string -> unit
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    10
  val warning_message: string -> unit
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    11
  val error_message: string -> unit
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    12
end;
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    13
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    14
structure Debugger: DEBUGGER =
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    15
struct
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    16
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
    17
(** global state **)
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
    18
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
    19
(* output messages *)
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    20
60834
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    21
fun output_message kind msg =
60864
20cfa048fe7c suppress empty messages as usual;
wenzelm
parents: 60862
diff changeset
    22
  if msg = "" then ()
20cfa048fe7c suppress empty messages as usual;
wenzelm
parents: 60862
diff changeset
    23
  else
20cfa048fe7c suppress empty messages as usual;
wenzelm
parents: 60862
diff changeset
    24
    Output.protocol_message
61556
0d4ee4168e41 clarified modules;
wenzelm
parents: 60956
diff changeset
    25
      (Markup.debugger_output (Standard_Thread.the_name ()))
60864
20cfa048fe7c suppress empty messages as usual;
wenzelm
parents: 60862
diff changeset
    26
      [Markup.markup (kind, Markup.serial_properties (serial ())) msg];
60834
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    27
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    28
val writeln_message = output_message Markup.writelnN;
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    29
val warning_message = output_message Markup.warningN;
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    30
val error_message = output_message Markup.errorN;
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    31
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    32
fun error_wrapper e = e ()
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    33
  handle exn =>
62505
9e2a65912111 clarified modules;
wenzelm
parents: 62504
diff changeset
    34
    if Exn.is_interrupt exn then Exn.reraise exn
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    35
    else error_message (Runtime.exn_message exn);
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    36
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    37
60888
35d85fd89fc1 eliminated cancel operation: disrupts normal evaluation of thread;
wenzelm
parents: 60887
diff changeset
    38
(* thread input *)
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    39
60888
35d85fd89fc1 eliminated cancel operation: disrupts normal evaluation of thread;
wenzelm
parents: 60887
diff changeset
    40
val thread_input =
60891
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    41
  Synchronized.var "Debugger.state" (NONE: string list Queue.T Symtab.table option);
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    42
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    43
fun init_input () = Synchronized.change thread_input (K (SOME Symtab.empty));
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    44
fun exit_input () = Synchronized.change thread_input (K NONE);
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    45
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    46
fun input thread_name msg =
60891
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    47
  if null msg then error "Empty input"
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    48
  else
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    49
    Synchronized.change thread_input
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    50
      (Option.map (Symtab.map_default (thread_name, Queue.empty) (Queue.enqueue msg)));
60887
9d8b244234ab register thread such that cancel works;
wenzelm
parents: 60885
diff changeset
    51
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    52
fun get_input thread_name =
60891
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    53
  Synchronized.guarded_access thread_input
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    54
    (fn NONE => SOME ([], NONE)
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    55
      | SOME input =>
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    56
          (case Symtab.lookup input thread_name of
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    57
            NONE => NONE
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    58
          | SOME queue =>
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    59
              let
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    60
                val (msg, queue') = Queue.dequeue queue;
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    61
                val input' =
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    62
                  if Queue.is_empty queue' then Symtab.delete_safe thread_name input
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    63
                  else Symtab.update (thread_name, queue') input;
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
    64
              in SOME (msg, SOME input') end));
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    65
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    66
60932
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    67
(* global break *)
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    68
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    69
local
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    70
  val break = Synchronized.var "Debugger.break" false;
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    71
in
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    72
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    73
fun is_break () = Synchronized.value break;
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    74
fun set_break b = Synchronized.change break (K b);
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    75
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    76
end;
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    77
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
    78
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
    79
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
    80
(** thread state **)
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
    81
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
    82
(* stack frame during debugging *)
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    83
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
    84
val debugging_var = Thread_Data.var () : PolyML.DebuggerInterface.debugState list Thread_Data.var;
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    85
62889
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
    86
fun get_debugging () = the_default [] (Thread_Data.get debugging_var);
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    87
val is_debugging = not o null o get_debugging;
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    88
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    89
fun with_debugging e =
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
    90
  Thread_Data.setmp debugging_var (SOME (PolyML.DebuggerInterface.debugState (Thread.self ()))) e ();
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    91
60858
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    92
fun the_debug_state thread_name index =
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    93
  (case get_debugging () of
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    94
    [] => error ("Missing debugger information for thread " ^ quote thread_name)
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    95
  | states =>
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    96
      if index < 0 orelse index >= length states then
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    97
        error ("Bad debugger stack index " ^ signed_string_of_int index ^ " for thread " ^
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    98
          quote thread_name)
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
    99
      else nth states index);
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
   100
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
   101
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   102
(* flags for single-stepping *)
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   103
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   104
datatype stepping = Stepping of bool * int;  (*stepping enabled, stack depth limit*)
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   105
62889
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   106
val stepping_var = Thread_Data.var () : stepping Thread_Data.var;
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   107
62889
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   108
fun get_stepping () = the_default (Stepping (false, ~1)) (Thread_Data.get stepping_var);
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   109
fun put_stepping stepping = Thread_Data.put stepping_var (SOME (Stepping stepping));
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   110
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   111
fun is_stepping () =
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   112
  let
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   113
    val stack = PolyML.DebuggerInterface.debugState (Thread.self ());
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   114
    val Stepping (stepping, depth) = get_stepping ();
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   115
  in stepping andalso (depth < 0 orelse length stack <= depth) end;
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   116
60931
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   117
fun continue () = put_stepping (false, ~1);
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   118
fun step () = put_stepping (true, ~1);
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   119
fun step_over () = put_stepping (true, length (get_debugging ()));
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   120
fun step_out () = put_stepping (true, length (get_debugging ()) - 1);
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   121
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   122
60935
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   123
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   124
(** eval ML **)
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   125
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   126
local
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   127
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   128
val context_attempts =
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   129
  map ML_Lex.read
62889
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   130
   ["Context.put_generic_context (SOME (Context.Theory ML_context))",
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   131
    "Context.put_generic_context (SOME (Context.Proof ML_context))",
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   132
    "Context.put_generic_context (SOME ML_context)"];
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   133
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   134
fun evaluate {SML, verbose} =
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   135
  ML_Context.eval
62902
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62891
diff changeset
   136
    {SML = SML, exchange = false, redirect = false, verbose = verbose,
60956
10d463883dc2 explicit debug flag for ML compiler;
wenzelm
parents: 60935
diff changeset
   137
      debug = SOME false, writeln = writeln_message, warning = warning_message}
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   138
    Position.none;
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   139
60935
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   140
fun eval_setup thread_name index SML context =
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   141
  context
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   142
  |> Context_Position.set_visible_generic false
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   143
  |> ML_Env.add_name_space {SML = SML}
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   144
      (PolyML.DebuggerInterface.debugNameSpace (the_debug_state thread_name index));
60935
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   145
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   146
fun eval_context thread_name index SML toks =
60858
7bf2188a0998 evaluate ML expressions within debugger context;
wenzelm
parents: 60857
diff changeset
   147
  let
62876
507c90523113 clarified modules -- simplified bootstrap;
wenzelm
parents: 62873
diff changeset
   148
    val context = Context.the_generic_context ();
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   149
    val context1 =
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   150
      if SML orelse forall (fn Antiquote.Text tok => ML_Lex.is_improper tok | _ => false) toks
60935
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   151
      then context
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   152
      else
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   153
        let
60935
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   154
          val context' = context
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   155
            |> eval_setup thread_name index SML
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   156
            |> ML_Context.exec (fn () =>
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   157
                evaluate {SML = SML, verbose = true} (ML_Lex.read "val ML_context = " @ toks));
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   158
          fun try_exec toks =
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   159
            try (ML_Context.exec (fn () => evaluate {SML = false, verbose = false} toks)) context';
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   160
        in
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   161
          (case get_first try_exec context_attempts of
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   162
            SOME context2 => context2
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   163
          | NONE => error "Malformed context: expected type theory, Proof.context, Context.generic")
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   164
        end;
60935
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   165
  in context1 |> eval_setup thread_name index SML end;
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   166
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   167
in
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   168
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   169
fun eval thread_name index SML txt1 txt2 =
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   170
  let
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   171
    val (toks1, toks2) = apply2 (ML_Lex.read_source SML o Input.string) (txt1, txt2);
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   172
    val context = eval_context thread_name index SML toks1;
62889
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   173
  in Context.setmp_generic_context (SOME context) (evaluate {SML = SML, verbose = true}) toks2 end;
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   174
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   175
fun print_vals thread_name index SML txt =
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   176
  let
60935
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   177
    val toks = ML_Lex.read_source SML (Input.string txt)
441c03582afa proper setup of evaluation context;
wenzelm
parents: 60932
diff changeset
   178
    val context = eval_context thread_name index SML toks;
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   179
    val space = PolyML.DebuggerInterface.debugNameSpace (the_debug_state thread_name index);
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   180
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   181
    fun print x =
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 62516
diff changeset
   182
      Pretty.from_polyml
62934
6e3fb0aa857a tuned signature;
wenzelm
parents: 62923
diff changeset
   183
        (PolyML.NameSpace.Values.printWithType
6e3fb0aa857a tuned signature;
wenzelm
parents: 62923
diff changeset
   184
          (x, FixedInt.fromInt (ML_Print_Depth.get_print_depth ()), SOME space));
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   185
    fun print_all () =
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   186
      #allVal (PolyML.DebuggerInterface.debugLocalNameSpace (the_debug_state thread_name index)) ()
60924
610794dff23c tuned signature, in accordance to sortBy in Scala;
wenzelm
parents: 60904
diff changeset
   187
      |> sort_by #1 |> map (Pretty.item o single o print o #2)
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   188
      |> Pretty.chunks |> Pretty.string_of |> writeln_message;
62889
99c7f31615c2 clarified modules;
wenzelm
parents: 62878
diff changeset
   189
  in Context.setmp_generic_context (SOME context) print_all () end;
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   190
097afdd8a2fd eval ML context;
wenzelm
parents: 60858
diff changeset
   191
end;
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   192
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   193
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   194
60891
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
   195
(** debugger loop **)
60857
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   196
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   197
local
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   198
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   199
fun debugger_state thread_name =
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   200
  Output.protocol_message (Markup.debugger_state thread_name)
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   201
   [get_debugging ()
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   202
    |> map (fn st =>
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62663
diff changeset
   203
      (Position.properties_of
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   204
        (Exn_Properties.position_of_polyml_location (PolyML.DebuggerInterface.debugLocation st)),
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   205
       PolyML.DebuggerInterface.debugFunction st))
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   206
    |> let open XML.Encode in list (pair properties string) end
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   207
    |> YXML.string_of_body];
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   208
60857
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   209
fun debugger_command thread_name =
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   210
  (case get_input thread_name of
60931
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   211
    [] => (continue (); false)
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   212
  | ["continue"] => (continue (); false)
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   213
  | ["step"] => (step (); false)
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   214
  | ["step_over"] => (step_over (); false)
f4bc0400bd15 tuned signature;
wenzelm
parents: 60924
diff changeset
   215
  | ["step_out"] => (step_out (); false)
60857
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   216
  | ["eval", index, SML, txt1, txt2] =>
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   217
     (error_wrapper (fn () =>
63806
c54a53ef1873 clarified modules;
wenzelm
parents: 62937
diff changeset
   218
        eval thread_name (Value.parse_int index) (Value.parse_bool SML) txt1 txt2); true)
60897
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   219
  | ["print_vals", index, SML, txt] =>
7aad4be8a48e print values for stack entry;
wenzelm
parents: 60891
diff changeset
   220
     (error_wrapper (fn () =>
63806
c54a53ef1873 clarified modules;
wenzelm
parents: 62937
diff changeset
   221
        print_vals thread_name (Value.parse_int index) (Value.parse_bool SML) txt); true)
60857
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   222
  | bad =>
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   223
     (Output.system_message
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   224
        ("Debugger: bad input " ^ ML_Syntax.print_list ML_Syntax.print_string bad); true));
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   225
60889
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   226
in
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   227
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   228
fun debugger_loop thread_name =
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents: 62902
diff changeset
   229
  Thread_Attributes.uninterruptible (fn restore_attributes => fn () =>
60885
d8d51f956f05 report final debugger_state more robustly, e.g. after interrupt;
wenzelm
parents: 60884
diff changeset
   230
    let
d8d51f956f05 report final debugger_state more robustly, e.g. after interrupt;
wenzelm
parents: 60884
diff changeset
   231
      fun loop () =
d8d51f956f05 report final debugger_state more robustly, e.g. after interrupt;
wenzelm
parents: 60884
diff changeset
   232
        (debugger_state thread_name; if debugger_command thread_name then loop () else ());
d8d51f956f05 report final debugger_state more robustly, e.g. after interrupt;
wenzelm
parents: 60884
diff changeset
   233
      val result = Exn.capture (restore_attributes with_debugging) loop;
d8d51f956f05 report final debugger_state more robustly, e.g. after interrupt;
wenzelm
parents: 60884
diff changeset
   234
      val _ = debugger_state thread_name;
d8d51f956f05 report final debugger_state more robustly, e.g. after interrupt;
wenzelm
parents: 60884
diff changeset
   235
    in Exn.release result end) ();
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   236
60857
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   237
end;
4c18d8e4fe14 clarified debugger loop;
wenzelm
parents: 60856
diff changeset
   238
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   239
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   240
878e32cf3131 more single stepping;
wenzelm
parents: 60864
diff changeset
   241
(** protocol commands **)
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   242
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   243
val _ =
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   244
  Isabelle_Process.protocol_command "Debugger.init"
60889
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   245
    (fn [] =>
60891
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
   246
     (init_input ();
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   247
      PolyML.DebuggerInterface.setOnBreakPoint
60889
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   248
        (SOME (fn (_, break) =>
60932
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
   249
          if not (is_debugging ()) andalso (! break orelse is_break () orelse is_stepping ())
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
   250
          then
61556
0d4ee4168e41 clarified modules;
wenzelm
parents: 60956
diff changeset
   251
            (case Standard_Thread.get_name () of
60889
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   252
              SOME thread_name => debugger_loop thread_name
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   253
            | NONE => ())
60891
89b7c84b0480 vacuous input means continue, e.g. after exit;
wenzelm
parents: 60889
diff changeset
   254
          else ()))));
60889
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   255
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   256
val _ =
7f210887cc4e init/exit depending on active debugger panels;
wenzelm
parents: 60888
diff changeset
   257
  Isabelle_Process.protocol_command "Debugger.exit"
62937
d5e7a76ec1a6 clarified modules;
wenzelm
parents: 62934
diff changeset
   258
    (fn [] => (PolyML.DebuggerInterface.setOnBreakPoint NONE; exit_input ()));
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   259
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   260
val _ =
60932
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
   261
  Isabelle_Process.protocol_command "Debugger.break"
63806
c54a53ef1873 clarified modules;
wenzelm
parents: 62937
diff changeset
   262
    (fn [b] => set_break (Value.parse_bool b));
60932
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
   263
13ee73f57c85 allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents: 60931
diff changeset
   264
val _ =
60878
1f0d2bbcf38b added action to toggle breakpoints (on editor side);
wenzelm
parents: 60871
diff changeset
   265
  Isabelle_Process.protocol_command "Debugger.breakpoint"
60880
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   266
    (fn [node_name, id0, breakpoint0, breakpoint_state0] =>
60878
1f0d2bbcf38b added action to toggle breakpoints (on editor side);
wenzelm
parents: 60871
diff changeset
   267
      let
63806
c54a53ef1873 clarified modules;
wenzelm
parents: 62937
diff changeset
   268
        val id = Value.parse_int id0;
c54a53ef1873 clarified modules;
wenzelm
parents: 62937
diff changeset
   269
        val breakpoint = Value.parse_int breakpoint0;
c54a53ef1873 clarified modules;
wenzelm
parents: 62937
diff changeset
   270
        val breakpoint_state = Value.parse_bool breakpoint_state0;
60880
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   271
63806
c54a53ef1873 clarified modules;
wenzelm
parents: 62937
diff changeset
   272
        fun err () = error ("Bad exec for command " ^ Value.print_int id);
60880
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   273
      in
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   274
        (case Document.command_exec (Document.state ()) node_name id of
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   275
          SOME (eval, _) =>
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   276
            if Command.eval_finished eval then
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   277
              let
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   278
                val st = Command.eval_result_state eval;
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   279
                val ctxt = Toplevel.presentation_context_of st
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   280
                  handle Toplevel.UNDEF => err ();
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   281
              in
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   282
                (case ML_Env.get_breakpoint (Context.Proof ctxt) breakpoint of
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   283
                  SOME (b, _) => b := breakpoint_state
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   284
                | NONE => err ())
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   285
              end
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   286
            else err ()
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   287
        | NONE => err ())
fa958e24ff24 set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents: 60878
diff changeset
   288
      end);
60878
1f0d2bbcf38b added action to toggle breakpoints (on editor side);
wenzelm
parents: 60871
diff changeset
   289
1f0d2bbcf38b added action to toggle breakpoints (on editor side);
wenzelm
parents: 60871
diff changeset
   290
val _ =
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   291
  Isabelle_Process.protocol_command "Debugger.input"
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   292
    (fn thread_name :: msg => input thread_name msg);
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   293
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   294
end;