windbg: Command output to text file - windbg

How can I Save Output of a command in WinDbg to a Text File?

Start WinDbg from the command line using the -logo option:
windbg.exe -logo logfile.txt
That will get everything done logged to the file specified. You can find more details of the command line options here.
Or, if you are already in a debugging session, you can use the .logopen command to start logging. For more info on this command see here
Or you can click edit->Open/Close log file in the WinDbg GUI.
More info on log files is here.

You can use .logopen , all of the commands you input and response from windbg will be logged, and then use .logclose to flush the data into

You can also do this from the WinDbg gui 'Edit>Write Window Text To File...' if you find that easier.

To send the output of a specific command to a file, go and get Tee, (I use Tee.bat), put it into a directory covered by the Windows PATH environment variable (or add the directory you've put it in). Then use the following syntax from the WinDbg GUI to send the output of a command to a file
.shell -ci "<command>" tee <file>
.shell -ci "!gcroot 000002b56c414750" tee c:\path\out_01.log
There's an undocumented !! abbreviation of .shell. So you could just do:
!! -ci "!gcroot 000002b56c414750" tee c:\path\out_01.log
Background
You can run shell commands in the WinDbg GUI. The syntax is:
.shell [Options] [ShellCommand]
.shell -i InFile [-o OutFile [-e ErrFile]] [Options] ShellCommand
Here I found the solution to send the command output to the clipboard.
.shell -ci "<command>" clip
If you can send the output to clip, shouldn't it be possible to send it to a file, like so?
.shell -ci "<command>" > <file>
.shell -ci "!gcroot 000002b56c414750" > c:\path\out_01.log
Would be nice, but it does not work. In the MS docs I found this example.
.shell -ci "!process 0 7" perl.exe parsemyoutput.pl
So it is possible to send the output to an application. Just like the above example using clip. And that's where Tee came in. But you could use any other script or .exe which takes the output and stores it in a file or another place.

Related

Solaris 10 time command to find execution time and save in a text file

H,
There is Solaris command "time" to find execution time of command ... e.g.
time command
I need to execute this "time command" command and save output in a file.
I can find -o flag in Linux (e.g. /usr/bin/time -o output.txt COMMAND) but this is not working in Solaris 10.
I would be thankful if some expert guide me to implement it in Solaris.
Best Regards
You need to redirect the output of time command to file:
time command >>output.txt 2>&1
the construction 2>&1 make STDERR also to go in the same file
And this work (with very high probability) on ANY Unix and Linux

How to check if a file exists in perforce in perl script

I can use the if (-e $file) in perl script to tell if a file exists in linux or not.
I can run p4 fstat $file in linux to tell if a file exists in perforce depot.
But how do I use perl script to check if a file is in perforce depot?
You can run a command from a Perl script with the backtick operator:
`p4 files $file`
This will work as a truthy check to see if a file exists in Perforce:
if (`p4 -F %depotFile% files "$file"`)
Note that this requires you to be logged in to the Perforce server before you run the script (if you can't connect you'll always get "false" from the above condition).
Based on the question asked, it appears that you could run the p4 command using Perl's system(), or the exec() method, or as described above using the "backticks" operator.
The backtics operator will allow you trap STDERR and STDOUT from the command, so that you can use it for whatever.
The other two will attempt run the command and give return values accordingly, as described in perldoc -f exec
So, typically what can be done is a command like:
p4 files $file
Followed by
echo $?
The second command will get you the exit status of the last shell command.
When using backticks you can also try these:
Windows: command-to-execute 2>NUL
Linux / OSX: command-to-execute 2> /dev/null
These will suppress STDERR and give you the output only. If you use a "1" instead of a "2" you will suppress STDOUT and get the error only.
Finally, if you want both, you can use:
Windows / Linux / macOS: command-to-execute 1>2&
This will concatenate STDOUT and STDERR.
So in short, you use Perl to run any existing program on any operating system, and there are multiple ways to process the result of that.

How to write Command Prompt to a file AND to the screen at the same time

I am trying to write the output of a powershell command to a file. I have completed this:
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1
This is dumping all the screen output to a File... GREAT!!
NOW, I also want it to dump to the console screen too. I have used the argument >CON
but it will dump everything to the screen, but not the file.
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1 >CON
Maybe >CON is not the way to go.. Here is a test I am using:
dir > c:\test\directory.txt 2>&1
Anyone got an idea on how to do this?????
Instead of having CMD output the file, let your Powershell command do it with the Tee-Object cmdlt.
So for your case, something like:
powershell ^& "'C:\Program Files\7-Zip\7z.exe'" ^| tee B:\test.log
Note that the ^ is required before | so that way it passes it to Powershell and isn't interpreted literally.
Well, as it turns out, I cannot find anything that says you can write to 2 different outputs in Command Prompt. Once you redirect the output to a file, you redirect it. That's it. I cannot find any way to write to the screen as well using commands. SO, the solution that I used was to download "TailXP". Install it on the box and configure it to point to the file i am writing. This will read the file as it's being generated and write it to it's own console screen. It served our purpose and I have it all wrapped up in a .cmd file.

How is this zip command being used in perl?

I found this piece of code in perl
system("zip $ZIP_DEBUG -r -9 itvlib.zip $include $exclude");
However I don't understand how it is working. I mean system() is used to fire 'system' commands right ? So is this 'zip' command used here a 'system' command ?
But I tried firing just the following on the command prompt;
zip $ZIP_DEBUG -r -9 itvlib.zip arg1 arg2
It didn't work !
it gave the following error:
'zip' is not recognized as an internal or external command,
operable program or batch file.
Well this shouldn't have happened, since the command seems to use 'zip' as a system command. So this makes the command 'zip' mysterious
Can you please help me to understand this command with all its parameters?
It's probably not working since you're not replacing things like $ZIP_DEBUG with their equivalent real values. Within Perl, they will be replaced with the values of the variables before being passed to the system call.
If you print out those Perl variables (or even the entire command) before you execute that system call, you'll find out those real values that you need to use. You can use the following transcript to guide you:
$ perl -e '
> $ZIP_DEBUG = "xyzzy";
> $include = "inc_files";
> $exclude = "exc_files";
> print "zip $ZIP_DEBUG -r -9 itvlib.zip $include $exclude";
> '
zip xyzzy -r -9 itvlib.zip inc_files exc_files
For details on how system works, see here. For details on what zip needs to function, you should just be able to run:
man zip
from a command line shell (assuming you're on Linux or its brethren). If, instead, you're on a different operating system (like Windows), you'll have to figure out how to get the zip options out. This may well be as simple as zip -? of zip -h but there's no guarantee that will work.
If it's the same as the Info-ZIP zip under Linux (and it may be if you have the -9 and -r options and your exclude variable starts with -x), then zip -h will get you basic help and zip -h2 will give you a lot more.
system("zip $ZIP_DEBUG -r -9 itvlib.zip $include $exclude");
is running a program named zip (probably zip.exe) somewhere on the path. $ZIP_DEBUG, $include, and $exclude are Perl variables that are interpolated into the command line before the command is run.
If the system call works in the Perl script, but zip -? gives the 'zip' is not recognized as an internal or external command, operable program or batch file error, then the PATH of the Perl script must be different than the PATH in your command prompt. Or, there might be a zip command in the current directory when Perl executes the system command. (In Windows, the current directory is an implicit member of your PATH.)
To see what the PATH is for the Perl script, you can add a print "$ENV{PATH}\n"; before the system command. To see what the PATH is in your command prompt, type PATH.
Yes, zip is a system command. The variables $ZIP_DEBUG and such are perl variables that are interpolated to the command before launching zip.
To debug what the actual call is, try adding:
print("zip $ZIP_DEBUG -r -9 itvlib.zip $include $exclude\n");
See perldoc for details on system.

How to specify in a bat file that a script needs to be called in Cygwin mode?

I wrote a perl script which uses some linux commands (grep, ls etc..). I can successfully run this from Cygwin or Linux. I want this task to be run periodically on a Windows Server which has Cygwin installed. I was planning to use Windows task scheduler. But I am not sure how to specify in a Windows bat file, that my perl script needs to be called in Cygwin mode?
EDIT: I tried the command by Glenn. When I tried running the perl script, it doesn't seem to respond. So I tried with a sample script: test.sh, which has the following two lines:
ls -l
cd ..
Here is the screen capture of what I am getting:
I'm not a great fan of cygwin and personally prefer natively compiled versions of the GNU tools, e.g. GnuWin32.
I also wonder why you would be using grep, ls etc. from a Perl script. Most of that functionality can be handled natively by Perl and this usually results in much better portability and robustness.
Perhaps (untested): c:\cygwin\bash.exe -c /path/to/your/script.pl
UPDATE:
The last error message reveals one problem: your script is a DOS format file (CRLF line endings), while cygwin looks for UNIX format (LF line endings). The stray carriage returns at the end of each line is the problem. For example, there's no directory named "..\r"
Use a text editor where you can specify the line endings to use. In a bash shell, you can do dos2unix test.sh
The ls error indicates that /bin and /usr/bin are not in your bash environment's $PATH -- is that true?
Just add cygwin to your path before running perl. For example, I often run find in a dos shell, but get the rather horrible message FIND: Parameter format not correct. Bah! Instead I have to run find via a dos cmd file cyg.cmd:
c:> find . -iname interesting.txt
FIND: Parameter format not correct
c:> cyg find . -iname interesting.txt
sub/sub/interesting.TXT
c:> type bin\cyg.cmd
setlocal
PATH=c:\Progs\Cygwin\bin;%PATH%
%1 %2 %3 %4 %5 %6 %7 %8 %9
endlocal
The important bit here is the PATH=c:\Progs\Cygwin\bin;%PATH%.
BTW, I much prefer the cygwin versions of the tools rather than their MinGW equivalents—the environment is much closer to Mac/Linux, and portability is important after all.
You run cygwin bash, but you still have to setup your PATH. Unless you set it in your profile, but initialize the profile then with bash -i.
Either specify the full path to cygwin commands needed, like /bin/ls, /bin/grep,
or add c:\cygwin\bin and maybe other paths to your PATH beforehand.
2nd preferred. Like
schedule.bat:
PATH=C:\cygwin\bin;%PATH%
sh -c ./schedule.sh
schedule.sh:
#!/bin/sh
ls ...
grep ...
perl ...
schedule.sh gets the environment with the PATH from the parent process sh.exe, which inherits it from your bat.
Seperating shell scripts from batch files just for easier testing. You can call most cygwin programs from cmd.exe also.
You cannot set /usr/bin in your DOS PATH, DOS will not have a c:/usr directory. And it only works if you are in C: