调试嵌入式linux应用程序时,出现下述错误:
terminate called after throwing an instance of ‘std::length_error’
what(): basic_string::_S_create
采用在gdb中运行,错误如下:
[19-03-05 22:04:22.818][DBG][CHA_MAIN][powerIfBase.cpp,L0092] CIF-A send 0x18226401 2 00 00
[19-03-05 22:04:22.986][DBG][CHA_MAIN][powerIfBase.cpp,L0135] CIF-A recv 0x18210164 0
[19-03-05 22:04:22.986][DBG][CHA_MAIN][powerIfXpeng.cp,L0521] receive data of yx —————
[19-03-05 22:04:23.183][DBG][CHA_MAIN][ccoreIfXpeng.cp,L0864] frame length [4] less than min [8]
[19-03-05 22:04:23.183][DBG][CHA_MAIN][ccoreIfXpeng.cp,L0871] frame data length [22] mismatch frame length [29]
[19-03-05 22:04:23.183][DBG][CHA_MAIN][ccoreIfXpeng.cp,L0914] << receive frame (30 bytes): 68 00 01 04 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FFFFFFA0 0F FFFFFFAF 16
[19-03-05 22:04:23.183][DBG][CHA_MAIN][ccoreIfXpeng.cp,L0718] receive ccore-yc data (len [22])
[New Thread 14748.0x5e78]
[19-03-05 22:04:23.317][DBG][CHA_MAIN][powerIfBase.cpp,L0092] CIF-A send 0x18226401 2 00 00
[New Thread 14748.0x2fbc]
[19-03-05 22:04:23.470][DBG][CHA_MAIN][ccoreIfXpeng.cp,L0999] send yx to ccore [ok]
[19-03-05 22:04:23.470][DBG][CHA_MAIN][ccoreIfXpeng.cp,L1051] send yc to ccore [ok]
[New Thread 14748.0×6160]
[19-03-05 22:04:23.474][DBG][CHA_MAIN][powerIfBase.cpp,L0092] CIF-A send 0x18416401 8 15 00 00 00 00 00 00 00
terminate called after throwing an instance of ‘std::length_error’
what(): basic_string::_S_create
Program received signal SIGABRT, Aborted.
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
0x00000000 in ?? ()
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
大体上应该是操作basic_string时发生了错误,根据错误描述,是basic_string抛出了个异常。
在basic_string.h头文件中搜索“std::length_error”,如下:
std::length_error If @a res_arg exceeds @c max_size(). std::length_error If new length exceeds @c max_size().
使用gdb查看core文件堆栈信息,如下:
没发现有啥有用信息。
现在有如茫茫大海,无从查起的感觉。无耐之下,在偶然间重新运行,发现打印日志有异常,如下图红色所示:
这条日志输出是有异常的,本来是打印报文,都是2位十六进制,这里却出现了4个字节的报文打印,而且发送数据长度显示为32 bytes。找到代码如下,发现有异常,如红色框框所示。
增加数据类型强制转换即可解决问题:
const int n = sprintf(buf, "%02X ", (__u8)frame[i]);
参考文章: