【问题标题】:gettimeofday() fails with ERRNO 75gettimeofday() 失败并返回 ERRNO 75
【发布时间】:2023-01-08 07:19:42
【问题描述】:

在Android 12手机中执行的程序中的以下代码:

struct timeval tv;
int ret = gettimeofday(&tv, NULL);
if (ret < 0)
    printf("gettimeofday failed with %s\n", strerror(errno));
printf("%lu.%ld\n", tv.tv_sec, tv.tv_usec);

回报

gettimeofday failed with Value too large for defined data type
1078605538.1

此外,seconds 和 useconds 始终保持不变,即使在循环内调用也是如此。 使用 clock_gettime(CLOCK_REALTIME, &amp;tv); 得到完全相同的结果

一定是出了什么问题,因为根据联机帮助页,gettimeofday() 不应该返回 errno 75。

跟踪输出:

read(3, " \0\0\0\1\0\0\0\273\0\0\0\230\1\0\0\1\0\0\0\20\0\254\0\254\0\223\261zG\377F"..., 65536) = 199
clock_gettime(CLOCK_REALTIME, {tv_sec=3416400658881740019, tv_nsec=4632575514938983213}) = 0
dup(2)                                  = 4
fcntl64(4, F_GETFL)                     = 0x20002 (flags O_RDWR|O_LARGEFILE)
statx(4, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_BASIC_STATS, {stx_mask=STATX_BASIC_STATS, stx_attributes=0, stx_mode=S_IFCHR|0600, stx_size=0, ...}) = 0
write(4, "gettimeofday: Value too large fo"..., 52gettimeofday: Value too large for defined data type
) = 52
close(4)
[ and same repeats, we are in a loop ]            

【问题讨论】:

  • 1078605538 回到 2004 年的某个时候,但是大概这只是一堆垃圾。如果您在 gettimeofday 调用之前立即添加 memset(&amp;tv, 0, sizeof tv),是否会改为打印 0.0
  • 我还要仔细检查系统时钟是否实际设置正确;这可能是某种健全性检查,类似于“如果时钟设置为 Android 12 发布之前的日期,则一定有问题”。
  • gettimeofday 内部调用的东西可能返回 -1,但被转换为无符号整数,这可能使其看起来非常大。这是设置errno 的东西。请记住,errno 是全局的。
  • 好的,接下来我要尝试的是在strace 下运行该程序。 (这会产生几百行输出,我们需要看最后的30 行左右。)
  • 这是一些令人费解的 strace 输出:clock_gettime 返回 0 (我假设这是 gettimeofday libc 包装器调用的),但是你检查 errno 而不是 0... 有什么东西坏了.另外,stdout 的 fd 是否以某种方式 4?或者你在别的地方写?这很奇怪。您确定那正是您正在运行的代码吗?例如。如果你只把那几行放在 int main(void) 中并运行它,你会得到相同的结果吗?

标签: android c linux


【解决方案1】:

原来这是某种与 Android 相关的问题。从使用 arm-linux-gnueabi-gcc 的交叉编译切换到使用 ndk-build 后,此问题得到解决。

不完全确定根本原因,但 Makefile 的某些内容已关闭。

【讨论】:

    【解决方案2】:

    我认为这是最近 Android 内核的回归。请注意,这只会影响在 64 位内核上运行的 32 位 ARM 代码。可能您的 NDK 以 AArch64 为目标。

    【讨论】:

      猜你喜欢
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 2012-02-21
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多