Skip to content

[windows][lldb] implement system logging on Windows #11017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: swift/release/6.2
Choose a base branch
from

Conversation

charles-zablit
Copy link

@charles-zablit charles-zablit commented Jul 17, 2025

This patch makes LLDB use system logging on Windows rather than piping to the standard output.

Darwin, Linux and FreeBSD based systems pipe the health logs to system events. On Windows that's not the case, everything is redirected to the standard output/error. This leads to confusing diagnostics messages when running the REPL for example. This is fixes with this PR.

rdar://156039517

@charles-zablit charles-zablit self-assigned this Jul 17, 2025
@charles-zablit charles-zablit requested a review from a team as a code owner July 17, 2025 10:10
@charles-zablit
Copy link
Author

Before

Screenshot 2025-07-17 at 10 46 08

After

Screenshot 2025-07-17 at 10 42 45

@charles-zablit
Copy link
Author

@swift-ci please test

@charles-zablit
Copy link
Author

@swift-ci please test

}

WORD event_type;
WORD event_id;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to be set in the switch case but I forgot to remove the declaration. Fixed!

@charles-zablit
Copy link
Author

@swift-ci please test

return nullptr;
const int length = strlen(ansi);
const int unicode_length =
MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can omit length entirely here and replace it with -1. Apparently that tells MultiByteToWideChar that the string is null-terminated, and it will add the null-terminator itself.

So you don't need to reserve that extra + 1 length and don't need to null-terminate on line `334.

Concretely, could we just change the length parameters in both calls to MultiByteToWideChar to -1?

Tbf, I'm not super familiar with these APIs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this I would add a comment to this function saying it expects a null-terminated string

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reused the snippet in llvm-project/third-party/unittest/googletest/src/gtest.cc. I imagine that it assumed the char * could be missing a null terminator.

I've changed the cbMultiByte parameter to take a -1 instead of the length and removed the +1 as well.

This works fine when testing on Windows.


class WindowsEventLog {
public:
WindowsEventLog() { handle = RegisterEventSource(nullptr, L"LLDB"); }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WindowsEventLog() { handle = RegisterEventSource(nullptr, L"LLDB"); }
WindowsEventLog() : handle(RegisterEventSource(nullptr, L"LLDB")) {}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks 👍

LPCWSTR wide_message = AnsiToUtf16(message.str().c_str());

if (!h || !wide_message) {
switch (severity) {
Copy link

@Michael137 Michael137 Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if !h but we allocated wide_message, then we would leak memory. Can we make the UTF-16 string a unique_ptr?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks 👍

Copy link

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a great idea

Should we upstream this first?

@charles-zablit
Copy link
Author

Should we upstream this first?

The implementation of SystemLog is empty upstream:
https://github.com/llvm/llvm-
project/blob/145b6cdffaf6711a5b7ad191444ab3e5d97b8992/lldb/source/Host/common/Host.cpp#L86-L90

However I think that the comment is not accurate anymore: there is an implementation for Darwin through os_log, and there is also an implementation for linux and freebsd using syslog.h. And this patch introduces an implementation for windows as well now.

@Michael137
Copy link

Should we upstream this first?

The implementation of SystemLog is empty upstream: https://github.com/llvm/llvm- project/blob/145b6cdffaf6711a5b7ad191444ab3e5d97b8992/lldb/source/Host/common/Host.cpp#L86-L90

However I think that the comment is not accurate anymore: there is an implementation for Darwin through os_log, and there is also an implementation for linux and freebsd using syslog.h. And this patch introduces an implementation for windows as well now.

Yea it would make sense to have this upstream. And yea i think that comment is stale

@charles-zablit
Copy link
Author

Should we upstream this first?

The implementation of SystemLog is empty upstream: https://github.com/llvm/llvm- project/blob/145b6cdffaf6711a5b7ad191444ab3e5d97b8992/lldb/source/Host/common/Host.cpp#L86-L90
However I think that the comment is not accurate anymore: there is an implementation for Darwin through os_log, and there is also an implementation for linux and freebsd using syslog.h. And this patch introduces an implementation for windows as well now.

Yea it would make sense to have this upstream. And yea i think that comment is stale

The linux implementation was removed 9 months ago because the use of system log is too heterogeneous on Linux, from this comment. Windows logging was made a NOOP upstream here: llvm#112052.

It seems like Linux and Windows system logging were made NOOPs less than a year ago. Should we revert both of them (or at least Windows), now that we can log to the System's Log on Windows?

cc @JDevlieghere

@compnerd
Copy link
Member

Should this really do to the event logger? Are these actionable by the system administrators? I think it's better to push these into the lossy logger like the kernel debug log. Use OutputDebugStringW instead. The health check log can be attached to a system event.

@charles-zablit
Copy link
Author

Should this really do to the event logger?

The Event Viewer on Windows seems like the closest thing to System Logs on macOS and syslog.h utilities on Linux, which are already implemented. It was suggested in this comment.

Are these actionable by the system administrators?

It would display the same messages that get displayed on Linux and macOS (i.e swift version, etc). They are very useful on macOS.

I think it's better to push these into the lossy logger like the kernel debug log

I'm not sure I understand what you mean by kernel debug log. Is that in lldb?

Use OutputDebugStringW instead. The health check log can be attached to a system event.

From my understanding of the windows documentation, this function passes the output to the debugger, not the system logs or an equivalent. If I understand correctly, this is a major difference with the already implemented behaviors on Linux and macOS.

@JDevlieghere
Copy link

The linux implementation was removed 9 months ago because the use of system log is too heterogeneous on Linux, from this comment. Windows logging was made a NOOP upstream here: llvm#112052.

It seems like Linux and Windows system logging were made NOOPs less than a year ago. Should we revert both of them (or at least Windows), now that we can log to the System's Log on Windows?

cc @JDevlieghere

Yes, if there's an appropriate place for the system log to go, then we should definitely use that upstream. I only removed it because the things we wanted to log to the system log would have caused too much disruption when printing to stdio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants