1// LLDB C++ API Test: Verify that when the Debugger stdin 2// is set to a FILE *, lldb can still successfully run a 3// python command in a stop hook. 4 5#include <errno.h> 6#include <mutex> 7#include <stdio.h> 8#include <string> 9#include <vector> 10 11%include_SB_APIs% 12 13#include "common.h" 14 15using namespace lldb; 16 17void test(SBDebugger &dbg, std::vector<std::string> args) { 18 // The problem we had was that when the thread that was 19 // waiting on input went into the call to 'read' it had 20 // the file handle lock. Then when the python interpreter 21 // Initialized itself to run the python command, it tried 22 // to flush the file channel, and that deadlocked. 23 // This only happens when Async is true, since otherwise 24 // the process event is handled on the I/O read thread, 25 // which sidestepped the problem. 26 dbg.SetAsync(true); 27 28 SBTarget target = dbg.CreateTarget(args.at(0).c_str()); 29 if (!target.IsValid()) 30 throw Exception("invalid target"); 31 32 SBBreakpoint breakpoint = target.BreakpointCreateByName("next"); 33 if (!breakpoint.IsValid()) 34 throw Exception("invalid breakpoint"); 35 36 SBCommandInterpreter interp = dbg.GetCommandInterpreter(); 37 SBCommandReturnObject result; 38 39 // Bring in the python command. We actually add two commands, 40 // one that runs in the stop hook and sets a variable when it 41 // runs, and one that reports out the variable so we can ensure 42 // that we did indeed run the stop hook. 43 const char *source_dir = "%SOURCE_DIR%"; 44 SBFileSpec script_spec(source_dir); 45 script_spec.AppendPathComponent("some_cmd.py"); 46 char path[PATH_MAX]; 47 script_spec.GetPath(path, PATH_MAX); 48 49 std::string import_command("command script import "); 50 import_command.append(path); 51 interp.HandleCommand(import_command.c_str(), result); 52 if (!result.Succeeded()) 53 throw Exception("Couldn't import %SOURCE_DIR%/some_cmd.py"); 54 55 SBProcess process = target.LaunchSimple(nullptr, nullptr, nullptr); 56 if (!process.IsValid()) 57 throw Exception("Couldn't launch process."); 58 if (process.GetState() != lldb::eStateStopped) 59 throw Exception("Process was not stopped"); 60 61 process.SetSelectedThreadByIndexID(0); 62 63 // Now add the stop hook: 64 interp.HandleCommand("target stop-hook add -o some-cmd", result); 65 if (!result.Succeeded()) 66 throw Exception("Couldn't add a stop hook."); 67 68 // Now switch the I/O over to a pipe, which will be handled by the 69 // NativeFile class: 70 int to_lldb_des[2]; 71 int pipe_result = pipe(to_lldb_des); 72 FILE *fh_lldb_in = fdopen(to_lldb_des[0], "r"); 73 FILE *fh_to_lldb = fdopen(to_lldb_des[1], "w"); 74 75 // We need to reset the handle before destroying the debugger 76 // or the same deadlock will stall exiting: 77 class Cleanup { 78 public: 79 Cleanup(SBDebugger dbg, int filedes[2]) : m_dbg(dbg) { 80 m_file = m_dbg.GetInputFileHandle(); 81 m_filedes[0] = filedes[0]; 82 m_filedes[1] = filedes[1]; 83 } 84 ~Cleanup() { 85 m_dbg.SetInputFileHandle(m_file, false); 86 close(m_filedes[0]); 87 close(m_filedes[1]); 88 } 89 90 private: 91 FILE *m_file; 92 SBDebugger m_dbg; 93 int m_filedes[2]; 94 }; 95 Cleanup cleanup(dbg, to_lldb_des); 96 97 dbg.SetInputFileHandle(fh_lldb_in, false); 98 99 // Now run the command interpreter. You have to pass true to 100 // start thread so we will run the I/O in a separate thread. 101 dbg.RunCommandInterpreter(false, true); 102 103 // Now issue a stepi, and fetch the running and stopped events: 104 fprintf(fh_to_lldb, "thread step-inst\n"); 105 106 SBEvent proc_event; 107 StateType state; 108 bool got_event; 109 110 got_event = dbg.GetListener().WaitForEventForBroadcaster( 111 100, process.GetBroadcaster(), proc_event); 112 if (!got_event) 113 throw Exception("Didn't get running event"); 114 state = SBProcess::GetStateFromEvent(proc_event); 115 if (state != eStateRunning) 116 throw Exception("Event wasn't a running event."); 117 118 got_event = dbg.GetListener().WaitForEventForBroadcaster( 119 100, process.GetBroadcaster(), proc_event); 120 if (!got_event) 121 throw Exception("Didn't get a stopped event"); 122 state = SBProcess::GetStateFromEvent(proc_event); 123 if (state != eStateStopped) 124 throw Exception("Event wasn't a stop event."); 125 126 // At this point the stop hook should have run. Check that: 127 interp.HandleCommand("report-cmd", result); 128 if (!result.Succeeded()) 129 throw Exception("Didn't actually call stop hook."); 130} 131