gcc 7.1.0下gdb无法prinf查看errno解决
centos7.4 下, gcc 7.1.0, gdb8.1下编译postgresql-10.10源码
编译参数:
./configure --enable-debug --prefix=/home/postgres/pginstall CFLAGS="-DGCC_HASCLASSVISIBILITY -O0 -Wall -W -g3 -gdwarf-2"
// miscinit.c -> CreateDataDirLockFile()
fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0)
break; /* Success; exit the retry loop */
/*
* Couldn't create the pid file. Probably it already exists.
*/
if ((errno != EEXIST && errno != EACCES) || ntries > 100)
ereport(FATAL,
(errcode_for_file_access(),
errmsg("could not create lock file \"%s\": %m",
filename)));
gdb调试上面代码时, 想打印errno的值, 结果报错如下:
(gdb) p errno
'__errno_location' has unknown return type; cast the call to its declared return type
解决如下:
(gdb) p *((int*(*)())__errno_location)()
$13 = 17
在网上查了一下, 理论上"p errno" 就可以查看errno的值, 在gcc7.1.0 和 gdb8.1下,
可能存在Bug或者时某些功能缺失造成的.
解决方法就是:
p *((int*(*)())__errno_location)()
或
prinf *((int*(*)())__errno_location)()
参考
https://stackoverflow.com/questions/11495720/why-cant-errnos-value-be-printed