c++ - 如何调试 Cygwin 故障?

标签 c++ gdb cygwin porting botan

我正在从 Centos 移植到 Cygwin,发现我的应用程序在 Botan::InitializationVector 的构造函数执行期间没有错误消息退出,退出状态为零。

如果我尝试在等待自旋变量的 main() 中主动附加 gdb,我不会得到正常的堆栈跟踪:

(gdb) where
#0  0x7c90120f in ntdll!DbgUiConnectToDbg ()
   from /cygdrive/c/WINDOWS/system32/ntdll.dll
#1  0x7c952119 in ntdll!KiIntSystemCall ()
   from /cygdrive/c/WINDOWS/system32/ntdll.dll
#2  0x00000005 in ?? ()
#3  0x00000000 in ?? ()

所以没有 gdb,很难找出问题所在。

为什么我在 Cygwin 上没有收到错误消息,但应用程序会在执行过程中退出?

我推断它在构造函数内部,因为阻塞只显示在构造函数之前而不是之后的行:

clog << "  About to create iv for Botan.\n";
Botan::InitializationVector iv(_rng, size);
clog << "  About to copy iv for Botan.\n";

Botan 开源:http://botan.randombit.net/以下是 src/sym_algo/symkey.{h,cpp} 中的一些代码片段:

typedef OctetString InitializationVector;

class BOTAN_DLL OctetString
{
public:
  u32bit length() const { return bits.size(); }
  SecureVector<byte> bits_of() const { return bits; }

  const byte* begin() const { return bits.begin(); }
  const byte* end() const   { return bits.end(); }

  std::string as_string() const;

  OctetString& operator^=(const OctetString&);

  void set_odd_parity();

  void change(const std::string&);
  void change(const byte[], u32bit);
  void change(const MemoryRegion<byte>& in) { bits = in; }

  OctetString(class RandomNumberGenerator&, u32bit len);
  OctetString(const std::string& str = "") { change(str); }
  OctetString(const byte in[], u32bit len) { change(in, len); }
  OctetString(const MemoryRegion<byte>& in) { change(in); }
private:
  SecureVector<byte> bits;
};

OctetString::OctetString(RandomNumberGenerator& rng,
                     u32bit length)
{
   bits.create(length);
   rng.randomize(bits, length);
}

我将失败的代码移到了 main() 中,它工作正常。我还在代码周围放了一个 try catch ... 并且没有抛出异常。 main() 和应用程序后面的故障点之间出现问题。我可以分而治之以缩小它不再起作用的确切点。 Botan 的一位开发人员给了我这个精简的代码来代替,但它也失败了:

Botan::AutoSeeded_RNG _rng;
unsigned int size = 1; // or 16, or 1452 all fail.
Botan::SecureVector<Botan::byte> iv_val(size);
cerr << "We get to here." << endl;
_rng.randomize(&iv_val[0], size);
cerr << "But not here." << endl;

现在调试器开始工作了,我看到了 segv:

(gdb) s
Botan::AutoSeeded_RNG::randomize (this=0x1270380, out=0x5841420 "", len=1)
    at ../../src/Botan-1.8.11/build/include/botan/auto_rng.h:23
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x005d79ee in Botan::AutoSeeded_RNG::randomize (this=0x1270380, 
    out=0x5841420 "", len=1)
    at ../../src/Botan-1.8.11/build/include/botan/auto_rng.h:23
(gdb) p rng
$7 = (class Botan::RandomNumberGenerator *) 0x5841324
(gdb) p *this
$8 = {<Botan::RandomNumberGenerator> = {
    _vptr$RandomNumberGenerator = 0x11efc14}, rng = 0x5841324}
(gdb) p *rng
$9 = {_vptr$RandomNumberGenerator = 0x656e6f4e}

这里是 auto_rng.h 代码:

class BOTAN_DLL AutoSeeded_RNG : public RandomNumberGenerator
  {
  public:
    void randomize(byte out[], u32bit len)
    { rng->randomize(out, len); }           // SEGV on this line.
    bool is_seeded() const
    { return rng->is_seeded(); }
    void clear() throw() { rng->clear(); }
    std::string name() const
    { return "AutoSeeded(" + rng->name() + ")"; }

    void reseed(u32bit poll_bits = 256) { rng->reseed(poll_bits); }
    void add_entropy_source(EntropySource* es)
    { rng->add_entropy_source(es); }
    void add_entropy(const byte in[], u32bit len)
    { rng->add_entropy(in, len); }

    AutoSeeded_RNG(u32bit poll_bits = 256);
    ~AutoSeeded_RNG() { delete rng; }
  private:
    RandomNumberGenerator* rng;
  };

最佳答案

Cygwin 应用程序是多线程的(例如,一个线程是信号监听器线程)。在 gdb 中使用 info threads 找到真正出错的线程。

关于c++ - 如何调试 Cygwin 故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5061821/

相关文章:

c++ - 无法在NS3上调用Tcp套接字收到的回调函数

c++ - 在 C++ 中声明 "interface"并且不将其 vtable 发送到每个翻译单元

c++ - 打开exe时的cygwin1 dll

gdb - cygwin gdb 程序收到信号?,未知信号

gdb - 将内部命令的输出通过管道传输到 shell 命令

正则表达式以使用 egrep 获取分隔内容

c# - 如何开始使用 Cygwin 进行 C# 开发?

c++ - 在 Open GL 2.0 和 glm 中创建第一人称相机

c++ - IHttpRequest 响应长度限制

c++ - gdb 说 "cannot open shared object file"