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

Add option to retry reading the stack trace of a running application immediately #124

Open
TysonAndre opened this issue Oct 24, 2022 · 2 comments

Comments

@TysonAndre
Copy link
Contributor

See #111 (comment) for context

--- a/phpspy.c
+++ b/phpspy.c
@@ -428,6 +428,9 @@ int main_pid(pid_t pid) {
         if (opt_pause) rv |= pause_pid(pid);
         rv |= do_trace_ptr(&context);
         if (opt_pause) rv |= unpause_pid(pid);
+        struct timeval cur;
+        gettimeofday(&cur, NULL);
+        fprintf(stderr, "%.6f rv=%d\n", cur.tv_sec + 1.e-6 * cur.tv_usec, (int)rv);

Observed: When copy_proc_mem fails (i.e. both rv != 0 and the context's depth < 0 ) for the first function on the stack trace, it sleeps for --sleep-ms seconds
Expected: There should be an option to aggressively retry printing the stack from the first step immediately without sleeping, on failure, so that the phpspy output reflects a running application's stack trace accurately, and has traces from as many milliseconds as possible. (Optionally suppress the repeated warnings until the time elapses or the first success)
Alternative: Print a suggestion to use --pause-process when copy_proc_mem fails if that option is not set, mentioning that it is not safe in production

1666575804.829005 rv=0
copy_proc_mem: Failed to copy zfunc; err=Bad address raddr=0x7f7e00000308 size=156
1666575804.839143 rv=0
1666575804.849331 Printed frames 2 skipped 1
0 Phan\Language\Type::make /path/to/phan/src/Phan/Language/Type.php:427
0 Phan\Language\Type::make /path/to/phan/src/Phan/Language/Type.php:427

1666575804.849365 rv=0
copy_proc_mem: Failed to copy zfunc; err=Bad address raddr=0x7f7e00000007 size=156
1666575804.859566 rv=0
copy_proc_mem: Failed to copy zfunc; err=Bad address raddr=0x307 size=156
1666575804.869782 rv=0
0 file_get_contents <internal>:-1
1 Phan\Library\FileCache::getOrReadEntry /path/to/phan/src/Phan/Library/FileCache.php:91
2 Phan\Analysis::parseFile /path/to/phan/src/Phan/Analysis.php:73
3 Phan\Phan::analyzeFileList /path/to/phan/src/Phan/Phan.php:196
4 <main> /path/to/phan/src/phan.php:1
5 <main> /path/to/phan/phan:1

Workarounds

https://github.com/adsr/phpspy#demos doesn't mention --pause-process in the instructions for profiling, but the help text in the README does.

  -S, --pause-process                Pause process while reading stacktrace
                                       (unsafe for production!)
@adsr
Copy link
Owner

adsr commented Oct 24, 2022

How about something like --immediate-retry=<n> where n is the number of times to retry or -1 to retry indefinitely? -1 could be unsafe as it could peg CPU if memory cannot be read for some reason. Of course we should never retry if the pid is detected to be dead.

@TysonAndre
Copy link
Contributor Author

That sounds like a good solution, a finite limit is probably for the best and users can set 9999... if they need something effectively immediate

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

No branches or pull requests

2 participants