Monitoring certain system calls done by a process in Windows
Asked Answered
C

8

49

I would like to be able to monitor certain system calls made by a process, primarily file I/O calls. On Linux I can probably get away using strace with suitable parameters, but how can I do this on Windows?

I'm primarily interested in running a process and figuring out which files it has read and written.

I want to do this programmatically from another process. I'm aware of Process Monitor, but I would like to receive the data in a form which I can import into another program for further analysis.

If I narrow down my requirements even further, it is probably enough to be able to monitor calls to CreateFile(). I'm really only interested in what files are opened, and if they are opened for read/write or just read. Another requirement which I didn't really state is that speed is fairly important; I was planning on doing this for things like compiling a C++-file, and pulling up a full GUI which generates a 20 MB logfile will have prohibitive overhead.

It would also be nice if it did not require administrative privileges.

Coastal answered 14/5, 2009 at 18:6 Comment(2)
Interesting. Isn't there something similar to linux ptrace syscall in Windows? All the answers are about using other programs but with something like ptrace() you could do it yourshelf.Hindward
In case you're interested in a different approach I've asked about a windows equivalent to ptrace : #865606Hindward
R
37

There are several options on Windows.

Windows Performance Toolkit can be used to enable tracing of various system events, including file I/O, and includes tools for processing and viewing these events. You can use xperf to begin trace variously classes of events and save to an ETL file that you can then process or view using the same tools later.

Process Monitor from Sysinternals is another, very easy to use, option, and enables you to quickly see all file and registry accesses any process on the system is doing. You can also run Process Monitor in an automated fashion.

If you'd like to do this completely programmatically, you can use the ETW functions (StartTrace, EnableTrace, etc.) to snap file I/O events and save to an ETL file. Sample code here.

Retiform answered 14/5, 2009 at 18:9 Comment(8)
Thanks, I'll look into WPT. Does ProcessMonitor have any support for running without a GUI?Coastal
Updated answer with a link showing how to automated Process Monitor.Retiform
Is there a way to get the xperf set of tool without downloading a whopping 1.3GB (!) iso-image?Coastal
The command line use of ProcessMonitor falls a little short: it still opens a window, and I couldn't find a way to specify filters on the command line. Also, ProcessMonitor has be run as administrator (at least on my Vista machine), which makes it rather unusable for my purposes.Coastal
msdn.microsoft.com/en-us/performance/default.aspx, under downloads, has several downloads of just the toolkit for under 5 MB.Retiform
That link to WPF is broken, I found it here: msdn.microsoft.com/en-us/library/windows/hardware/hh162945.aspxMitchum
The link in the third paragraph is (effectively) broken. It redirects to a generic page, "Archived MSDN and TechNet Blogs".Solvent
Undead link: cloudnotes.io/how-to-automate-process-monitorLateshalatest
I
11

On Windows, you can use Process Monitor to monitor process activity (I/O and registry). I guess this fits your need if you don't really want to know the system calls.

And you can use winapioverride32 to monitor API calls.

Inventive answered 14/5, 2009 at 18:14 Comment(1)
How to do this in order to modify the parameter used by a specific system call ?Buttonhole
C
7

API Monitor by Rohitab Batra is very good for system calls.

Conversazione answered 3/7, 2013 at 15:1 Comment(0)
O
4

Use FileMon (now integrated into Process Monitor).

There is also NtTrace, similar to strace.

Ozan answered 14/5, 2009 at 18:16 Comment(1)
NtTrace fails on my Vista x64 with lots of "Cannot trap ... wrong signature". It hasn't been updated since 2007, though.Coastal
A
3

Another Windows API tracing tool: logexts.dll (part of the Debugging Tools for Windows), which can be run from inside WinDbg/ntsd/cdb or through a standalone logger.exe program.

Autocade answered 14/5, 2009 at 18:21 Comment(0)
L
3

Another way is to use Deviare API Hook and intercept all user-mode system calls that you want. Using this framework you can code a generic handler for all calls since the parameters can be read using COM interfaces (for example, each parameter is an INktParam, and you can get the value using INktParam.Value).

Another alternative, but it will cost some money, is to use SpyStudio from the same company. This product has a command-line option that is useful to collect logs without a GUI.

Lager answered 25/6, 2013 at 20:16 Comment(0)
H
0

DTRACE

I want to mention this tool that was intentionally created for monitoring system calls in Solaris but later was ported to windows.

https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/dtrace

Unfortunately: Requires administrative privileges.

However I think it is possible to script this program to display exactly the CreateFile() syscall being executed.

Hoseahoseia answered 1/2 at 17:1 Comment(0)
O
-7

Use strace. Example output:

open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
fcntl64(3, F_GETFD)                     = 0x1 (flags FD_CLOEXEC)
getdents64(3, /* 18 entries */, 4096)   = 496
getdents64(3, /* 0 entries */, 4096)    = 0
close(3)                                = 0
fstat64(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f2c000
write(1, "autofs\nbackups\ncache\nflexlm\ngames"..., 86autofsA
Operatic answered 14/9, 2010 at 3:52 Comment(2)
@matt anyway, there is strace in cygwin.Piperidine
@Matt it is expected that the answer is for Windows. strace is mainly a Linux utility.Perch

© 2022 - 2024 — McMap. All rights reserved.