sysprog21/lab0-c

lldb hang after building lab0-c using clang for apple-silicon

Closed this issue · 2 comments

I build this lab0-c using clang for apple-silicon by modifying dudect/cpucycles.h

static inline int64_t cpucycles(void)
{
#if defined(__i386__) || defined(__x86_64__)
    unsigned int hi, lo;
    __asm__ volatile("rdtsc\n\t" : "=a"(lo), "=d"(hi));
    return ((int64_t) lo) | (((int64_t) hi) << 32);
#elif defined(__aarch64__)
    uint64_t val;

    /* According to ARM DDI 0487F.c, from Armv8.0 to Armv8.5 inclusive, the
     * system counter is at least 56 bits wide; from Armv8.6, the counter
     * must be 64 bits wide.  So the system counter could be less than 64
     * bits wide and it is attributed with the flag 'cap_user_time_short'
     * is true.
     */
    asm volatile("mrs %0, cntvct_el0" : "=r" (val));

    return val;

#else
    uint64_t Res;
    asm volatile("mrs %0, cntvct_el0" : "=r"(Res));
    return Res;
#endif
}

But when I tried to ran lldb for qtest, it got hang on line 890 of linenoise.c after enter return. I backtrack the log and found it might have crashed or infitite looping in libsystem_kernel.read function. However, I couldn't find any known issue related to lldb causing libsystem_kernel read hang.

My lldb version is below:

/usr/bin/lldb: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]
/usr/bin/lldb (for architecture x86_64):        Mach-O 64-bit executable x86_64
/usr/bin/lldb (for architecture arm64e):        Mach-O 64-bit executable arm64e

Below are the log when I ran lldb and backtrack the issue.

lldb qtest                                      
(lldb) target create "qtest"
Current executable set to '/Users/shouenhsiao/sideProject/CPP/lab0-c/qtest' (arm64).
(lldb) b linenoise
Breakpoint 1: where = qtest`linenoise + 56 at linenoise.c:1152:10, address = 0x00000001000052f8
(lldb) r
Process 62297 launched: '/Users/shouenhsiao/sideProject/CPP/lab0-c/qtest' (arm64)
qtest was compiled with optimization - stepping may behave oddly; variables may not be available.
Process 62297 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001000052f8 qtest`linenoise(prompt="cmd> ") at linenoise.c:1152:10 [opt]
   1149     char buf[LINENOISE_MAX_LINE];
   1150     int count;
   1151
-> 1152     if (!isatty(STDIN_FILENO)) {
   1153         /* Not a tty: read from file / pipe. In this mode we don't want any
   1154          * limit to the line size, so we call a function to handle that. */
   1155         return linenoiseNoTTY();
Target 0: (qtest) stopped.
(lldb) c
Process 62297 resuming
cmd> new
Process 62297 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x00000001ac6e6538 libsystem_kernel.dylib`read + 8
libsystem_kernel.dylib`read:
->  0x1ac6e6538 <+8>:  b.lo   0x1ac6e6558               ; <+40>
    0x1ac6e653c <+12>: pacibsp
    0x1ac6e6540 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x1ac6e6544 <+20>: mov    x29, sp
Target 0: (qtest) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  * frame #0: 0x00000001ac6e6538 libsystem_kernel.dylib`read + 8
    frame #1: 0x0000000100005f7c qtest`linenoiseEdit(stdin_fd=0, stdout_fd=1, buf="new\n", buflen=4096, prompt=<unavailable>) at linenoise.c:890:17 [opt]
    frame #2: 0x0000000100005558 qtest`linenoiseRaw(buf="new\n", buflen=4096, prompt="cmd> ") at linenoise.c:1097:13 [opt]
    frame #3: 0x0000000100005394 qtest`linenoise(prompt="cmd> ") at linenoise.c:1170:17 [opt]
    frame #4: 0x000000010000354c qtest`run_console(infile_name=0x0000000000000000) at console.c:659:27 [opt]
    frame #5: 0x0000000100000b14 qtest`main(argc=1, argv=0x000000016fdff298) at qtest.c:788:16 [opt]
    frame #6: 0x000000010001d0f4 dyld`start + 520
(lldb) f 1
frame #1: 0x0000000100005f7c qtest`linenoiseEdit(stdin_fd=0, stdout_fd=1, buf="new\n", buflen=4096, prompt=<unavailable>) at linenoise.c:890:17 [opt]
   887          int nread;
   888          char seq[3];
   889
-> 890          nread = read(l.ifd, &c, 1);
   891          if (nread <= 0)
   892              return l.len;
   893
(lldb) f 0
frame #0: 0x00000001ac6e6538 libsystem_kernel.dylib`read + 8
libsystem_kernel.dylib`read:
->  0x1ac6e6538 <+8>:  b.lo   0x1ac6e6558               ; <+40>
    0x1ac6e653c <+12>: pacibsp
    0x1ac6e6540 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x1ac6e6544 <+20>: mov    x29, sp
(lldb)

Thank you for any assitance in advance.
(I would delete this issue if this isn't a right place to file a issue. )

jserv commented

I tend to NOT support macOS because this project was meant to be the prerequisite of Linux system programming. However, if you have an idea to overcome the above, you can still send pull request(s).

jserv commented

@Shawn5141, I made some Arm64 specific changes. You can try the latest master branch.