英文:
Strange issue desired function not getting called in C
问题
在我们的传统产品代码中,有一个函数定义如下:
int
mkdirhier(char * path, mode_t mode, uid_t uid, gid_t gid)
{
printf("TEMP_DEBUG: Inside function mkdirhier");
// <源代码>
}
它被调用如下:
mkdirhier( path, 0755, AdminUid, AdminGid );
但实际情况是,虽然调用了mkdirhier,但不是我们自己的那个,即我们没有得到printf消息“TEMP_DEBUG: Inside function mkdirhier”。
另一方面,如果我将函数名从mkdirhier
更改为mkdirhier_custom
,并且从调用者代码中也将名称更改为mkdirhier_custom
,那么我就能够得到我的printf消息。
我们怀疑mkdirhier
是从系统库中调用的,我使用以下命令在所有第三方库的.so、.a、.o中进行了搜索:
find . -name "*.a" | xargs nm -C | grep "mkdirhier"
类似的对.so、.o进行搜索
但没有发现这样的情况。
我们使用了第三方库libcurl
、libevent
、openssl
,但没有mkdirhier
的出现。
我甚至尝试使用gdb
运行我们的应用程序,并在mkdirhier
处设置断点。
gdb
提到断点也应用于mkdirhier
,还提到了我们有定义的文件名。但在运行时通过gdb执行时,它不会停在mkdirhier
并继续执行。
另一方面,在mkdirhier_custom
处设置断点时,在运行时它会停在mkdirhier_custom
。
似乎对于mkdirhier
,系统调用优先级更高,我们不确定它在哪里定义,甚至在应用断点时也不会停止,我无法获得回溯信息,因为它不会停在mkdirhier
。
由于这是传统代码,我们没有权限(不允许)更改自定义函数名称从mkdirhier
到mkdirhier_custom
,请告诉我如何继续前进,因为我没有任何线索。
英文:
In our legacy product code, there is function defined as:
int
mkdirhier(char * path, mode_t mode, uid_t uid, gid_t gid)
{
printf("TEMP_DEBUG: Inside function mkdirhier");
<source code>
}
Its called as:
mkdirhier( path, 0755, AdminUid, AdminGid );
But when we actually see is that, mkdirhier is getting called but not the one which is ours i.e. we are not getting printf message TEMP_DEBUG: Inside function mkdirhier
On the other hand if I change function name: mkdirhier
to mkdirhier_custom
and from caller code also change name to mkdirhier_custom
I get my printfs.
We are suspecting mkdirhier
is getting called from any system library,I searched in all the third party libraries .so, .a, .o using command:
find . -name "*.a" | xargs nm -C | grep "mkdirhier"
similarly for .so , .o
But no such occurrences.
We are using third party libraries libcurl
,libevent
,openssl
but no occurrences of mkdirhier
.
Even I tried to run our application with gdb
and added breakpoint at mkdirhier
.
gdb
mentions that breakpoint is applied to mkdirhier
also mentions the file name in which we have definition. But during runtime execution via gdb it doesn't stop at mkdirhier
and continues.
While on the other hand breakpoint at mkdirhier_custom
when applied , during runtime it stops at mkdirhier_custom
.
Seems with mkdirhier
it gives priority to system call and we are not sure where its defined and even applying breakpoint doesnt' stops and I can not get backtrace as it does not stops in mkdirhier
.
Also as its legacy code and we don't have privilege(not allowed) to final check-in for changing custom function name from mkdirhier
to mkdirhier_custom
Please let me know how to proceed ahead as I am not getting any clue.
答案1
得分: 0
最有可能的情况是:
printf("TEMP_DEBUG: Inside function mkdirhier");
被调用,但你看不到输出,因为它被stdio包缓冲了。当输出到终端时,stdio库使用行缓冲描述符,这意味着只有在输出换行字符\n
时才会刷新输出。只需在末尾加上\n
,你就能知道函数是否被调用了。如果输出被打印到文件,情况会更糟,因为只有当缓冲区完全填满时(在实际系统上大约是16KB),数据才会被写入。
另一种选择是在printf()
调用之后立即执行fflush(stdout);
。这将强制刷新缓冲区。你还可以使用fprintf(stderr, ...);
,这在缓冲输出方面与stdout
有些小差异。
无论你使用哪种方法,我强烈建议你在每个日志行的末尾加上\n
,否则你的输出会有些混乱
英文:
The most probable thing is that:
printf("TEMP_DEBUG: Inside function mkdirhier");
is called, but you don't see the output, because it is being buffered by the stdio package. When output to a terminal, the stdio library uses line buffered descriptors, which means that output is flushed only when a \n
new line character is output. Just put a \n
at the end, and you'll then know actually if the function is being called or not. If the output is printed to a file, the situation is worse, as only when a buffer is filled completely (this is around 16kb on actual systems) the data is written.
Another alternative is to make a fflush(stdout);
just after the printf()
call. This will enforce the buffer flush. You can also fprintf(stderr, ...);
that has small differences about buffering output against stdout
.
Whatever method you use, I strongly recommend you to put a \n
at the end of every log line, or your output will be a bit meshed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论