added mutex debugger
[NeonServV5.git] / src / mutexDebug.c
1 /* mutexDebug.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "main.h"
18 #include "mutexDebug.h"
19
20 #ifdef ENABLE_MUTEX_DEBUG
21
22 struct MutexLockEvent {
23     int locked;
24     const char *file;
25     unsigned int line;
26     struct MutexLockEvent *next;
27 };
28
29 struct MutexLock {
30     int thread;
31     int count;
32     struct MutexLockEvent *first_event, *last_event;
33     struct MutexLock *prev, *next;
34 };
35
36 struct MutexNode {
37     pthread_mutex_t *mutex;
38     struct MutexLock *first_lock, *last_lock;
39     struct MutexNode *next;
40 };
41
42 static pthread_mutex_t synchronized;
43 static struct MutexNode *mutex_nodes = NULL;
44
45 static struct MutexNode *getMutexNode(pthread_mutex_t *mutex, int create);
46 static void lockMutex(struct MutexNode *node, const char *file, unsigned int line);
47 static void unlockMutex(struct MutexNode *node, const char *file, unsigned int line);
48
49 void xmutex(int lock, pthread_mutex_t *mutex, const char *file, unsigned int line) {
50     pthread_mutex_lock(&synchronized);
51     struct MutexNode *node = getMutexNode(mutex, 1);
52     if(lock)
53         lockMutex(node, file, line);
54     else
55         unlockMutex(node, file, line);
56     pthread_mutex_unlock(&synchronized);
57 }
58
59 void initMutexDebug() {
60     THREAD_MUTEX_INIT(synchronized);
61 }
62
63 static struct MutexNode *getMutexNode(pthread_mutex_t *mutex, int create) {
64     struct MutexNode *node;
65     for(node = mutex_nodes; node; node = node->next) {
66         if(node->mutex == mutex)
67             return node;
68     }
69     if(!create)
70         return NULL;
71     node = malloc(sizeof(*node));
72     node->first_lock = NULL;
73     node->last_lock = NULL;
74     node->mutex = mutex;
75     node->next = mutex_nodes;
76     mutex_nodes = node;
77     return node;
78 }
79
80 static void lockMutex(struct MutexNode *node, const char *file, unsigned int line) {
81     struct MutexLock *lock;
82     int thread = getCurrentThreadID();
83     for(lock = node->first_lock; lock; lock = lock->next) {
84         if(lock->thread == thread)
85             break;
86     }
87     if(!lock) {
88         lock = malloc(sizeof(*lock));
89         lock->thread = thread;
90         lock->count = 0;
91         lock->first_event = NULL;
92         lock->last_event = NULL;
93         lock->prev = node->last_lock;
94         lock->next = NULL;
95         node->last_lock = lock;
96         if(!node->first_lock)
97             node->first_lock = lock;
98     }
99     lock->count++;
100     //add event
101     struct MutexLockEvent *event = malloc(sizeof(*event));
102     event->locked = 1;
103     event->file = file;
104     event->line = line;
105     event->next = NULL;
106     if(lock->last_event) {
107         lock->last_event->next = event;
108         lock->last_event = event;
109     } else {
110         lock->first_event = event;
111         lock->last_event = event;
112     }
113 }
114
115 static void unlockMutex(struct MutexNode *node, const char *file, unsigned int line) {
116     struct MutexLock *lock;
117     int thread = getCurrentThreadID();
118     for(lock = node->first_lock; lock; lock = lock->next) {
119         if(lock->thread == thread)
120             break;
121     }
122     if(!lock)
123         return;
124     lock->count--;
125     if(lock->count <= 0) {
126         //remove lock
127         if(lock->prev)
128             lock->prev->next = lock->next;
129         else
130             node->first_lock = lock->next;
131         if(lock->next)
132             lock->next->prev = lock->prev;
133         else
134             node->last_lock = lock->prev;
135         //recursive free all events
136         struct MutexLockEvent *event, *next_event;
137         for(event = lock->first_event; event; event = next_event) {
138             next_event = event->next;
139             free(event);
140         }
141         free(lock);
142     } else {
143         //add event
144         struct MutexLockEvent *event = malloc(sizeof(*event));
145         event->locked = 0;
146         event->file = file;
147         event->line = line;
148         event->next = NULL;
149         if(lock->last_event) {
150             lock->last_event->next = event;
151             lock->last_event = event;
152         } else {
153             lock->first_event = event;
154             lock->last_event = event;
155         }
156     }
157 }
158
159 void mutex_debug(pthread_mutex_t *mutex) {
160     //replay mutex events to stdout
161     struct MutexNode *node = getMutexNode(mutex, 0);
162     if(!node) {
163         printf("[MUTEX_DEBUG] unknown mutex!\n");
164         return;
165     }
166     printf("[MUTEX_DEBUG] mutex replay:\n");
167     struct MutexLock *lock;
168     struct MutexLockEvent *event;
169     for(lock = node->first_lock; lock; lock = lock->next) {
170         printf("[MUTEX_DEBUG]  THREAD %d (%d locks):\n", lock->thread, lock->count);
171         for(event = lock->first_event; event; event = event->next) {
172             printf("[MUTEX_DEBUG]   %s in %s:%d\n", (event->locked ? "lock  " : "unlock"), event->file, event->line);
173         }
174     }
175     printf("[MUTEX_DEBUG] end of mutex replay.\n");
176 }
177
178 #endif