Attach to a Processes Output for Viewing

Attach to a processes output for viewing

There are a few options here. One is to redirect the output of the command to a file, and then use 'tail' to view new lines that are added to that file in real time.

Another option is to launch your program inside of 'screen', which is a sort-of text-based Terminal application. Screen sessions can be attached and detached, but are nominally meant only to be used by the same user, so if you want to share them between users, it's a big pain in the ass.

Console.WriteLine does not work when attached to process in VS

Visual Studio, when debugging windows programs (/target:winexe) will launch them with the stdout and stderr redirected to Named Pipes. The other end of the named pipe is owned by the VS debugger and anything read on these pipes (ie. anything written by the app to stdout or stderr) will be displayed in the Debug Output window (which by no means is the debugged application Output window).

When attach to an application, this redirect can no longer be done (obviously, since the application is already started and stderr and stdin cannot be redirected post-factum). So the Console.Write is no longer auto-magically redirected to the Debug output.

Note that this stdout/stderr redircetion does not occur for console programs (/target:exe)

But there is a dedicated API to write Debug info: Debug.Write. This uses the OutputDebugString functions, which sends text to the attached debugger (if any). This works no matter when or how the debugger is attached, as is not dependent upon stdout/stderr redirection tricks.

Replace your Console.Write with Debug.Write.

Visual Studio attach to process and view the code on break?

Instead of inserting delays and rushing to attach to the process in time, you can call

System.Diagnostics.Debugger.Break();

to break execution and attach the debugger programmatically.

Visual Studio - attach to process

If you are running an application in a different user context and need to debug it, you can attach to the process. This process can be local or remote.

Essentially it is used to debug an already running process.

Note: You do need to have the source code for this process in Visual Studio (or have a symbol server that Visual Studio can use that points to the same executable).

view output of already running processes in linux

There is already an program that uses ptrace(2) in linux to do this, retty:

http://pasky.or.cz/dev/retty/

It works if your running program is already attached to a tty, I do not know if it will work if you run your program in background.

At least it may give some good hints. :)

You can probably retreive the exit code from the program using ptrace(2), otherwise just attach to the process using gdb -p <pid>, and it will be printed when the program dies.

You can also manipulate file descriptors using gdb:

(gdb) p close(1)
$1 = 0
(gdb) p creat("/tmp/stdout", 0600)
$2 = 1

http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/

How do I attach Visual Studio to a process that is not started yet?

Actually you can; you don't attach to it, you start it. On the properties of your project, on the Debugging tab, specify the path of the program you want to attach to in the "Command" textbox.

You can also enter any command-line arguments for the program in the "Command Arguments" box:

Sample Image

Ensure that "Attach" is set to "No".



Related Topics



Leave a reply



Submit