Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Buffer Support For Logging In xtrace #1406

Closed
wants to merge 0 commits into from

Conversation

CuriousTommy
Copy link
Contributor

@CuriousTommy CuriousTommy commented Jul 20, 2023

Tasks

  • Rewrite code to not rely on std::string & std::vector
  • Figure out why Multi-threaded test case fails

Test Cases

Single Thread
// single_thread_test.c
#include <stdio.h>

int main() {
    printf("Hello world ");
    #if __i386__
        printf("from i386!\n");
    #elif __x86_64__
        printf("from x86_64!\n");
    #endif
}
Multi-threaded
// multithreaded_thread_test.c
#include <stdio.h>
#include <pthread.h>

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
Copy link
Contributor Author

@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
Copy link
Member

@CuriousTommy A fairly simple example would be 176375e (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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants