本机 localtime() 段错误 [英] Native localtime() segfaults

查看:43
本文介绍了本机 localtime() 段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试公开 Perl 6 中的 localtime 功能时,我似乎做错了:

I seem to be doing something wrong in this attempt to expose the localtime functionality in Perl 6:

use NativeCall;
my class TimeStruct is repr<CStruct> {
    has int32 $!tm_sec;
    has int32 $!tm_min;
    has int32 $!tm_hour;
    has int32 $!tm_mday;
    has int32 $!tm_mon;
    has int32 $!tm_year;
    has int32 $!tm_wday;
    has int32 $!tm_yday;
    has int32 $!tm_isdst;
    has Str   $!tm_zone;
    has long  $!tm_gmtoff;
}

sub localtime(uint32 $epoch --> TimeStruct) is native {*}
dd localtime(time);  # segfault

perl6-lldb-m 下运行,我得到:

Running under perl6-lldb-m, I get:

Process 82758 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x5ae5dda1)
    frame #0: 0x00007fffe852efb4 libsystem_c.dylib`_st_localsub + 13
libsystem_c.dylib`_st_localsub:
->  0x7fffe852efb4 <+13>: movq   (%rdi), %rax
    0x7fffe852efb7 <+16>: movq   %rax, -0x20(%rbp)
    0x7fffe852efbb <+20>: movq   0x8e71d3e(%rip), %rbx     ; lclptr
    0x7fffe852efc2 <+27>: testq  %rbx, %rbx
Target 0: (moar) stopped.

我在这里做错了什么?

更新:最终的工作解决方案:

UPDATE: the final working solution:

class TimeStruct is repr<CStruct> {
    has int32 $.tm_sec;    # *must* be public attributes
    has int32 $.tm_min;
    has int32 $.tm_hour;
    has int32 $.tm_mday;
    has int32 $.tm_mon;
    has int32 $.tm_year;
    has int32 $.tm_wday;
    has int32 $.tm_yday;
    has int32 $.tm_isdst;
    has long  $.tm_gmtoff; # these two were
    has Str   $.time_zone; # in the wrong order
}

sub localtime(int64 $epoch is rw --> TimeStruct) is native {*}

my int64 $epoch = time;  # needs a separate definition somehow
dd localtime($epoch);

推荐答案

localtime() 需要 time_t* 类型的指针作为参数.假设 time_tuint32_t 是您特定平台上的兼容类型,

localtime() expects a pointer of type time_t* as argument. Assuming time_t and uint32_t are compatible types on your particular platform,

sub localtime(uint32 $epoch is rw --> TimeStruct) is native {*}
my uint32 $t = time;
dd localtime($t);

应该这样做(尽管除非您公开属性,否则您将看不到任何内容).

should do it (though you won't get to see anything unless you make your attributes public).

我有点惊讶您的 time_t 不是 64 位类型,而且刚刚在 google 上搜索了 apple time.h,我也怀疑最后两个结构声明中的属性顺序错误...

I'm a bit surprised that your time_t isn't a 64-bit type, and having just googled apple time.h, I also suspect the last two attributes in your struct declaration are in the wrong order...

这篇关于本机 localtime() 段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆