记录一个原因不明的段错误(libxml2 proc activemq的三角恋)

时间:2021-07-05 20:11:50

又碰到了令人头疼的段错误,但是这次,虽然最终找到了出自何处,但是还是不明白究竟是为何。

最后将程序简化,贴上来,希望高人能指点一下。

memt.pc:

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <sqlca.h>

int main(int argc,char** argv)
{
    EXEC SQL BEGIN DECLARE SECTION;
       char uid1[50] = "TOTO/TOTO@10.44.111.19:1521/orcl";
    EXEC SQL END DECLARE SECTION;

EXEC SQL CONNECT :uid1;
    xmlReadFile("/home/toto/etc/transdb.xml","GBK",XML_PARSE_RECOVER);
    printf("line[%d]\n",__LINE__);

return 0;
}

Makefile:

CC=gcc

PROC=proc

DBINCLUDE=-I$(ORACLE_HOME)/precomp/public

DBFLAGS= -L$(ORACLE_HOME)/precomp/lib -lclntsh -pthread

memt: memt.pc
    $(PROC) iname=memt.pc parse=no lines=yes
    $(CC) $(DBINCLUDE) -I/usr/local/include/libxml2 -o memt memt.c -L/usr/local/ActiveMQ-C/lib -lactivemq-c -L/usr/local/lib -lxml2 $(DBFLAGS) -w
#   $(CC) $(DBINCLUDE) -I/usr/local/include/libxml2 -o memt memt.c -L/usr/local/ActiveMQ-C/lib -L/usr/local/lib -lxml2 $(DBFLAGS) -w
    rm $@.c $@.lis
    mv memt $(HOME)/bin/memt
    make clean

编译没有问题,运行之后出现段错误。

1.如果把程序中的"EXEC SQL CONNECT :uid1;"这句注掉,编译运行通过;

2.如果把程序中的"xmlReadFile("/home/toto/etc/transdb.xml","GBK",XML_PARSE_RECOVER);"这句注掉,编译运行通过;

3.如果把Makefile中的"-lactivemq-c"去掉,程序也可以编译运行通过.

使用gdb调试:

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-suse-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/toto/bin/memt...done.
(gdb) r
Starting program: /home/toto/bin/memt
Missing separate debuginfo for /lib/ld-linux.so.2
Try: zypper install -C "debuginfo(build-id)=77c46e45f6efdf89044415840ec59f7b3c2fd041"
Missing separate debuginfo for /usr/lib/libxml2.so.2
Try: zypper install -C "debuginfo(build-id)=48ad46afc2a5a5794ff474fa66eb3ca4d8354cef"
Missing separate debuginfo for /lib/libpthread.so.0
Try: zypper install -C "debuginfo(build-id)=3043b78c80daa60cdb3d347dcb33f00bd1551163"
[Thread debugging using libthread_db enabled]
Missing separate debuginfo for /lib/libc.so.6
Try: zypper install -C "debuginfo(build-id)=947f93f3895469254f408b3eff732d0f12135a60"
Missing separate debuginfo for /lib/librt.so.1
Try: zypper install -C "debuginfo(build-id)=88e2c2308d90ca748f6c16545dca818ca8f5a50f"
Missing separate debuginfo for /lib/libcrypt.so.1
Try: zypper install -C "debuginfo(build-id)=4fdfcab55467c7d1c6b52986053631a854d508bf"
Missing separate debuginfo for /lib/libdl.so.2
Try: zypper install -C "debuginfo(build-id)=cd785200787e37f6917cc1c87966fb4404e65297"
Missing separate debuginfo for /usr/lib/libstdc++.so.6
Try: zypper install -C "debuginfo(build-id)=a06c7686acca71405f69e506919c07c1db9ea518"
Missing separate debuginfo for /lib/libm.so.6
Try: zypper install -C "debuginfo(build-id)=293aa876def3cb4177e0389f96c11e76b839b7e4"
Missing separate debuginfo for /lib/libgcc_s.so.1
Try: zypper install -C "debuginfo(build-id)=815f111c78054fb492b3e12dd7bef2d7efc808b7"
Missing separate debuginfo for /lib/libz.so.1
Try: zypper install -C "debuginfo(build-id)=9f5e4b386d9826b14a48677c23dcdf8a2cb45bff"
Missing separate debuginfo for /lib/libnsl.so.1
Try: zypper install -C "debuginfo(build-id)=39faa9c7203fd34021eba28be4090f6f58d83c4c"

Program received signal SIGSEGV, Segmentation fault.
0xb6fac88d in strncmp () from /lib/libc.so.6

(gdb) where
#0  0xb6fac88d in strncmp () from /lib/libc.so.6
#1  0xb7ece0ad in __xmlParserInputBufferCreateFilename () from /usr/lib/libxml2.so.2
#2  0xb7ece12a in xmlParserInputBufferCreateFilename () from /usr/lib/libxml2.so.2
#3  0xb7ea2001 in xmlNewInputFromFile () from /usr/lib/libxml2.so.2
#4  0xb7ece543 in xmlLoadExternalEntity () from /usr/lib/libxml2.so.2
#5  0xb7ea6661 in xmlCreateURLParserCtxt () from /usr/lib/libxml2.so.2
#6  0xb7ebcda1 in xmlReadFile () from /usr/lib/libxml2.so.2
#7  0x080487f0 in main (argc=<value optimized out>, argv=<value optimized out>) at memt.pc:12

从GDB的调试信息中可以看到,是xmlReadFile函数内部嵌套调用的strncmp函数导致了段错误,但是不明白为什么在没有嵌入SQL语句或者编译的时候不连接activemq的动态库就没有产生段错误。

在这个程序中,"-lactivemq-c”是在一个Makefile的变量里的,为了多个程序的编译共用,所以没有去掉,导致了段错误。这是一个教训,没有必要的时候,不要随意引用第三方库,还是根据实际需要最靠谱啊。

但是这里面究竟是有什么冲突呢?proc,libxml2,activemq三者的关系真是有点暧昧啊。。。