src/Pure/System/isabelle_process.ML
author wenzelm
Wed Nov 26 14:35:55 2014 +0100 (2014-11-26 ago)
changeset 59055 5a7157b8e870
parent 58850 1bb0ad7827b4
child 59056 cbe9563c03d1
permissions -rw-r--r--
more informative failure of protocol commands, with exception trace;
eliminated obsolete Runtime.TERMINATE (left-over from former 'exit' command);
wenzelm@30173
     1
(*  Title:      Pure/System/isabelle_process.ML
wenzelm@25528
     2
    Author:     Makarius
wenzelm@25528
     3
wenzelm@57916
     4
Isabelle process wrapper.
wenzelm@25528
     5
*)
wenzelm@25528
     6
wenzelm@25528
     7
signature ISABELLE_PROCESS =
wenzelm@25528
     8
sig
wenzelm@42897
     9
  val is_active: unit -> bool
wenzelm@46119
    10
  val protocol_command: string -> (string list -> unit) -> unit
wenzelm@52655
    11
  val reset_tracing: Document_ID.exec -> unit
wenzelm@43684
    12
  val crashes: exn list Synchronized.var
wenzelm@45029
    13
  val init_fifos: string -> string -> unit
wenzelm@45028
    14
  val init_socket: string -> unit
wenzelm@25528
    15
end;
wenzelm@25528
    16
wenzelm@31797
    17
structure Isabelle_Process: ISABELLE_PROCESS =
wenzelm@25528
    18
struct
wenzelm@25528
    19
wenzelm@42897
    20
(* print mode *)
wenzelm@25554
    21
wenzelm@25748
    22
val isabelle_processN = "isabelle_process";
wenzelm@25748
    23
wenzelm@42897
    24
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
wenzelm@42897
    25
wenzelm@26550
    26
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
wenzelm@28036
    27
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
wenzelm@25841
    28
wenzelm@25841
    29
wenzelm@46119
    30
(* protocol commands *)
wenzelm@38270
    31
wenzelm@38270
    32
local
wenzelm@38270
    33
wenzelm@43684
    34
val commands =
wenzelm@52800
    35
  Synchronized.var "Isabelle_Process.commands"
wenzelm@52800
    36
    (Symtab.empty: (string list -> unit) Symtab.table);
wenzelm@38270
    37
wenzelm@38270
    38
in
wenzelm@38270
    39
wenzelm@46119
    40
fun protocol_command name cmd =
wenzelm@43684
    41
  Synchronized.change commands (fn cmds =>
wenzelm@38270
    42
   (if not (Symtab.defined cmds name) then ()
wenzelm@52582
    43
    else warning ("Redefining Isabelle protocol command " ^ quote name);
wenzelm@43684
    44
    Symtab.update (name, cmd) cmds));
wenzelm@38270
    45
wenzelm@46119
    46
fun run_command name args =
wenzelm@43684
    47
  (case Symtab.lookup (Synchronized.value commands) name of
wenzelm@52582
    48
    NONE => error ("Undefined Isabelle protocol command " ^ quote name)
wenzelm@46119
    49
  | SOME cmd =>
wenzelm@59055
    50
      (print_exception_trace Runtime.exn_message Output.system_message (fn () => cmd args)
wenzelm@59055
    51
        handle _ (*sic!*) => error ("Isabelle protocol command failure: " ^ quote name)));
wenzelm@38270
    52
wenzelm@38270
    53
end;
wenzelm@38270
    54
wenzelm@38270
    55
wenzelm@50505
    56
(* restricted tracing messages *)
wenzelm@49647
    57
wenzelm@52105
    58
val tracing_messages =
wenzelm@52105
    59
  Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
wenzelm@49647
    60
wenzelm@52655
    61
fun reset_tracing exec_id =
wenzelm@52655
    62
  Synchronized.change tracing_messages (Inttab.delete_safe exec_id);
wenzelm@49647
    63
wenzelm@50505
    64
fun update_tracing () =
wenzelm@50911
    65
  (case Position.parse_id (Position.thread_data ()) of
wenzelm@49647
    66
    NONE => ()
wenzelm@52655
    67
  | SOME exec_id =>
wenzelm@50505
    68
      let
wenzelm@52560
    69
        val ok =
wenzelm@52105
    70
          Synchronized.change_result tracing_messages (fn tab =>
wenzelm@50505
    71
            let
wenzelm@52655
    72
              val n = the_default 0 (Inttab.lookup tab exec_id) + 1;
wenzelm@52105
    73
              val ok = n <= Options.default_int "editor_tracing_messages";
wenzelm@52655
    74
            in (ok, Inttab.update (exec_id, n) tab) end);
wenzelm@50505
    75
      in
wenzelm@50505
    76
        if ok then ()
wenzelm@50505
    77
        else
wenzelm@50505
    78
          let
wenzelm@50505
    79
            val (text, promise) = Active.dialog_text ();
wenzelm@50505
    80
            val _ =
wenzelm@50505
    81
              writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
wenzelm@51044
    82
                text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
wenzelm@50505
    83
            val m = Markup.parse_int (Future.join promise)
wenzelm@50505
    84
              handle Fail _ => error "Stopped";
wenzelm@50505
    85
          in
wenzelm@52105
    86
            Synchronized.change tracing_messages
wenzelm@52655
    87
              (Inttab.map_default (exec_id, 0) (fn k => k - m))
wenzelm@50505
    88
          end
wenzelm@50505
    89
      end);
wenzelm@49647
    90
wenzelm@49647
    91
wenzelm@52800
    92
(* output channels *)
wenzelm@28188
    93
wenzelm@52854
    94
val serial_props = Markup.serial_properties o serial;
wenzelm@52854
    95
wenzelm@48712
    96
fun init_channels channel =
wenzelm@28189
    97
  let
wenzelm@40134
    98
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
wenzelm@40134
    99
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
wenzelm@40134
   100
wenzelm@52584
   101
    val msg_channel = Message_Channel.make channel;
wenzelm@52580
   102
wenzelm@52800
   103
    fun message name props body =
wenzelm@52800
   104
      Message_Channel.send msg_channel (Message_Channel.message name props body);
wenzelm@52800
   105
wenzelm@52854
   106
    fun standard_message props name body =
wenzelm@56333
   107
      if forall (fn s => s = "") body then ()
wenzelm@52580
   108
      else
wenzelm@57913
   109
        let
wenzelm@57913
   110
          val props' =
wenzelm@57913
   111
            (case (Properties.defined props Markup.idN, Position.get_id (Position.thread_data ())) of
wenzelm@57913
   112
              (false, SOME id') => props @ [(Markup.idN, id')]
wenzelm@57913
   113
            | _ => props);
wenzelm@57913
   114
        in message name props' body end;
wenzelm@28044
   115
  in
wenzelm@55387
   116
    Output.status_fn := standard_message [] Markup.statusN;
wenzelm@55387
   117
    Output.report_fn := standard_message [] Markup.reportN;
wenzelm@55387
   118
    Output.result_fn :=
wenzelm@52854
   119
      (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s);
wenzelm@55387
   120
    Output.writeln_fn := (fn s => standard_message (serial_props ()) Markup.writelnN s);
wenzelm@55387
   121
    Output.tracing_fn :=
wenzelm@52854
   122
      (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s));
wenzelm@55387
   123
    Output.warning_fn := (fn s => standard_message (serial_props ()) Markup.warningN s);
wenzelm@55387
   124
    Output.error_message_fn :=
wenzelm@52854
   125
      (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s);
wenzelm@57975
   126
    Output.system_message_fn := message Markup.systemN [];
wenzelm@55387
   127
    Output.protocol_message_fn := message Markup.protocolN;
wenzelm@56333
   128
    message Markup.initN [] [Session.welcome ()];
wenzelm@52584
   129
    msg_channel
wenzelm@28044
   130
  end;
wenzelm@25841
   131
wenzelm@25841
   132
wenzelm@39234
   133
(* protocol loop -- uninterruptible *)
wenzelm@38270
   134
wenzelm@43684
   135
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   136
wenzelm@38270
   137
local
wenzelm@38270
   138
wenzelm@38270
   139
fun recover crash =
wenzelm@43684
   140
  (Synchronized.change crashes (cons crash);
wenzelm@57975
   141
    Output.physical_stderr
wenzelm@57975
   142
      "Recovered from Isabelle process crash -- see also Isabelle_Process.crashes\n");
wenzelm@38270
   143
wenzelm@45029
   144
fun read_chunk channel len =
wenzelm@38270
   145
  let
wenzelm@38270
   146
    val n =
wenzelm@38270
   147
      (case Int.fromString len of
wenzelm@38270
   148
        SOME n => n
wenzelm@52799
   149
      | NONE => error ("Isabelle process: malformed header " ^ quote len));
wenzelm@45029
   150
    val chunk = System_Channel.inputN channel n;
wenzelm@52799
   151
    val i = size chunk;
wenzelm@38270
   152
  in
wenzelm@52799
   153
    if i <> n then
wenzelm@52799
   154
      error ("Isabelle process: bad chunk (unexpected EOF after " ^
wenzelm@52799
   155
        string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
wenzelm@52799
   156
    else chunk
wenzelm@38270
   157
  end;
wenzelm@38270
   158
wenzelm@45029
   159
fun read_command channel =
wenzelm@59055
   160
  System_Channel.input_line channel
wenzelm@59055
   161
  |> Option.map (fn line => map (read_chunk channel) (space_explode "," line));
wenzelm@38270
   162
wenzelm@54649
   163
fun task_context e =
wenzelm@54649
   164
  Future.task_context "Isabelle_Process.loop" (Future.new_group NONE) e ();
wenzelm@52559
   165
wenzelm@38270
   166
in
wenzelm@38270
   167
wenzelm@45029
   168
fun loop channel =
wenzelm@59055
   169
  let
wenzelm@59055
   170
    val continue =
wenzelm@59055
   171
      (case read_command channel of
wenzelm@59055
   172
        NONE => false
wenzelm@59055
   173
      | SOME [] => (Output.system_message "Isabelle process: no input"; true)
wenzelm@59055
   174
      | SOME (name :: args) => (task_context (fn () => run_command name args); true))
wenzelm@59055
   175
      handle exn => (Runtime.exn_system_message exn handle crash => recover crash; true);
wenzelm@52770
   176
  in
wenzelm@52770
   177
    if continue then loop channel
wenzelm@53192
   178
    else (Future.shutdown (); Execution.reset (); ())
wenzelm@52770
   179
  end;
wenzelm@38270
   180
wenzelm@38270
   181
end;
wenzelm@38270
   182
wenzelm@38270
   183
wenzelm@25554
   184
(* init *)
wenzelm@25554
   185
wenzelm@50715
   186
val default_modes1 =
wenzelm@50715
   187
  [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
wenzelm@50715
   188
val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
wenzelm@49566
   189
wenzelm@52578
   190
val init = uninterruptible (fn _ => fn rendezvous =>
wenzelm@38253
   191
  let
wenzelm@53212
   192
    val _ = SHA1_Samples.test ()
wenzelm@53212
   193
      handle exn as Fail msg => (Output.physical_stderr (msg ^ "\n"); reraise exn);
wenzelm@46548
   194
    val _ = Output.physical_stderr Symbol.STX;
wenzelm@39528
   195
wenzelm@49661
   196
    val _ = Printer.show_markup_default := true;
wenzelm@39528
   197
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   198
    val _ =
wenzelm@43671
   199
      Unsynchronized.change print_mode
wenzelm@49566
   200
        (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
wenzelm@39528
   201
wenzelm@45029
   202
    val channel = rendezvous ();
wenzelm@52584
   203
    val msg_channel = init_channels channel;
wenzelm@52111
   204
    val _ = Session.init_protocol_handlers ();
wenzelm@58848
   205
    val _ = loop channel;
wenzelm@52584
   206
  in Message_Channel.shutdown msg_channel end);
wenzelm@25528
   207
wenzelm@45029
   208
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
wenzelm@45029
   209
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
wenzelm@45028
   210
wenzelm@25528
   211
end;
wenzelm@39528
   212