简体   繁体   English

如何在Windows上的控制台输出上中断(设置断点)?

[英]How to break (set a breakpoint) on Console output on Windows?

Background: 背景:

I'm running a large nUnit test suite on my developer PC (via nunit-console) and I'm seeing some garbage output in the console window. 我正在我的开发者PC上运行一个大型的nUnit测试套件(通过nunit-console),我在控制台窗口看到了一些垃圾输出。

The units under test do involve .NET code as well as underlying C++ and C libraries and I haven't yet found out who is producing the garbage output. 被测单元确实涉及.NET代码以及底层的C ++和C库,我还没有找到谁在生成垃圾输出。

Question: 题:

Is there a single Windows API function where all Console output goes through? 是否有一个Windows API函数,其中所有控制台输出都通过? (regardless where it comes from.) (无论它来自哪里。)

I have tried setting a breakpoint inside WriteConsole but that doesn't even catch printf output from the CRT. 我已经尝试在WriteConsole设置一个断点,但是甚至没有从CRT捕获printf输出。 Is there any "central" location to set a breakpoint to catch all console output in a Windows application? 是否有任何“中心”位置来设置断点以捕获Windows应用程序中的所有控制台输出? (Some Nt... function?) (有些Nt...功能?)

Console I/O makes calls to ReadFile and WriteFile on Windows. 控制台I / O在Windows上调用ReadFile和WriteFile。 WriteFile is the routine for console output. WriteFile是控制台输出的例程。 It would be best to hook or breakpoint WriteFile and filter the handle specified to it. 最好挂钩或断点WriteFile并过滤指定给它的句柄。 Don't mistake actual File I/O for operations on a console handle. 不要将实际的文件I / O误认为是控制台句柄上的操作。

To acquire the correct handle used for console output on a Windows process: 要获取Windows进程上用于控制台输出的正确句柄:

GetStdHandle(STD_OUTPUT_HANDLE);

Alternatively, opening the console output handle using CreateFile and specifying CONOUT$ works and is also recommended: 或者,使用CreateFile打开控制台输出句柄并指定CONOUT $工作,也建议:

HANDLE console_output = CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

This will return the handle you need to watch for in WriteFile calls. 这将返回WriteFile调用中需要注意的句柄。 Just like with a hook though, you can specify the conditions for your breakpoint and check for the correct console handle in the call. 就像使用钩子一样,您可以指定断点的条件并检查调用中的正确控制台句柄。

There is more about operations on console handles using ReadFile/WriteFile in the Microsoft documentation Microsoft文档中 ,有关使用ReadFile / WriteFile的控制台句柄操作的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM