| skipped 56 lines |
57 | 57 | | Ok(by_pid) |
58 | 58 | | } |
59 | 59 | | |
| 60 | + | fn find_inspection_port(ports: &[u16]) -> Option<u16> { |
| 61 | + | for port in ports { |
| 62 | + | if protocol::get_debug_url(*port).is_ok() { |
| 63 | + | return Some(*port); |
| 64 | + | } |
| 65 | + | } |
| 66 | + | None |
| 67 | + | } |
| 68 | + | |
60 | 69 | | fn enable_inspection_port(pid: u32) -> Result<u16, Error> { |
61 | | - | let port: u16; |
62 | 70 | | let ports_before = find_listening_ports_by_pid(pid) |
63 | 71 | | .map_err(|e| format!("could not find enumerate process open ports: {:?}", e))?; |
64 | 72 | | |
65 | | - | if ports_before.len() == 1 { |
66 | | - | // println!("inspection already enabled on port {}", ports_before[0]); |
67 | | - | port = ports_before[0]; |
68 | | - | } else { |
69 | | - | println!("sending SIGUSR1 to process {}", pid); |
| 73 | + | if let Some(port) = find_inspection_port(&ports_before) { |
| 74 | + | println!("inspection already enabled on port {}", port); |
| 75 | + | return Ok(port); |
| 76 | + | } |
70 | 77 | | |
71 | | - | nix::sys::signal::kill( |
72 | | - | nix::unistd::Pid::from_raw(pid as i32), |
73 | | - | nix::sys::signal::Signal::SIGUSR1, |
74 | | - | ) |
75 | | - | .map_err(|e| format!("could not send SIGUSR1 signal: {:?}", e))?; |
| 78 | + | println!("sending SIGUSR1 to process {}", pid); |
76 | 79 | | |
77 | | - | println!("waiting 3 seconds for the debugger to start ..."); |
| 80 | + | nix::sys::signal::kill( |
| 81 | + | nix::unistd::Pid::from_raw(pid as i32), |
| 82 | + | nix::sys::signal::Signal::SIGUSR1, |
| 83 | + | ) |
| 84 | + | .map_err(|e| format!("could not send SIGUSR1 signal: {:?}", e))?; |
78 | 85 | | |
79 | | - | // give the debugger some time to start |
80 | | - | std::thread::sleep(std::time::Duration::from_secs(3)); |
| 86 | + | println!("waiting 3 seconds for the debugger to start ..."); |
81 | 87 | | |
82 | | - | let ports_after = find_listening_ports_by_pid(pid) |
83 | | - | .map_err(|e| format!("could not find enumerate process open ports: {:?}", e))?; |
| 88 | + | // give the debugger some time to start |
| 89 | + | std::thread::sleep(std::time::Duration::from_secs(3)); |
84 | 90 | | |
85 | | - | let new_ports: Vec<_> = ports_after |
86 | | - | .iter() |
87 | | - | .filter(|port| !ports_before.contains(port)) |
88 | | - | .collect(); |
89 | | - | if new_ports.len() == 1 { |
90 | | - | port = *new_ports[0]; |
91 | | - | } else { |
92 | | - | return Err(format!( |
93 | | - | "could not infer inspection port, before={:?} after={:?}", |
94 | | - | ports_before, ports_after |
95 | | - | )); |
96 | | - | } |
| 91 | + | let ports_after = find_listening_ports_by_pid(pid) |
| 92 | + | .map_err(|e| format!("could not find enumerate process open ports: {:?}", e))?; |
| 93 | + | |
| 94 | + | if let Some(port) = find_inspection_port(&ports_after) { |
| 95 | + | return Ok(port); |
97 | 96 | | } |
98 | 97 | | |
99 | | - | Ok(port) |
| 98 | + | Err(format!( |
| 99 | + | "could not infer inspection port, before={:?} after={:?}", |
| 100 | + | ports_before, ports_after |
| 101 | + | )) |
100 | 102 | | } |
101 | 103 | | |
102 | 104 | | #[derive(Parser, Default, Debug, Clone)] |
| skipped 191 lines |