代码之家  ›  专栏  ›  技术社区  ›  Elizabeth Mattijsen

本机localtime()segfaults

  •  9
  • Elizabeth Mattijsen  · 技术社区  · 8 年前

    我在试图揭露 localtime 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 ,我得到:

    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.
    

    有什么明显的事情我做错了吗?

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

    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);
    
    1 回复  |  直到 7 年前
        1
  •  8
  •   Christoph    8 年前

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

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

    应该这样做(尽管除非你公开你的属性,否则你不会看到任何东西)。

    我有点惊讶你的 时间\u t 不是64位类型,只是在google上搜索 apple time.h ,我还怀疑struct声明中的最后两个属性的顺序错误。。。