I make a standalone deubugger app using Microsoft's DbgEng.
I want to open a 2nd thread that sends commands to an additional IDebugControl instance, and I want to get the specific output for this IDebugControl Execute method call (and send it through a tcp connection).
I have a problem since the IDebugControl from main thread also sends commands to Execute() and the outputs might be mixed.
I need a second thread because once it execute a command like "g" (go) it will call WaitForEvent() and I won't be able to get any further information about the target until an event occurs.
I need a solution for that.
The second IDebugControl is used for operations that send requests for only data, like disassembly lines, memory dump...
I wonder if I can make a 2nd IDebugClient and attach it to the already opened debugged process, and then the problem is solved because I can put other callbacks to it.
Is there a solution for me?
I think you should not call any command to debug engine while it is waiting for events except IDebugControl::SetInterrupt.
If you want use two thread anyway you can register IDebugOutputCallbacks callback interface and handle output with any sort of mutex.
Related
I have c++ process which is running forever and all data is in memory (not in database). Occasionally I need to change code and deploy new version and I need to stop process but before I need to serialize to file and/or save to database ( I have function inside running class which can do that). How to trigger this function, is possible to hook some callback and then send some event/signal/interrupt from command line ?
( I can use 0mq but and add command inside but I wonder is there more elegant solution to this).
I’m writing a simple Windows console c++ application. If the application is started a second time (on the same computer) it should not span an new instance but pass command line arguments to the instance already running.
I have accomplished to ensure that the application only runs in one instance by using mutex but I am unable to notify the first application that it has been started as second time and pass on command line arguments.
Use case:
listener.exe -start // starts listener
listener.exe -stop // stops listener
If you just want to communicate a simple boolean value (start/stop, for example), then you probably need an Event object.
If you want to exchange more complex data between processes, you could use named pipes or perhaps blocks of shared memory.
The first listener should wait on an event object which is for shutdown. When you launch listener.exe -stop then it will just set the global event for shutdown and if first instance is running then it would exit. Named event object is required in order for the other processes to refer it. Also when you fire command 2nd time it will launch another process there is no implicit IPC with command interpreter.
listener.exe -start:
Create a named event (CreateEvent)
Wait on the event in the main thread or any suitable thread. (WaitForSingleObject)
On event initiate shutdown
listener.exe -stop
Get Handle to named event.
Set the event so that the thread of first process knows that shutdown event is fired and it exits
Some reference:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686915(v=vs.85).aspx
There are many types of IPC. One technique that worked well for me on Windows was using a separate thread to process messages for a message only window. Once you determine you are the primary instance of the program, or listener(via mutex creation as in your scenario) create the message only window and start a thread to process messages. For secondary instances, if there is something on the command tail, pass it as a string to the message only window using WM_COPYDATA message. The listener program ignores all other messages except perhaps a token telling it to quit. Once the secondary instance passes the message to the message only window it exits.
This can work very well for a scenario where you may have dozens of secondary instances opening. One example would be the user selects 50 files in an Explorer folder, right clicks, and runs the program. The listener processes the message only window in a dedicated thread and queues up the strings(in this case filenames) for processing.
I made a MFC application which probably has two threads, one for receiving data from a socket using UDP protocol and one is the main thread of MFC app. While any data is received some objects, created in the main thread by new operator, would be notified to fetch the data through apply the observer design pattern. The problem is that sometimes after I clicked the close system button, the GUI of the app disappeared, but its process can still be found in the Task Manager. If I stop the data source (UDP client) this problem would never happen. Other important and maybe helpful information is listed below:
The Observer design pattern was implemented with STL container list. I have used the critical section protection in the Attach, Detach and Notify functions.
I deleted the observer objects before closing the UDP socket.
The data transfer rate may be a little faster than process data, because after closing the data source the data process is still working.
I can't figure out what lead my app can not exit completely. Please give me some clues.
This is usually caused by a thread you created and not exit it programmatically when you exit the appliation. There must be a while clause in your thread. The way to find where it is still running is:
use debug mode to start you application and click the exit button the top right corner to exit it.
Check from task manager and see if it is still running
if it is, excute Debug->Break All,
Open threads windows, double click each thread, you will find where your code is still looping.
Typically a process won't terminate because there's still a foreground thread running somewhere. You must ensure that your socket library isn't running any thread when you want to close your application.
First thing, with MFC, please use the notification based methods to get notifications on message arrivals, connections etc. So you can get rid of threads if you have.
It's quite easy to attache to a debugger and Break see which threads are existing and waiting for what.
Alternatively you can use ProcessExplorer with proper symbol configuration to see the call stacks of the threads available for the particular process.
The application can two kind of issues to exit, one could be infinite loop and other might be waiting/deadlock (e.g. socket read command is a blocking call). You can easily deduce the problem by attaching to debugger.
Otherwise please provide further information about the threads, code snippet possible.
I'm developing a DLL in C++ which needs to write some data via a (previously established) TCP/IP connection using the write() call. To be precise, the DLL should send a little 'Process 12345 is terminating at 2007-09-27 15:30:42, value of i is 131' message over the wire when the process goes down.
Unfortunately, all the ways I know for detecting that the process is ending are apparently too late for any network calls to succeed. In particular, I tried the following approaches and the write() call returned -1 in every case:
Calling write() from the destructor of a global object.
Calling write() from a callback function registered using atexit().
Calling write() from DllMain (in case the reason argument is DLL_PROCESS_DETACH). I know that this is not a safe thing to do, but I'm getting a bit desperate. :-)
I'm aware that a DLL can't detect any process shutdown (it might have been unloaded long before the process terminates) but since the shutdown data which the DLL needs to send depends on other code in the DLL, that's acceptable. I'm basically looking for the latest moment at which I can safely perform network IO.
Does anybody know how to do this?
Consider monitoring the process from a separate watchdog process.
Determining If a Process Has Exited: http://msdn.microsoft.com/en-us/library/y111seb2(v=VS.71).aspx
Tutorial: Managing a Windows Process: http://msdn.microsoft.com/en-us/library/s9tkk4a3(v=VS.71).aspx
Consider to use Windows Job Objects.
You main program (monitoring program, which will use for example send()) can start child process suspended, place it into a Job and then resume. Then it will run in the job object. You can register notification via SetInformationJobObject with JobObjectAssociateCompletionPortInformation. Then you will be notified if in the job will be created some child process and if some process inside of job will be ended. So you will be able to send all what you need from the monitoring process. If you debug a program in Visual Studio it uses also job objects to have control under your process and all child processes which you start.
I successfully use the technique in C++ and in C#. So if you will have some problem with implementation I could post you a code example.
I suggest taking option 3. Just do your DLL loading/unloading properly and you're fine. Calling write() should work, I can't explain why it's not in your case. Is it possible that the call fails for a different reason that is unrelated?
Does it work if you call your DLL function manually from the host app?
Why? Just close the socket. If that's the only close in the program, which by your description it must be, that tells the other end that this end is exiting, and you can send the process ID information at the beginning instead of the end. You shouldn't do anything time-consuming or potentially blocking in an exit hook or static destructor.
Where is Winsock being shut down using WSACleanup? You need to make sure that your I/O completes before this happens.
You should be able to work out if this is happening by placing a breakpoint on the Win32 call in Winsock2.dll. Unload of DLLs is displayed in the output in the debug window.
I need to execute some commands via "/bin/sh" from a daemon. Some times these commands takes too long to execute, and I need to somehow interrupt them. The daemon is written in C++, and the commands are executed with std::system(). I need the stack cleaned up so that destructors are called when the thread dies. (Catching the event in a C++ exception-handler would be perfect).
The threads are created using boost:thread. Unfortunately, neither boost::thread::interrupt() or pthread_cancel() are useful in this case.
I can imagine several ways to do this, from writing my own version of system(), to finding the child's process-id and signal() it. But there must be a simpler way?
Any command executed using the system command is executed in a new process. Unfortunately system halts the execution of the current process until the new process completes. If the sub process hangs the new process hangs as well.
The way to get round this is to use fork to create a new process and call one of the exec calls to execute the desired command. Your main process can then wait on the child process's Process Id (pid). The timeout can be achieve by generating a SIGALRM using the alarm call before the wait call.
If the sub process times out you can kill it using the kill command. Try first with SIGTERM, if that fails you can try again will SIGKILL, this will certainly kill the child process.
Some more information on fork and exec can be found here
I did not try boost::process, as it is not part of boost. I did however try ACE_Process, which showed some strange behavior (the time-outs sometimes worked and sometimes did not work). So I wrote a simple std::system replacement, that polls for the status of the running process (effectively removing the problems with process-wide signals and alarms on a multi threading process). I also use boost::this_thread::sleep(), so that boost::thread::interrupt() should work as an alternative or in addition to the time-out.
Stackoverflow.com does not work very good with my Firefox under Debian (in fact, I could not reply at all, I had to start Windows in a VM) or Opera (in my VM), so I'm unable to post the code in a readable manner. My prototype (before I moved it to the actual application) is available here: http://www.jgaa.com/files/ExternProcess.cpp
You can try to look at Boost.Process:
Where is Boost.Process?
I have been waiting for a long time for such a class.
If you are willing to use Qt, a nice portable solution is QProcess:
http://doc.trolltech.com/4.1/qprocess.html
Of course, you can also make your own system-specific solution like Let_Me_Be suggests.
Anyway you'd probably have to get rid of the system() function call and replace it by a more powerful alternative.