1 //===-- UnixLocalInferiorProcess.cpp - A Local process on a Unixy system --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides one implementation of the InferiorProcess class, which is
11 // designed to be used on unixy systems (those that support pipe, fork, exec,
14 // When the process is started, the debugger creates a pair of pipes, forks, and
15 // makes the child starts executing the program. The child executes the process
16 // with an IntrinsicLowering instance that turns debugger intrinsics into actual
19 // This target takes advantage of the fact that the Module* addresses in the
20 // parent and the Module* addresses in the child will be the same, due to the
21 // use of fork(). As such, global addresses looked up in the child can be sent
22 // over the pipe to the debugger.
24 //===----------------------------------------------------------------------===//
26 #include "llvm/Debugger/InferiorProcess.h"
27 #include "llvm/IntrinsicLowering.h"
28 #include "llvm/Constant.h"
29 #include "llvm/Module.h"
30 #include "llvm/ModuleProvider.h"
31 #include "llvm/Type.h"
32 #include "llvm/iOther.h"
33 #include "llvm/ExecutionEngine/GenericValue.h"
34 #include "llvm/ExecutionEngine/ExecutionEngine.h"
35 #include "Support/FileUtilities.h"
36 #include "Support/StringExtras.h"
38 #include <unistd.h> // Unix specific debugger support
39 #include <sys/types.h>
43 // runChild - Entry point for the child process.
44 static void runChild(Module *M, const std::vector<std::string> &Arguments,
45 const char * const *envp,
46 FDHandle ReadFD, FDHandle WriteFD);
48 //===----------------------------------------------------------------------===//
49 // Parent/Child Pipe Protocol
50 //===----------------------------------------------------------------------===//
52 // The parent/child communication protocol is designed to have the child process
53 // responding to requests that the debugger makes. Whenever the child process
54 // has stopped (due to a break point, single stepping, etc), the child process
55 // enters a message processing loop, where it reads and responds to commands
56 // until the parent decides that it wants to continue execution in some way.
58 // Whenever the child process stops, it notifies the debugger by sending an
59 // character over the wire.
63 /// LocationToken - Objects of this type are sent across the pipe from the
64 /// child to the parent to indicate where various stack frames are located.
65 struct LocationToken {
67 const GlobalVariable *File;
68 LocationToken(unsigned L = 0, unsigned C = 0, const GlobalVariable *F = 0)
69 : Line(L), Col(C), File(F) {}
73 // Once the debugger process has received the LocationToken, it can make
74 // requests of the child by sending one of the following enum values followed by
75 // any data required by that command. The child responds with data appropriate
79 /// CommandID - This enum defines all of the commands that the child process
80 /// can respond to. The actual expected data and responses are defined as the
81 /// enum values are defined.
84 //===------------------------------------------------------------------===//
85 // Execution commands - These are sent to the child to from the debugger to
86 // get it to do certain things.
89 // StepProgram: void->char - This command causes the program to continue
90 // execution, but stop as soon as it reaches another stoppoint.
93 // FinishProgram: FrameDesc*->char - This command causes the program to
94 // continue execution until the specified function frame returns.
97 // ContProgram: void->char - This command causes the program to continue
98 // execution, stopping at some point in the future.
101 // GetSubprogramDescriptor: FrameDesc*->GlobalValue* - This command returns
102 // the GlobalValue* descriptor object for the specified stack frame.
103 GetSubprogramDescriptor,
105 // GetParentFrame: FrameDesc*->FrameDesc* - This command returns the frame
106 // descriptor for the parent stack frame to the specified one, or null if
110 // GetFrameLocation - FrameDesc*->LocationToken - This command returns the
111 // location that a particular stack frame is stopped at.
114 // AddBreakpoint - LocationToken->unsigned - This command instructs the
115 // target to install a breakpoint at the specified location.
118 // RemoveBreakpoint - unsigned->void - This command instructs the target to
119 // remove a breakpoint.
127 //===----------------------------------------------------------------------===//
128 // Parent Process Code
129 //===----------------------------------------------------------------------===//
132 class IP : public InferiorProcess {
133 // ReadFD, WriteFD - The file descriptors to read/write to the inferior
135 FDHandle ReadFD, WriteFD;
137 // ChildPID - The unix PID of the child process we forked.
138 mutable pid_t ChildPID;
140 IP(Module *M, const std::vector<std::string> &Arguments,
141 const char * const *envp);
144 std::string getStatus() const;
146 /// Execution method implementations...
147 virtual void stepProgram();
148 virtual void finishProgram(void *Frame);
149 virtual void contProgram();
152 // Stack frame method implementations...
153 virtual void *getPreviousFrame(void *Frame) const;
154 virtual const GlobalVariable *getSubprogramDesc(void *Frame) const;
155 virtual void getFrameLocation(void *Frame, unsigned &LineNo,
157 const GlobalVariable *&SourceDesc) const;
159 // Breakpoint implementation methods
160 virtual unsigned addBreakpoint(unsigned LineNo, unsigned ColNo,
161 const GlobalVariable *SourceDesc);
162 virtual void removeBreakpoint(unsigned ID);
166 /// startChild - This starts up the child process, and initializes the
169 void startChild(Module *M, const std::vector<std::string> &Arguments,
170 const char * const *envp);
172 /// killChild - Kill or reap the child process. This throws the
173 /// InferiorProcessDead exception an exit code if the process had already
174 /// died, otherwise it just kills it and returns.
175 void killChild() const;
178 // Methods for communicating with the child process. If the child exits or
179 // dies while attempting to communicate with it, ChildPID is set to zero and
180 // an exception is thrown.
182 /// readFromChild - Low-level primitive to read some data from the child,
183 /// throwing an exception if it dies.
184 void readFromChild(void *Buffer, unsigned Size) const;
186 /// writeToChild - Low-level primitive to send some data to the child
187 /// process, throwing an exception if the child died.
188 void writeToChild(void *Buffer, unsigned Size) const;
190 /// sendCommand - Send a command token and the request data to the child.
192 void sendCommand(CommandID Command, void *Data, unsigned Size) const;
194 /// waitForStop - This method waits for the child process to reach a stop
200 // create - This is the factory method for the InferiorProcess class. Since
201 // there is currently only one subclass of InferiorProcess, we just define it
204 InferiorProcess::create(Module *M, const std::vector<std::string> &Arguments,
205 const char * const *envp) {
206 return new IP(M, Arguments, envp);
209 /// IP constructor - Create some pipes, them fork a child process. The child
210 /// process should start execution of the debugged program, but stop at the
211 /// first available opportunity.
212 IP::IP(Module *M, const std::vector<std::string> &Arguments,
213 const char * const *envp)
214 : InferiorProcess(M) {
216 // Start the child running...
217 startChild(M, Arguments, envp);
219 // Okay, we created the program and it is off and running. Wait for it to
223 } catch (InferiorProcessDead &IPD) {
224 throw "Error waiting for the child process to stop. "
225 "It exited with status " + itostr(IPD.getExitCode());
230 // If the child is still running, kill it.
231 if (!ChildPID) return;
236 /// getStatus - Return information about the unix process being debugged.
238 std::string IP::getStatus() const {
240 return "Unix target. ERROR: child process appears to be dead!\n";
242 return "Unix target: PID #" + utostr((unsigned)ChildPID) + "\n";
246 /// startChild - This starts up the child process, and initializes the
249 void IP::startChild(Module *M, const std::vector<std::string> &Arguments,
250 const char * const *envp) {
251 // Create the pipes. Make sure to immediately assign the returned file
252 // descriptors to FDHandle's so they get destroyed if an exception is thrown.
254 if (pipe(FDs)) throw "Error creating a pipe!";
255 FDHandle ChildReadFD(FDs[0]);
258 if (pipe(FDs)) throw "Error creating a pipe!";
260 FDHandle ChildWriteFD(FDs[1]);
262 // Fork off the child process.
263 switch (ChildPID = fork()) {
264 case -1: throw "Error forking child process!";
266 delete this; // Free parent pipe file descriptors
267 runChild(M, Arguments, envp, ChildReadFD, ChildWriteFD);
273 /// sendCommand - Send a command token and the request data to the child.
275 void IP::sendCommand(CommandID Command, void *Data, unsigned Size) const {
276 writeToChild(&Command, sizeof(Command));
277 writeToChild(Data, Size);
280 /// stepProgram - Implement the 'step' command, continuing execution until
281 /// the next possible stop point.
282 void IP::stepProgram() {
283 sendCommand(StepProgram, 0, 0);
287 /// finishProgram - Implement the 'finish' command, executing the program until
288 /// the current function returns to its caller.
289 void IP::finishProgram(void *Frame) {
290 sendCommand(FinishProgram, &Frame, sizeof(Frame));
294 /// contProgram - Implement the 'cont' command, continuing execution until
295 /// a breakpoint is encountered.
296 void IP::contProgram() {
297 sendCommand(ContProgram, 0, 0);
302 //===----------------------------------------------------------------------===//
303 // Stack manipulation methods
306 /// getPreviousFrame - Given the descriptor for the current stack frame,
307 /// return the descriptor for the caller frame. This returns null when it
308 /// runs out of frames.
309 void *IP::getPreviousFrame(void *Frame) const {
310 sendCommand(GetParentFrame, &Frame, sizeof(Frame));
311 readFromChild(&Frame, sizeof(Frame));
315 /// getSubprogramDesc - Return the subprogram descriptor for the current
317 const GlobalVariable *IP::getSubprogramDesc(void *Frame) const {
318 sendCommand(GetSubprogramDescriptor, &Frame, sizeof(Frame));
319 const GlobalVariable *Desc;
320 readFromChild(&Desc, sizeof(Desc));
324 /// getFrameLocation - This method returns the source location where each stack
325 /// frame is stopped.
326 void IP::getFrameLocation(void *Frame, unsigned &LineNo, unsigned &ColNo,
327 const GlobalVariable *&SourceDesc) const {
328 sendCommand(GetFrameLocation, &Frame, sizeof(Frame));
330 readFromChild(&Loc, sizeof(Loc));
333 SourceDesc = Loc.File;
337 //===----------------------------------------------------------------------===//
338 // Breakpoint manipulation methods
340 unsigned IP::addBreakpoint(unsigned LineNo, unsigned ColNo,
341 const GlobalVariable *SourceDesc) {
345 Loc.File = SourceDesc;
346 sendCommand(AddBreakpoint, &Loc, sizeof(Loc));
348 readFromChild(&ID, sizeof(ID));
352 void IP::removeBreakpoint(unsigned ID) {
353 sendCommand(RemoveBreakpoint, &ID, sizeof(ID));
357 //===----------------------------------------------------------------------===//
358 // Methods for communication with the child process
360 // Methods for communicating with the child process. If the child exits or dies
361 // while attempting to communicate with it, ChildPID is set to zero and an
362 // exception is thrown.
365 /// readFromChild - Low-level primitive to read some data from the child,
366 /// throwing an exception if it dies.
367 void IP::readFromChild(void *Buffer, unsigned Size) const {
369 "Child process died and still attempting to communicate with it!");
371 ssize_t Amount = read(ReadFD, Buffer, Size);
373 // If we cannot communicate with the process, kill it.
375 // If killChild succeeded, then the process must have closed the pipe FD
376 // or something, because the child existed, but we cannot communicate with
378 throw InferiorProcessDead(-1);
379 } else if (Amount == -1) {
380 if (errno != EINTR) {
383 throw "Error reading from child process!";
387 Buffer = (char*)Buffer + Amount;
393 /// writeToChild - Low-level primitive to send some data to the child
394 /// process, throwing an exception if the child died.
395 void IP::writeToChild(void *Buffer, unsigned Size) const {
397 ssize_t Amount = write(WriteFD, Buffer, Size);
398 if (Amount < 0 && errno == EINTR) continue;
400 // If we cannot communicate with the process, kill it.
403 // If killChild succeeded, then the process must have closed the pipe FD
404 // or something, because the child existed, but we cannot communicate with
406 throw InferiorProcessDead(-1);
409 Buffer = (char*)Buffer + Amount;
415 /// killChild - Kill or reap the child process. This throws the
416 /// InferiorProcessDead exception an exit code if the process had already
417 /// died, otherwise it just returns the exit code if it had to be killed.
418 void IP::killChild() const {
419 assert(ChildPID != 0 && "Child has already been reaped!");
424 PID = waitpid(ChildPID, &Status, WNOHANG);
425 } while (PID < 0 && errno == EINTR);
426 if (PID < 0) throw "Error waiting for child to exit!";
428 // If the child process was already dead, then it died unexpectedly.
430 assert(PID == ChildPID && "Didn't reap child?");
431 ChildPID = 0; // Child has been reaped
432 if (WIFEXITED(Status))
433 throw InferiorProcessDead(WEXITSTATUS(Status));
434 else if (WIFSIGNALED(Status))
435 throw InferiorProcessDead(WTERMSIG(Status));
436 throw InferiorProcessDead(-1);
439 // Otherwise, the child exists and has not yet been killed.
440 if (kill(ChildPID, SIGKILL) < 0)
441 throw "Error killing child process!";
444 PID = waitpid(ChildPID, 0, 0);
445 } while (PID < 0 && errno == EINTR);
446 if (PID <= 0) throw "Error waiting for child to exit!";
448 assert(PID == ChildPID && "Didn't reap child?");
452 /// waitForStop - This method waits for the child process to reach a stop
453 /// point. When it does, it fills in the CurLocation member and returns.
454 void IP::waitForStop() {
456 readFromChild(&Dummy, sizeof(char));
460 //===----------------------------------------------------------------------===//
461 // Child Process Code
462 //===----------------------------------------------------------------------===//
465 class SourceSubprogram;
467 /// SourceRegion - Instances of this class represent the regions that are
468 /// active in the program.
470 /// Parent - A pointer to the region that encloses the current one.
471 SourceRegion *Parent;
473 /// CurSubprogram - The subprogram that contains this region. This allows
474 /// efficient stack traversals.
475 SourceSubprogram *CurSubprogram;
477 /// CurLine, CurCol, CurFile - The last location visited by this region.
478 /// This is used for getting the source location of callers in stack frames.
479 unsigned CurLine, CurCol;
482 //std::vector<void*> ActiveObjects;
484 SourceRegion(SourceRegion *p, SourceSubprogram *Subprogram = 0)
485 : Parent(p), CurSubprogram(Subprogram ? Subprogram : p->getSubprogram()) {
486 CurLine = 0; CurCol = 0;
490 virtual ~SourceRegion() {}
492 SourceRegion *getParent() const { return Parent; }
493 SourceSubprogram *getSubprogram() const { return CurSubprogram; }
495 void updateLocation(unsigned Line, unsigned Col, void *File) {
501 /// Return a LocationToken for the place that this stack frame stopped or
502 /// called a sub-function.
503 LocationToken getLocation(ExecutionEngine *EE) {
507 const GlobalValue *GV = EE->getGlobalValueAtAddress(CurFileDesc);
508 LT.File = dyn_cast_or_null<GlobalVariable>(GV);
513 /// SourceSubprogram - This is a stack-frame that represents a source program.
515 class SourceSubprogram : public SourceRegion {
516 /// Desc - A pointer to the descriptor for the subprogram that this frame
520 SourceSubprogram(SourceRegion *P, void *desc)
521 : SourceRegion(P, this), Desc(desc) {}
522 void *getDescriptor() const { return Desc; }
526 /// Child class - This class contains all of the information and methods used
527 /// by the child side of the debugger. The single instance of this object is
528 /// pointed to by the "TheChild" global variable.
530 /// M - The module for the program currently being debugged.
534 /// EE - The execution engine that we are using to run the program.
538 /// ReadFD, WriteFD - The file descriptor handles for this side of the
540 FDHandle ReadFD, WriteFD;
542 /// RegionStack - A linked list of all of the regions dynamically active.
544 SourceRegion *RegionStack;
546 /// StopAtNextOpportunity - If this flag is set, the child process will stop
547 /// and report to the debugger at the next possible chance it gets.
548 volatile bool StopAtNextOpportunity;
550 /// StopWhenSubprogramReturns - If this is non-null, the debugger requests
551 /// that the program stops when the specified function frame is destroyed.
552 SourceSubprogram *StopWhenSubprogramReturns;
554 /// Breakpoints - This contains a list of active breakpoints and their IDs.
556 std::vector<std::pair<unsigned, LocationToken> > Breakpoints;
558 /// CurBreakpoint - The last assigned breakpoint.
560 unsigned CurBreakpoint;
563 Child(Module *m, ExecutionEngine *ee, FDHandle &Read, FDHandle &Write)
564 : M(m), EE(ee), ReadFD(Read), WriteFD(Write),
565 RegionStack(0), CurBreakpoint(0) {
566 StopAtNextOpportunity = true;
567 StopWhenSubprogramReturns = 0;
570 /// writeToParent - Send the specified buffer of data to the debugger
572 void writeToParent(const void *Buffer, unsigned Size);
574 /// readFromParent - Read the specified number of bytes from the parent.
576 void readFromParent(void *Buffer, unsigned Size);
578 /// childStopped - This method is called whenever the child has stopped
579 /// execution due to a breakpoint, step command, interruption, or whatever.
580 /// This stops the process, responds to any requests from the debugger, and
581 /// when commanded to, can continue execution by returning.
585 /// startSubprogram - This method creates a new region for the subroutine
586 /// with the specified descriptor.
587 void startSubprogram(void *FuncDesc);
589 /// startRegion - This method initiates the creation of an anonymous region.
593 /// endRegion - This method terminates the last active region.
597 /// reachedLine - This method is automatically called by the program every
598 /// time it executes an llvm.dbg.stoppoint intrinsic. If the debugger wants
599 /// us to stop here, we do so, otherwise we continue execution.
600 void reachedLine(unsigned Line, unsigned Col, void *SourceDesc);
603 /// TheChild - The single instance of the Child class, which only gets created
604 /// in the child process.
606 } // end anonymous namespace
609 // writeToParent - Send the specified buffer of data to the debugger process.
610 void Child::writeToParent(const void *Buffer, unsigned Size) {
612 ssize_t Amount = write(WriteFD, Buffer, Size);
613 if (Amount < 0 && errno == EINTR) continue;
615 write(2, "ERROR: Connection to debugger lost!\n", 36);
619 Buffer = (const char*)Buffer + Amount;
625 // readFromParent - Read the specified number of bytes from the parent.
626 void Child::readFromParent(void *Buffer, unsigned Size) {
628 ssize_t Amount = read(ReadFD, Buffer, Size);
629 if (Amount < 0 && errno == EINTR) continue;
631 write(2, "ERROR: Connection to debugger lost!\n", 36);
635 Buffer = (char*)Buffer + Amount;
641 /// childStopped - This method is called whenever the child has stopped
642 /// execution due to a breakpoint, step command, interruption, or whatever.
643 /// This stops the process, responds to any requests from the debugger, and when
644 /// commanded to, can continue execution by returning.
646 void Child::childStopped() {
647 // Since we stopped, notify the parent that we did so.
649 writeToParent(&Token, sizeof(char));
651 StopAtNextOpportunity = false;
652 StopWhenSubprogramReturns = 0;
654 // Now that the debugger knows that we stopped, read commands from it and
655 // respond to them appropriately.
660 readFromParent(&Command, sizeof(CommandID));
664 // To step the program, just return.
665 StopAtNextOpportunity = true;
668 case FinishProgram: // Run until exit from the specified function...
669 readFromParent(&Frame, sizeof(Frame));
670 // The user wants us to stop when the specified FUNCTION exits, not when
671 // the specified REGION exits.
672 StopWhenSubprogramReturns = Frame->getSubprogram();
676 // To continue, just return back to execution.
679 case GetSubprogramDescriptor:
680 readFromParent(&Frame, sizeof(Frame));
682 EE->getGlobalValueAtAddress(Frame->getSubprogram()->getDescriptor());
683 writeToParent(&Result, sizeof(Result));
687 readFromParent(&Frame, sizeof(Frame));
688 Result = Frame ? Frame->getSubprogram()->getParent() : RegionStack;
689 writeToParent(&Result, sizeof(Result));
692 case GetFrameLocation: {
693 readFromParent(&Frame, sizeof(Frame));
694 LocationToken LT = Frame->getLocation(EE);
695 writeToParent(<, sizeof(LT));
698 case AddBreakpoint: {
700 readFromParent(&Loc, sizeof(Loc));
701 // Convert the GlobalVariable pointer to the address it was emitted to.
702 Loc.File = (GlobalVariable*)EE->getPointerToGlobal(Loc.File);
703 unsigned ID = CurBreakpoint++;
704 Breakpoints.push_back(std::make_pair(ID, Loc));
705 writeToParent(&ID, sizeof(ID));
708 case RemoveBreakpoint: {
710 readFromParent(&ID, sizeof(ID));
711 for (unsigned i = 0, e = Breakpoints.size(); i != e; ++i)
712 if (Breakpoints[i].first == ID) {
713 Breakpoints.erase(Breakpoints.begin()+i);
719 assert(0 && "Unknown command!");
726 /// startSubprogram - This method creates a new region for the subroutine
727 /// with the specified descriptor.
728 void Child::startSubprogram(void *SPDesc) {
729 RegionStack = new SourceSubprogram(RegionStack, SPDesc);
732 /// startRegion - This method initiates the creation of an anonymous region.
734 void Child::startRegion() {
735 RegionStack = new SourceRegion(RegionStack);
738 /// endRegion - This method terminates the last active region.
740 void Child::endRegion() {
741 SourceRegion *R = RegionStack->getParent();
743 // If the debugger wants us to stop when this frame is destroyed, do so.
744 if (RegionStack == StopWhenSubprogramReturns) {
745 StopAtNextOpportunity = true;
746 StopWhenSubprogramReturns = 0;
756 /// reachedLine - This method is automatically called by the program every time
757 /// it executes an llvm.dbg.stoppoint intrinsic. If the debugger wants us to
758 /// stop here, we do so, otherwise we continue execution. Note that the Data
759 /// pointer coming in is a pointer to the LLVM global variable that represents
760 /// the source file we are in. We do not use the contents of the global
761 /// directly in the child, but we do use its address.
763 void Child::reachedLine(unsigned Line, unsigned Col, void *SourceDesc) {
765 RegionStack->updateLocation(Line, Col, SourceDesc);
767 // If we hit a breakpoint, stop the program.
768 for (unsigned i = 0, e = Breakpoints.size(); i != e; ++i)
769 if (Line == Breakpoints[i].second.Line &&
770 SourceDesc == (void*)Breakpoints[i].second.File &&
771 Col == Breakpoints[i].second.Col) {
776 // If we are single stepping the program, make sure to stop it.
777 if (StopAtNextOpportunity)
784 //===----------------------------------------------------------------------===//
785 // Child class wrapper functions
787 // These functions are invoked directly by the program as it executes, in place
788 // of the debugging intrinsic functions that it contains.
792 /// llvm_debugger_stop - Every time the program reaches a new source line, it
793 /// will call back to this function. If the debugger has a breakpoint or
794 /// otherwise wants us to stop on this line, we do so, and notify the debugger
798 void *llvm_debugger_stop(void *Dummy, unsigned Line, unsigned Col,
799 void *SourceDescriptor) {
800 TheChild->reachedLine(Line, Col, SourceDescriptor);
805 /// llvm_dbg_region_start - This function is invoked every time an anonymous
806 /// region of the source program is entered.
809 void *llvm_dbg_region_start(void *Dummy) {
810 TheChild->startRegion();
814 /// llvm_dbg_subprogram - This function is invoked every time a source-language
815 /// subprogram has been entered.
818 void *llvm_dbg_subprogram(void *FuncDesc) {
819 TheChild->startSubprogram(FuncDesc);
823 /// llvm_dbg_region_end - This function is invoked every time a source-language
824 /// region (started with llvm.dbg.region.start or llvm.dbg.func.start) is
828 void llvm_dbg_region_end(void *Dummy) {
829 TheChild->endRegion();
836 /// DebuggerIntrinsicLowering - This class implements a simple intrinsic
837 /// lowering class that revectors debugging intrinsics to call actual
838 /// functions (defined above), instead of being turned into noops.
839 struct DebuggerIntrinsicLowering : public DefaultIntrinsicLowering {
840 virtual void LowerIntrinsicCall(CallInst *CI) {
841 Module *M = CI->getParent()->getParent()->getParent();
842 switch (CI->getCalledFunction()->getIntrinsicID()) {
843 case Intrinsic::dbg_stoppoint:
844 // Turn call into a call to llvm_debugger_stop
845 CI->setOperand(0, M->getOrInsertFunction("llvm_debugger_stop",
846 CI->getCalledFunction()->getFunctionType()));
848 case Intrinsic::dbg_region_start:
849 // Turn call into a call to llvm_dbg_region_start
850 CI->setOperand(0, M->getOrInsertFunction("llvm_dbg_region_start",
851 CI->getCalledFunction()->getFunctionType()));
854 case Intrinsic::dbg_region_end:
855 // Turn call into a call to llvm_debugger_stop
856 CI->setOperand(0, M->getOrInsertFunction("llvm_dbg_region_end",
857 CI->getCalledFunction()->getFunctionType()));
859 case Intrinsic::dbg_func_start:
860 // Turn call into a call to llvm_debugger_stop
861 CI->setOperand(0, M->getOrInsertFunction("llvm_dbg_subprogram",
862 CI->getCalledFunction()->getFunctionType()));
865 DefaultIntrinsicLowering::LowerIntrinsicCall(CI);
870 } // end anonymous namespace
873 static void runChild(Module *M, const std::vector<std::string> &Arguments,
874 const char * const *envp,
875 FDHandle ReadFD, FDHandle WriteFD) {
877 // Create an execution engine that uses our custom intrinsic lowering object
878 // to revector debugging intrinsic functions into actual functions defined
880 ExecutionEngine *EE =
881 ExecutionEngine::create(new ExistingModuleProvider(M), false,
882 new DebuggerIntrinsicLowering());
883 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
885 // Call the main function from M as if its signature were:
886 // int main (int argc, char **argv, const char **envp)
887 // using the contents of Args to determine argc & argv, and the contents of
888 // EnvVars to determine envp.
890 Function *Fn = M->getMainFunction();
893 // Create the child class instance which will be used by the debugger
894 // callbacks to keep track of the current state of the process.
895 assert(TheChild == 0 && "A child process has already been created??");
896 TheChild = new Child(M, EE, ReadFD, WriteFD);
899 int Result = EE->runFunctionAsMain(Fn, Arguments, envp);
901 // If the program didn't explicitly call exit, call exit now, for the program.
902 // This ensures that any atexit handlers get called correctly.
903 Function *Exit = M->getOrInsertFunction("exit", Type::VoidTy, Type::IntTy, 0);
905 std::vector<GenericValue> Args;
906 GenericValue ResultGV;
907 ResultGV.IntVal = Result;
908 Args.push_back(ResultGV);
909 EE->runFunction(Exit, Args);