У меня есть код, который печатает некоторую структуру данных.
void
print_wheel_timer(wheel_timer_t *wt){
<code to print data structure>
}
Then i sandwiched the code in-between lock and unlock mutex calls.
void
print_wheel_timer(wheel_timer_t *wt){
pthread_mutex_lock(&wt->global_lock);
<code to print data structure>
pthread_mutex_unlock(&wt->global_lock);
}
А теперь, это segfault. Я отключил все потоки в коде, и теперь программа однопоточная, все еще segfault. Я использую gcc, Ubuntu 19.04.
GDB показывает pthread_mutex_lock()
вызов испортить структуры данных !! Это странно! Я ставлю точку наблюдения на адрес, который записывается произвольным значением, и он показывает, что вызов pthread_mutex_lock()
изменяет его. Когда я удаляю эти вызовы блокировки / разблокировки, сегфоутов не видно.
(gdb) watch *(glthread_t *)&(wt->slotlist[1].slots.right)
Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)
(gdb) c
Continuing.
Printing Wheel Timer DS
Thread 1 "test.exe" hit Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)
Old value = {left = 0x0, right = 0x0}
New value = {left = 0x1, right = 0x0} <<< corruption !!
0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
80 ../nptl/pthread_mutex_lock.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
#1 0x000055555555f3a2 in print_wheel_timer (wt=0x555555577180)
at WheelTimer/WheelTimer.c:276
Code :
========
void
print_wheel_timer(wheel_timer_t *wt){
int i = 0, j = 0;
glthread_t *curr;
glthread_t *slot_list_head = NULL;
wheel_timer_elem_t *wt_elem = NULL;
printf("Printing Wheel Timer DS\n");
pthread_mutex_lock(&wt->global_lock);
printf("wt->current_clock_tic = %d\n", wt->current_clock_tic);
printf("wt->clock_tic_interval = %d\n", wt->clock_tic_interval);
printf("wt->wheel_size = %d\n", wt->wheel_size);
printf("wt->current_cycle_no = %d\n", wt->current_cycle_no);
printf("wt->wheel_thread = %p\n", &wt->wheel_thread);
printf("WT uptime = %s\n", hrs_min_sec_format(WT_UPTIME(wt)));
printf("wt->no_of_wt_elem = %u\n", wt->no_of_wt_elem);
printf("printing slots : \n");
for(; i < wt->wheel_size; i++){
slot_list_head = WT_SLOTLIST_HEAD(wt, i);
ITERATE_GLTHREAD_BEGIN(slot_list_head, curr){ << segfaulting here for i = 1
wt_elem = glthread_to_wt_elem(curr);
Если какая-то святая душа хочет попробовать это на своей машине. Пожалуйста, загрузите код здесь: https://github.com/sachinites/tcpip_stack
After downloading :
switch to branch "Hellos"
git checkout Hellos
Compile :
make all
run
./test.exe
to reproduce , run the cmd :
debug show node H1 timer
Теперь это будет ядро в функции: print_wheel_timer()
реализовано в WheelTimer / WheelTimer.c