summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/winbindd_traceid.c
blob: acf16bec3a425228c5cd79f9e2f05cd8b95260fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2021 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "lib/util/debug.h"
#include "winbindd_traceid.h"
#include "tevent.h"

static void debug_traceid_trace_fde(struct tevent_fd *fde,
                                   enum tevent_event_trace_point point,
                                   void *private_data)
{
    switch (point) {
    case TEVENT_EVENT_TRACE_ATTACH:
        /* Assign the current traceid id when the event is created. */
        tevent_fd_set_tag(fde, debug_traceid_get());
        break;
    case TEVENT_EVENT_TRACE_BEFORE_HANDLER:
        /* Set the traceid id when a handler is being called. */
        debug_traceid_set(tevent_fd_get_tag(fde));
        break;
    default:
        /* Do nothing. */
        break;
    }
}

static void debug_traceid_trace_signal(struct tevent_signal *se,
                                      enum tevent_event_trace_point point,
                                      void *private_data)
{
    switch (point) {
    case TEVENT_EVENT_TRACE_ATTACH:
        /* Assign the current traceid id when the event is created. */
        tevent_signal_set_tag(se, debug_traceid_get());
        break;
    case TEVENT_EVENT_TRACE_BEFORE_HANDLER:
        /* Set the traceid id when a handler is being called. */
        debug_traceid_set(tevent_signal_get_tag(se));
        break;
    default:
        /* Do nothing. */
        break;
    }
}

static void debug_traceid_trace_timer(struct tevent_timer *timer,
                                     enum tevent_event_trace_point point,
                                     void *private_data)
{
    switch (point) {
    case TEVENT_EVENT_TRACE_ATTACH:
        /* Assign the current traceid id when the event is created. */
        tevent_timer_set_tag(timer, debug_traceid_get());
        break;
    case TEVENT_EVENT_TRACE_BEFORE_HANDLER:
        /* Set the traceid id when a handler is being called. */
        debug_traceid_set(tevent_timer_get_tag(timer));
        break;
    default:
        /* Do nothing. */
        break;
    }
}

static void debug_traceid_trace_immediate(struct tevent_immediate *im,
                                         enum tevent_event_trace_point point,
                                         void *private_data)
{
    switch (point) {
    case TEVENT_EVENT_TRACE_ATTACH:
        /* Assign the current traceid id when the event is created. */
        tevent_immediate_set_tag(im, debug_traceid_get());
        break;
    case TEVENT_EVENT_TRACE_BEFORE_HANDLER:
        /* Set the traceid id when a handler is being called. */
        debug_traceid_set(tevent_immediate_get_tag(im));
        break;
    default:
        /* Do nothing. */
        break;
    }
}

static void debug_traceid_trace_queue(struct tevent_queue_entry *qe,
                                         enum tevent_event_trace_point point,
                                         void *private_data)
{
    switch (point) {
    case TEVENT_EVENT_TRACE_ATTACH:
        /* Assign the current traceid id when the event is created. */
        tevent_queue_entry_set_tag(qe, debug_traceid_get());
        break;
    case TEVENT_EVENT_TRACE_BEFORE_HANDLER:
        /* Set the traceid id when a handler is being called. */
        debug_traceid_set(tevent_queue_entry_get_tag(qe));
        break;
    default:
        /* Do nothing. */
        break;
    }
}

static void debug_traceid_trace_loop(enum tevent_trace_point point,
                                    void *private_data)
{
    switch (point) {
    case TEVENT_TRACE_AFTER_LOOP_ONCE:
        /* Reset traceid id when we got back to the loop. An event handler
         * that set traceid id was fired. This tracepoint represents a place
         * after the event handler was finished, we need to restore traceid
         * id to 1 (out of request). 0 means not initialized.
         */
        debug_traceid_set(1);
        break;
    default:
        /* Do nothing. */
        break;
    }
}

void winbind_debug_traceid_setup(struct tevent_context *ev)
{
    tevent_set_trace_callback(ev, debug_traceid_trace_loop, NULL);
    tevent_set_trace_fd_callback(ev, debug_traceid_trace_fde, NULL);
    tevent_set_trace_signal_callback(ev, debug_traceid_trace_signal, NULL);
    tevent_set_trace_timer_callback(ev, debug_traceid_trace_timer, NULL);
    tevent_set_trace_immediate_callback(ev, debug_traceid_trace_immediate, NULL);
    tevent_set_trace_queue_callback(ev, debug_traceid_trace_queue, NULL);
    debug_traceid_set(1);
}