darlinghq / darling

Darwin/macOS emulation layer for Linux
http://www.darlinghq.org
GNU General Public License v3.0
11.55k stars 446 forks source link

Implement Buffer Support For Logging In xtrace #1406

Closed CuriousTommy closed 1 year ago

CuriousTommy commented 1 year ago

Tasks

Test Cases

Single Thread ```c // single_thread_test.c #include int main() { printf("Hello world "); #if __i386__ printf("from i386!\n"); #elif __x86_64__ printf("from x86_64!\n"); #endif } ```
Multi-threaded ```c // multithreaded_thread_test.c #include #include void* hello_thread(void* args) { int* value = (int*)args; printf("Hello from thread %d!\n", *value); return NULL; } int main() { printf("Hello world "); #if __i386__ printf("from i386!\n"); #elif __x86_64__ printf("from x86_64!\n"); #endif printf("Attempting to create threads\n"); pthread_t threads[4]; int temp[4]; for (int i=0; i<4; i++) { temp[i]=i+1; if (pthread_create(&threads[i],NULL,hello_thread,&temp[i])) { printf("Unable to create thread\n"); return -1; } } printf("Joining threads\n"); for (int i=0; i<4; i++) { pthread_join(threads[i],NULL); } return 0; } ```
CuriousTommy commented 1 year ago

@facekapow

Now that we have access to basically all of the host libc (since it's loaded implicitly by mldr), we could just add elfcalls for the host's malloc and free and use those instead.

Is there an example I can look at on how I should do this? Am I suppose to use wrap_elf and dlopen?

facekapow commented 1 year ago

@CuriousTommy A fairly simple example would be 176375e9345c4499c0970b496d956516fd7bed0b (minus the changes to src/kernel/emulation/linux/misc/proc_info.c, of course). No, you don't need wrap_elf or dlopen. You basically just need to add entries for malloc and free to the struct elf_calls structure (in src/startup/mldr/elfcalls/elfcalls.h) and initialize those members in elfcalls_make (in src/startup/mldr/elfcalls/elfcalls.c).

Since this is just for xtrace to use them, you don't even need to add the corresponding wrappers in libsystem_kernel; just include <elfcalls.h> and add an extern struct elf_calls* _elfcalls declaration.