Skip to content
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

Add etw UnicodeStringField #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/etw/eventdata.go
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"encoding/binary"
"syscall"
"unicode/utf16"
)

// eventData maintains a buffer which builds up the data for an ETW event. It
Expand All @@ -28,6 +29,13 @@ func (ed *eventData) writeString(data string) {
_ = ed.buffer.WriteByte(0)
}

// writeUnicodeString appends a string converted to UTF-16, including the null terminator, to the buffer.
func (ed *eventData) writeUnicodeString(data string) {
unicode := utf16.Encode([]rune(data))
binary.Write(&ed.buffer, binary.LittleEndian, unicode)
ed.buffer.Write([]byte{0, 0})
Comment on lines +35 to +36
Copy link
Contributor

Choose a reason for hiding this comment

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

to suppress the linter on error checking (or you can add a //nolint:errcheck directive)

Suggested change
binary.Write(&ed.buffer, binary.LittleEndian, unicode)
ed.buffer.Write([]byte{0, 0})
_ = binary.Write(&ed.buffer, binary.LittleEndian, unicode)
_, _ = ed.buffer.Write([]byte{0, 0})

}

// writeInt8 appends a int8 to the buffer.
func (ed *eventData) writeInt8(value int8) {
_ = ed.buffer.WriteByte(uint8(value))
Expand Down
19 changes: 19 additions & 0 deletions pkg/etw/fieldopt.go
Expand Up @@ -64,6 +64,14 @@ func JSONStringField(name string, value string) FieldOpt {
}
}

// UnicodeStringField adds a single UTF-16 string field to the event.
func UnicodeStringField(name string, value string) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeUnicodeString, outTypeString, 0)
helsaawy marked this conversation as resolved.
Show resolved Hide resolved
ed.writeUnicodeString(value)
}
}

// StringArray adds an array of string to the event.
func StringArray(name string, values []string) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
Expand All @@ -75,6 +83,17 @@ func StringArray(name string, values []string) FieldOpt {
}
}

// UnicodeStringArray adds an array of UTF-16 strings to the event.
func UnicodeStringArray(name string, values []string) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeUnicodeString, outTypeString, 0)
Copy link
Member

Choose a reason for hiding this comment

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

Do we want the out type here? It looks like TraceLoggingWideString in TraceLoggingProvider.h just uses TlgInUNICODESTRING with no out type.

Copy link
Author

Choose a reason for hiding this comment

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

Please see below.

ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeUnicodeString(v)
}
}
}

// IntField adds a single int field to the event.
func IntField(name string, value int) FieldOpt {
switch unsafe.Sizeof(value) {
Expand Down