summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/util/u_debug_refcnt.c
blob: 6f706a35fda355232bd980c4518c619e408cd0b3 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**************************************************************************
 *
 * Copyright 2010 Luca Barbieri
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#if defined(DEBUG) && (!defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_WINDOWS_USER))

/* see http://www.mozilla.org/performance/refcnt-balancer.html for what do with the output
 * on Linux, use tools/addr2line.sh to postprocess it before anything else
 **/
#include <util/u_debug.h>
#include <util/u_debug_refcnt.h>
#include <util/u_debug_stack.h>
#include <util/u_debug_symbol.h>
#include <util/u_string.h>
#include <util/u_hash_table.h>
#include <os/os_thread.h>
#include <os/os_stream.h>

int debug_refcnt_state;

struct os_stream* stream;

/* TODO: maybe move this serial machinery to a stand-alone module and expose it? */
pipe_static_mutex(serials_mutex);

static struct util_hash_table* serials_hash;
static unsigned serials_last;

static unsigned hash_ptr(void* p)
{
   return (unsigned)(uintptr_t)p;
}

static int compare_ptr(void* a, void* b)
{
   if(a == b)
      return 0;
   else if(a < b)
      return -1;
   else
      return 1;
}

static boolean debug_serial(void* p, unsigned* pserial)
{
   unsigned serial;
   boolean found = TRUE;
#ifdef PIPE_SUBSYSTEM_WINDOWS_USER
   static boolean first = TRUE;

   if (first) {
      pipe_mutex_init(serials_mutex);
      first = FALSE;
   }
#endif

   pipe_mutex_lock(serials_mutex);
   if(!serials_hash)
      serials_hash = util_hash_table_create(hash_ptr, compare_ptr);
   serial = (unsigned)(uintptr_t)util_hash_table_get(serials_hash, p);
   if(!serial)
   {
      /* time to stop logging... (you'll have a 100 GB logfile at least at this point)
       * TODO: avoid this
       */
      serial = ++serials_last;
      if(!serial)
      {
         debug_error("More than 2^32 objects detected, aborting.\n");
         os_abort();
      }

      util_hash_table_set(serials_hash, p, (void*)(uintptr_t)serial);
      found = FALSE;
   }
   pipe_mutex_unlock(serials_mutex);
   *pserial = serial;
   return found;
}

static void debug_serial_delete(void* p)
{
   pipe_mutex_lock(serials_mutex);
   util_hash_table_remove(serials_hash, p);
   pipe_mutex_unlock(serials_mutex);
}

#define STACK_LEN 64

static void dump_stack(const char* symbols[STACK_LEN])
{
   unsigned i;
   for(i = 0; i < STACK_LEN; ++i)
   {
      if(symbols[i])
         os_stream_printf(stream, "%s\n", symbols[i]);
   }
   os_stream_write(stream, "\n", 1);
}

void debug_reference_slowpath(const struct pipe_reference* p, debug_reference_descriptor get_desc, int change)
{
   if(debug_refcnt_state < 0)
      return;

   if(!debug_refcnt_state)
   {
      const char* filename = debug_get_option("GALLIUM_REFCNT_LOG", NULL);
      if(filename && filename[0])
         stream = os_file_stream_create(filename);

      if(stream)
         debug_refcnt_state = 1;
      else
         debug_refcnt_state = -1;
   }

   if(debug_refcnt_state > 0)
   {
      struct debug_stack_frame frames[STACK_LEN];
      const char* symbols[STACK_LEN];
      char buf[1024];

      unsigned i;
      unsigned refcnt = p->count;
      unsigned serial;
      boolean existing = debug_serial((void*)p, &serial);

      debug_backtrace_capture(frames, 1, STACK_LEN);
      for(i = 0; i < STACK_LEN; ++i)
      {
         if(frames[i].function)
            symbols[i] = debug_symbol_name_cached(frames[i].function);
         else
            symbols[i] = 0;
      }

      get_desc(buf, p);

      if(!existing)
      {
         os_stream_printf(stream, "<%s> %p %u Create\n", buf, p, serial);
         dump_stack(symbols);

         /* this is there to provide a gradual change even if we don't see the initialization */
         for(i = 1; i <= refcnt - change; ++i)
         {
            os_stream_printf(stream, "<%s> %p %u AddRef %u\n", buf, p, serial, i);
            dump_stack(symbols);
         }
      }

      if(change)
      {
         os_stream_printf(stream, "<%s> %p %u %s %u\n", buf, p, serial, change > 0 ? "AddRef" : "Release", refcnt);
         dump_stack(symbols);
      }

      if(!refcnt)
      {
         debug_serial_delete((void*)p);
         os_stream_printf(stream, "<%s> %p %u Destroy\n", buf, p, serial);
         dump_stack(symbols);
      }

      os_stream_flush(stream);
   }
}
#endif