在GDB中打印或检查信号量计数值



我正在尝试使用ACE信号量库实现线程池。它不提供任何API,如sem_getvalue是Posix信号量。我需要调试一些不按预期行为的流。我可以检查GDB中的信号量吗?我使用Centos作为操作系统。

使用提供计数0和10的默认构造函数初始化了两个信号量。我在类中将它们声明为静态,并在cpp文件中将其初始化为

DP_Semaphore ThreadPool::availableThreads(10);
DP_Semaphore ThreadPool::availableWork(0);

但是当我使用print命令在GDB中打印信号量时,我得到了类似的输出

(gdb) p this->availableWork
$7 = {
  sema = {
    semaphore_ = {
      sema_ = 0x6fe5a0,
      name_ = 0x0
    },
    removed_ = false
  }
}
(gdb) p this->availableThreads
$8 = {
  sema = {
    semaphore_ = {
      sema_ = 0x6fe570,
      name_ = 0x0
    },
    removed_ = false
  }
}

有一个工具可以帮助我这里,或者我应该切换到Posix线程和重写我所有的代码。

编辑:根据@timrau的要求,呼叫this->availableWork->dump()的输出

(gdb) p this->availableWork.dump()
[Switching to Thread 0x2aaaae97e940 (LWP 28609)]
The program stopped in another thread while making a function call from GDB.
Evaluation of the expression containing the function
(DP_Semaphore::dump()) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb) call this->availableWork.dump()
[Switching to Thread 0x2aaaaf37f940 (LWP 28612)]
The program stopped in another thread while making a function call from GDB.
Evaluation of the expression containing the function
(DP_Semaphore::dump()) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb) info threads
[New Thread 0x2aaaafd80940 (LWP 28613)]
  6 Thread 0x2aaaafd80940 (LWP 28613)  0x00002aaaac10a61e in __lll_lock_wait_private ()
   from /lib64/libpthread.so.0
* 5 Thread 0x2aaaaf37f940 (LWP 28612)  ThreadPool::fetchWork (this=0x78fef0, worker=0x2aaaaf37f038)
    at ../../CallManager/src/DP_CallControlTask.cpp:1043
  4 Thread 0x2aaaae97e940 (LWP 28609)  DP_Semaphore::dump (this=0x6e1460) at ../../Common/src/DP_Semaphore.cpp:21
  2 Thread 0x2aaaad57c940 (LWP 28607)  0x00002aaaabe01ff3 in __find_specmb () from /lib64/libc.so.6
  1 Thread 0x2aaaacb7b070 (LWP 28604)  0x00002aaaac1027c0 in __nptl_create_event () from /lib64/libpthread.so.0
(gdb)

sema.semaphore_.sema_在您的代码中看起来像一个指针。尝试在ACE标头中找到它的类型,然后将其转换为类型并打印:

(gdb) p *((sem_t)0x6fe570)

更新:尝试转换结构中的地址,你发布到sem_t。如果您使用linux, ACE应该使用posix信号量,因此类型sem_t必须对gdb可见。

最新更新