src/Pure/Tools/debugger.ML
author wenzelm
Thu, 06 Aug 2015 17:40:05 +0200
changeset 60856 eb21ae05ec43
parent 60842 5510c8444bc4
child 60857 4c18d8e4fe14
permissions -rw-r--r--
clarified thread state; support for eval operation;
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
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    17
(* messages *)
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    18
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    19
val _ = Session.protocol_handler "isabelle.Debugger$Handler";
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    20
60834
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    21
fun output_message kind msg =
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    22
  Output.protocol_message
60834
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    23
    (Markup.debugger_output (Simple_Thread.the_name ()))
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    24
    [Markup.markup (kind, Markup.serial_properties (serial ())) msg];
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    25
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    26
val writeln_message = output_message Markup.writelnN;
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    27
val warning_message = output_message Markup.warningN;
781f1168d31e maintain debugger output messages;
wenzelm
parents: 60830
diff changeset
    28
val error_message = output_message Markup.errorN;
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    29
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    30
fun error_wrapper e = e ()
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    31
  handle exn =>
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    32
    if Exn.is_interrupt exn then reraise exn
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    33
    else error_message (Runtime.exn_message exn);
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    34
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    35
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    36
(* global state *)
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    37
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    38
datatype state =
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    39
  State of {
60829
4b16b778ce0d clarified thread name;
wenzelm
parents: 60765
diff changeset
    40
    threads: Thread.thread Symtab.table,  (*thread name ~> thread*)
4b16b778ce0d clarified thread name;
wenzelm
parents: 60765
diff changeset
    41
    input: string list Queue.T Symtab.table  (*thread name ~> input queue*)
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    42
  };
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    43
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    44
fun make_state (threads, input) = State {threads = threads, input = input};
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    45
val init_state = make_state (Symtab.empty, Symtab.empty);
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    46
fun map_state f (State {threads, input}) = make_state (f (threads, input));
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    47
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    48
val global_state = Synchronized.var "Debugger.state" init_state;
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    49
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    50
fun cancel thread_name =
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    51
  Synchronized.change global_state (tap (fn State {threads, ...} =>
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    52
    (case Symtab.lookup threads thread_name of
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    53
      NONE => ()
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    54
    | SOME thread => Simple_Thread.interrupt_unsynchronized thread)));
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    55
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    56
fun input thread_name msg =
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    57
  Synchronized.change global_state (map_state (fn (threads, input) =>
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    58
    let val input' = Symtab.map_default (thread_name, Queue.empty) (Queue.enqueue msg) input;
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    59
    in (threads, input') end));
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    60
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    61
fun get_input thread_name =
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    62
  Synchronized.guarded_access global_state (fn State {threads, input} =>
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    63
    (case Symtab.lookup input thread_name of
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    64
      NONE => NONE
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    65
    | SOME queue =>
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    66
        let
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    67
          val (msg, queue') = Queue.dequeue queue;
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    68
          val input' =
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    69
            if Queue.is_empty queue' then Symtab.delete_safe thread_name input
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    70
            else Symtab.update (thread_name, queue') input;
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    71
        in SOME (msg, make_state (threads, input')) end));
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    72
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    73
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    74
(* thread state *)
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    75
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    76
local
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    77
  val tag = Universal.tag () : ML_Debugger.state list Universal.tag;
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    78
in
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    79
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    80
fun get_debugging () = the_default [] (Thread.getLocal tag);
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    81
val is_debugging = not o null o get_debugging;
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    82
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    83
fun with_debugging e =
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    84
  setmp_thread_data tag (get_debugging ()) (ML_Debugger.state (Thread.self ())) e ();
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    85
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    86
end;
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    87
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    88
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    89
(* eval ML *)
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    90
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    91
fun eval opt_index SML context expression =  (* FIXME *)
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    92
  writeln_message ("context = " ^ context ^ "\nexpression = " ^ expression);
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    93
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    94
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    95
(* main entry point *)
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
    96
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    97
fun debugger_state thread_name =
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
    98
  Output.protocol_message (Markup.debugger_state thread_name)
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
    99
   [get_debugging ()
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   100
    |> map (fn st =>
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   101
      (Position.properties_of (Exn_Properties.position_of (ML_Debugger.debug_location st)),
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   102
       ML_Debugger.debug_function st))
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   103
    |> let open XML.Encode in list (pair properties string) end
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   104
    |> YXML.string_of_body];
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   105
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   106
fun debugger_loop thread_name =
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   107
  let
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   108
    fun loop () =
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   109
      (debugger_state thread_name;
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   110
       case get_input thread_name of
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   111
        ["continue"] => ()
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   112
      | ["eval", index, language, context, expression] =>
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   113
          (error_wrapper (fn () =>
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   114
            eval (try Markup.parse_int index) (language = "SML") context expression); loop ())
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   115
      | bad =>
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   116
         (Output.system_message
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   117
            ("Debugger: bad input " ^ ML_Syntax.print_list ML_Syntax.print_string bad);
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   118
          loop ()));
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   119
  in with_debugging loop; debugger_state thread_name end;
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   120
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   121
fun debugger cond =
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   122
  if is_debugging () orelse not (cond ()) orelse
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   123
    not (Options.default_bool @{system_option ML_debugger_active})
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   124
  then ()
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   125
  else
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   126
    (case Simple_Thread.get_name () of
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   127
      NONE => ()
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60842
diff changeset
   128
    | SOME thread_name => debugger_loop thread_name);
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   129
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   130
fun init () =
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   131
  ML_Debugger.on_breakpoint
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   132
    (SOME (fn (_, b) =>
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   133
      debugger (fn () => ! b orelse Options.default_bool @{system_option ML_debugger_stepping})));
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   134
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   135
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   136
(* protocol commands *)
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   137
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   138
val _ =
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   139
  Isabelle_Process.protocol_command "Debugger.init"
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   140
    (fn [] => init ());
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   141
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   142
val _ =
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   143
  Isabelle_Process.protocol_command "Debugger.cancel"
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   144
    (fn [thread_name] => cancel thread_name);
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   145
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   146
val _ =
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   147
  Isabelle_Process.protocol_command "Debugger.input"
60842
5510c8444bc4 protocol support for thread debugger state;
wenzelm
parents: 60834
diff changeset
   148
    (fn thread_name :: msg => input thread_name msg);
60765
e43e71a75838 support for ML debugger;
wenzelm
parents: 60749
diff changeset
   149
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   150
end;