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

stop indicator does not work in multi-threaded application #274

Open
dqminh opened this issue Apr 4, 2023 · 5 comments
Open

stop indicator does not work in multi-threaded application #274

dqminh opened this issue Apr 4, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@dqminh
Copy link

dqminh commented Apr 4, 2023

We have a simple reproducer:

///usr/bin/env -S clang++ -O0 "$0" -o /tmp/demo && exec /tmp/demo "$@"
#include <iostream>
#include <fstream>
#include <thread>

using namespace std;


extern "C" {
void __attribute__((noinline)) __attribute__((used)) magic_trace_stop_indicator() { }
}

void task() {
    ifstream r;
    ofstream w;
    r.open("/dev/zero", ios::binary);
    w.open("/dev/null", ios::binary);
    char buf[4096] = {0};
    for (int i = 0; i >= 0; i++)
    {
        r.read(buf, sizeof(buf));
        w.write(buf, sizeof(buf));
        if (i % 100 == 0) {
            magic_trace_stop_indicator();
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc > 1) {
        std::cout << "running in thread" << std::endl;
        std::thread t1(task);
        t1.join();
    } else {
        std::cout << "running in main" << std::endl;
        task();
    }
}

magic_trace_stop_indicator can be triggered reliably in main thread, but not in child thread.

@dqminh dqminh added the bug Something isn't working label Apr 4, 2023
@cgaebel
Copy link
Contributor

cgaebel commented Apr 4, 2023

How are you invoking magic-trace?

@mikea
Copy link

mikea commented Apr 4, 2023

I was able to reproduce it with $ sudo $HOME/bin/magic-trace attach -p 1084651 -multi-thread -trigger .
Stops when running demo without args, doesn't when there's an arg.

$ uname -a
Linux 709L7M3 5.10.0-20-amd64 #1 SMP Debian 5.10.158-2 (2022-12-13) x86_64 GNU/Linux
$ $HOME/bin/magic-trace -version
v1.1.0

@cgaebel
Copy link
Contributor

cgaebel commented Apr 4, 2023

Does it work reliably when you attach to the tid (instead of the parent process) without using the -multi-thread parameter?

@mikea
Copy link

mikea commented Apr 4, 2023

Does it work reliably when you attach to the tid (instead of the parent process) without using the -multi-thread parameter?

yes, attaching to gettid() without -multi-thread seems to stop as expected

@djvolz
Copy link

djvolz commented Apr 5, 2023

I have also encountered the issue @mikea is seeing. I ended up writing a script that wraps magic-trace and uses fzf to offer me the available thread IDs to attach to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants