11external is_process_alive : int -> bool = " olly_is_process_alive"
2-
3- (* On Windows, Unix.create_process_env returns a process HANDLE, not a PID.
4- This C stub calls GetProcessId(handle) on Windows to get the real PID.
5- On Unix it's the identity function since the handle IS the PID. *)
62external get_process_id : int -> int = " olly_get_process_id"
73
84let lost_events ring_id num =
@@ -13,19 +9,21 @@ type subprocess = {
139 cursor : Runtime_events .cursor ;
1410 close : unit -> unit ;
1511 pid : int ;
16- (* Terminate the process and wait for it to fully exit. Safe to call
17- more than once and safe to call after natural exit. *)
18- kill : unit -> unit ;
1912}
2013
14+
2115type runtime_events_config = { log_wsize : int option ; dir : string option }
2216type exec_config = Attach of string * int | Execute of string list
2317
2418(* Raised by exec_process to indicate various unrecoverable failures. *)
2519exception Fail of string
2620
21+ (* Function to kill a spawned process. Only the spawner can terminate
22+ the child. *)
23+ type kill = unit -> unit
24+
2725let exec_process (config : runtime_events_config ) (args : string list ) :
28- subprocess =
26+ subprocess * kill =
2927 if not (List. length args > 0 ) then
3028 raise (Fail (Printf. sprintf " no executable provided for exec_process" ));
3129
@@ -88,7 +86,7 @@ let exec_process (config : runtime_events_config) (args : string list) :
8886 in
8987 let child_pid = get_process_id child_handle in
9088 (* OCaml's Unix module on Windows uses the value returned by
91- create_process (a HANDLE) as its process identifier — both Unix.kill
89+ create_process (a HANDLE) as its process identifier, both Unix.kill
9290 and Unix.waitpid expect that value, NOT the real OS PID from
9391 GetProcessId. On Unix the handle and PID coincide.
9492
@@ -160,7 +158,7 @@ let exec_process (config : runtime_events_config) (args : string list) :
160158 try Unix. unlink ring_file with Unix. Unix_error _ -> ()
161159 end
162160 in
163- { alive; cursor; close; pid = child_pid; kill }
161+ ( { alive; cursor; close; pid = child_pid }, kill)
164162
165163let attach_process (dir : string ) (pid : int ) : subprocess =
166164 (* Check the target process exists before attempting to attach *)
@@ -187,25 +185,11 @@ let attach_process (dir : string) (pid : int) : subprocess =
187185 in
188186 let alive () = is_process_alive pid
189187 and close () = Runtime_events. free_cursor cursor in
190- (* Best-effort: we did not create the process, so we have no handle.
191- On Unix this kills via SIGKILL. On Windows this is effectively a
192- no-op because OCaml's Unix.kill expects the value returned by
193- create_process (a HANDLE), not an arbitrary PID — terminating an
194- attached-to process there would need a Win32 OpenProcess +
195- TerminateProcess C stub. *)
196- let kill () =
197- (try Unix. kill pid Sys. sigkill
198- with Unix. Unix_error _ | Invalid_argument _ -> () );
199- let deadline = Unix. gettimeofday () +. 2.0 in
200- while is_process_alive pid && Unix. gettimeofday () < deadline do
201- Unix. sleepf 0.02
202- done
203- in
204- { alive; cursor; close; pid; kill }
188+ { alive; cursor; close; pid }
205189
206190let launch_process config (exec_args : exec_config ) : subprocess =
207191 match exec_args with
208- | Execute argsl -> exec_process config argsl
192+ | Execute argsl -> fst ( exec_process config argsl)
209193 | Attach (dir , pid ) -> attach_process dir pid
210194
211195let interrupted = Atomic. make false
0 commit comments