Search code examples
c#.netlttngbabeltrace

How to get ThreadId during tracing EventSources with lttng


USING: .NET Core 6 on linux / lttng / babeltrace 2

I use lttng to collect the information about functions which were called while my program is running. I want to understand in which thread function was called.

I run this script

export COMPlus_PerfMapEnabled=1
export COMPlus_EnableEventLog=1
 
lttng create my-session -o ./my-trace
lttng enable-channel --userspace DotNetCoreChannel
lttng enable-event --userspace --tracepoint DotNETRuntime:EventSource --channel=DotNetCoreChannel --filter "EventSourceName=='Trace'"
lttng start
dotnet run 
lttng stop
lttng view
lttng destroy my-session

and get traces like this

[22:05:00.402124499] (+?.?????????) userVM DotNETRuntime:EventSource: { cpu_id = 1 }, { EventID = 1, EventName = "Enter", EventSourceName = "Trace", Payload = "" }
[22:05:00.429636742] (+0.000147700) userVM DotNETRuntime:EventSource: { cpu_id = 1 }, { EventID = 2, EventName = "Leave", EventSourceName = "Trace", Payload = "" }

Firstly I thought that cpu_id means thread number, but when I ran on a bigger program, I got the trace, where Enter and Leave have different cpu_ids, that is why I am looking for the way to get threadIds in trace


Solution

  • From the LTTing docs Add context fields to be recorded to the event records of a channel:

    Event record fields in trace files provide important information about previously emitted events, but sometimes some external context may help you solve a problem faster.

    Examples of context fields are:

    • The process ID, thread ID, process name, and process priority of the thread from which LTTng emits the event.

    And then follows up with:

    Example: Add context fields to be recorded to the event records of a specific channel. The following command line adds the thread identifier and user call stack context fields to the Linux kernel channel named my-channel of the current recording session.

    lttng add-context --kernel --channel=my-channel \
                      --type=tid --type=callstack-user
    

    So, it appears if you add --type=tid to the channel, you should get the thread id. (edit to include comment info):

    lttng enable-channel --userspace DotNetCoreChannel
    lttng add-context --userspace --channel=DotNetCoreChannel --type=vtid