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

时间:2014-03-10 09:53:05

标签: c++ gdb semaphore ace

我正在尝试使用ACE Semaphore库实现线程池。它不提供像Posix信号量中的sem_getvalue这样的API。我需要调试一些不按预期运行的流程。我可以检查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)

1 个答案:

答案 0 :(得分:0)

代码中的

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

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

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