-
-
Notifications
You must be signed in to change notification settings - Fork 419
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
make subprocess child shows different process name #391
Merged
gaogaotiantian
merged 15 commits into
gaogaotiantian:master
from
TTianshun:change_subprocess_name
Feb 1, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c8ab52e
make subprocess child shows different process name
TTianshun 07d92c0
use sys.argv[0] as subprocess name
TTianshun 38c75b6
change process_name in c module
TTianshun 6a5037e
fix coverage
TTianshun 4b7d72d
fix lint
TTianshun fb827cb
fix a bug
TTianshun 502208a
1. update code format 2. not use cmdline to change process_name
TTianshun 0687708
1. remove command line arg process_name 2. save process name as PyObject
TTianshun 4b476bd
fix subprocess miss
TTianshun 71af9c0
fix refcnt error and add unicode check
TTianshun aabb8b3
fix child process_name and add tests
TTianshun 76b26d6
change process_name in run_code
TTianshun aa598d6
change process_name of code to python -c
TTianshun 3dc0f72
temporarily fix coverage problem
TTianshun a7bc758
update test
TTianshun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -650,20 +650,27 @@ snaptrace_load(TracerObject* self, PyObject* args) | |
PyObject* dict = PyDict_New(); | ||
PyObject* args = PyDict_New(); | ||
PyObject* process_name_string = PyUnicode_FromString("process_name"); | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* process_name = PyObject_GetAttrString(current_process, "name"); | ||
PyObject* process_name = NULL; | ||
|
||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
if (self->process_name) { | ||
process_name = self->process_name; | ||
Py_INCREF(process_name); | ||
} else { | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
process_name = PyObject_GetAttrString(current_process, "name"); | ||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
} | ||
|
||
PyDict_SetItemString(dict, "ph", ph_M); | ||
PyDict_SetItemString(dict, "pid", pid); | ||
PyDict_SetItemString(dict, "tid", pid); | ||
|
@@ -898,22 +905,30 @@ snaptrace_dump(TracerObject* self, PyObject* args) | |
// == Load the metadata first == | ||
// Process Name | ||
{ | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
PyObject* process_name = NULL; | ||
if (self->process_name) { | ||
process_name = self->process_name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are losing reference here. You need to increment the reference because you are decreasing it later. |
||
Py_INCREF(process_name); | ||
} else { | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
process_name = PyObject_GetAttrString(current_process, "name"); | ||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
} | ||
PyObject* process_name = PyObject_GetAttrString(current_process, "name"); | ||
|
||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
fprintf(fptr, "{\"ph\":\"M\",\"pid\":%lu,\"tid\":%lu,\"name\":\"process_name\",\"args\":{\"name\":\"%s\"}},", | ||
pid, pid, PyUnicode_AsUTF8(process_name)); | ||
fprintf(fptr, "{\"ph\":\"M\",\"pid\":%lu,\"tid\":%lu,\"name\":\"process_name\",\"args\":{\"name\":\"", | ||
pid, pid); | ||
fprint_escape(fptr, PyUnicode_AsUTF8(process_name)); | ||
fprintf(fptr, "\"}},"); | ||
Py_DECREF(process_name); | ||
} | ||
|
||
|
@@ -1140,11 +1155,12 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
static char* kwlist[] = {"verbose", "lib_file_path", "max_stack_depth", | ||
"include_files", "exclude_files", "ignore_c_function", "ignore_frozen", | ||
"log_func_retval", "log_func_args", "log_async", "trace_self", | ||
"min_duration", | ||
"min_duration", "process_name", | ||
NULL}; | ||
int kw_verbose = -1; | ||
int kw_max_stack_depth = 0; | ||
char* kw_lib_file_path = NULL; | ||
PyObject* kw_process_name = NULL; | ||
PyObject* kw_include_files = NULL; | ||
PyObject* kw_exclude_files = NULL; | ||
int kw_ignore_c_function = -1; | ||
|
@@ -1154,7 +1170,7 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
int kw_log_async = -1; | ||
int kw_trace_self = -1; | ||
double kw_min_duration = 0; | ||
if (!PyArg_ParseTupleAndKeywords(args, kw, "|isiOOppppppd", kwlist, | ||
if (!PyArg_ParseTupleAndKeywords(args, kw, "|isiOOppppppdO", kwlist, | ||
&kw_verbose, | ||
&kw_lib_file_path, | ||
&kw_max_stack_depth, | ||
|
@@ -1166,7 +1182,8 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
&kw_log_func_args, | ||
&kw_log_async, | ||
&kw_trace_self, | ||
&kw_min_duration)) { | ||
&kw_min_duration, | ||
&kw_process_name)) { | ||
return NULL; | ||
} | ||
|
||
|
@@ -1190,6 +1207,15 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
strcpy(self->lib_file_path, kw_lib_file_path); | ||
} | ||
|
||
if (kw_process_name && !Py_IsNone(kw_process_name)) { | ||
if (!PyUnicode_CheckExact(kw_process_name)) { | ||
PyErr_SetString(PyExc_TypeError, "process_name must be a string"); | ||
return NULL; | ||
} | ||
Py_INCREF(kw_process_name); | ||
Py_XSETREF(self->process_name, kw_process_name); | ||
} | ||
|
||
if (kw_ignore_c_function == 1) { | ||
SET_FLAG(self->check_flags, SNAPTRACE_IGNORE_C_FUNCTION); | ||
} else if (kw_ignore_c_function == 0) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New reference here as well