eventdata.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //go:build windows
  2. // +build windows
  3. package etw
  4. import (
  5. "bytes"
  6. "encoding/binary"
  7. "syscall"
  8. )
  9. // eventData maintains a buffer which builds up the data for an ETW event. It
  10. // needs to be paired with EventMetadata which describes the event.
  11. type eventData struct {
  12. buffer bytes.Buffer
  13. }
  14. // toBytes returns the raw binary data containing the event data. The returned
  15. // value is not copied from the internal buffer, so it can be mutated by the
  16. // eventData object after it is returned.
  17. func (ed *eventData) toBytes() []byte {
  18. return ed.buffer.Bytes()
  19. }
  20. // writeString appends a string, including the null terminator, to the buffer.
  21. func (ed *eventData) writeString(data string) {
  22. _, _ = ed.buffer.WriteString(data)
  23. _ = ed.buffer.WriteByte(0)
  24. }
  25. // writeInt8 appends a int8 to the buffer.
  26. func (ed *eventData) writeInt8(value int8) {
  27. _ = ed.buffer.WriteByte(uint8(value))
  28. }
  29. // writeInt16 appends a int16 to the buffer.
  30. func (ed *eventData) writeInt16(value int16) {
  31. _ = binary.Write(&ed.buffer, binary.LittleEndian, value)
  32. }
  33. // writeInt32 appends a int32 to the buffer.
  34. func (ed *eventData) writeInt32(value int32) {
  35. _ = binary.Write(&ed.buffer, binary.LittleEndian, value)
  36. }
  37. // writeInt64 appends a int64 to the buffer.
  38. func (ed *eventData) writeInt64(value int64) {
  39. _ = binary.Write(&ed.buffer, binary.LittleEndian, value)
  40. }
  41. // writeUint8 appends a uint8 to the buffer.
  42. func (ed *eventData) writeUint8(value uint8) {
  43. _ = ed.buffer.WriteByte(value)
  44. }
  45. // writeUint16 appends a uint16 to the buffer.
  46. func (ed *eventData) writeUint16(value uint16) {
  47. _ = binary.Write(&ed.buffer, binary.LittleEndian, value)
  48. }
  49. // writeUint32 appends a uint32 to the buffer.
  50. func (ed *eventData) writeUint32(value uint32) {
  51. _ = binary.Write(&ed.buffer, binary.LittleEndian, value)
  52. }
  53. // writeUint64 appends a uint64 to the buffer.
  54. func (ed *eventData) writeUint64(value uint64) {
  55. _ = binary.Write(&ed.buffer, binary.LittleEndian, value)
  56. }
  57. // writeFiletime appends a FILETIME to the buffer.
  58. func (ed *eventData) writeFiletime(value syscall.Filetime) {
  59. _ = binary.Write(&ed.buffer, binary.LittleEndian, value)
  60. }