Skip to content

Commit

Permalink
bpo-44895: Introduce PYTHONDUMPFILE variable for refcount dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Aug 14, 2021
1 parent e4ed9d2 commit b0edcd4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Doc/using/cmdline.rst
Expand Up @@ -975,3 +975,10 @@ Debug-mode variables
shutting down the interpreter.

Need Python configured with the :option:`--with-trace-refs` build option.

.. envvar:: PYTHONDUMPFILE

If set, Python will create a file 'cpython-tracerefs-<timestamp>.dump` as dump file
which is generated by :envvar:`PYTHONDUMPREFS`.

Need Python configured with the :option:`--with-trace-refs` build option.
1 change: 1 addition & 0 deletions Include/cpython/initconfig.h
Expand Up @@ -144,6 +144,7 @@ typedef struct PyConfig {
int show_ref_count;
int dump_refs;
int malloc_stats;
int dump_file;
wchar_t *filesystem_encoding;
wchar_t *filesystem_errors;
wchar_t *pycache_prefix;
Expand Down
@@ -0,0 +1,2 @@
A debug variable :envvar:`PYTHONDUMPFILE` is added for creating a dump file
which is generated by :envvar:`PYTHONDUMPREFS`. Patch by Dong-hee Na.
5 changes: 5 additions & 0 deletions Python/initconfig.c
Expand Up @@ -611,6 +611,7 @@ config_check_consistency(const PyConfig *config)
assert(config->show_ref_count >= 0);
assert(config->dump_refs >= 0);
assert(config->malloc_stats >= 0);
assert(config->dump_file >= 0);
assert(config->site_import >= 0);
assert(config->bytes_warning >= 0);
assert(config->warn_default_encoding >= 0);
Expand Down Expand Up @@ -898,6 +899,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_ATTR(no_debug_ranges);
COPY_ATTR(show_ref_count);
COPY_ATTR(dump_refs);
COPY_ATTR(dump_file);
COPY_ATTR(malloc_stats);

COPY_WSTR_ATTR(pycache_prefix);
Expand Down Expand Up @@ -1700,6 +1702,9 @@ config_read_env_vars(PyConfig *config)
if (config_get_env(config, "PYTHONMALLOCSTATS")) {
config->malloc_stats = 1;
}
if (config_get_env(config, "PYTHONDUMPFILE")) {
config->dump_file = 1;
}

if (config->pythonpath_env == NULL) {
status = CONFIG_GET_ENV_DUP(config, &config->pythonpath_env,
Expand Down
16 changes: 14 additions & 2 deletions Python/pylifecycle.c
Expand Up @@ -1736,6 +1736,7 @@ Py_FinalizeEx(void)
int show_ref_count = tstate->interp->config.show_ref_count;
#endif
#ifdef Py_TRACE_REFS
int dump_file = tstate->interp->config.dump_file;
int dump_refs = tstate->interp->config.dump_refs;
#endif
#ifdef WITH_PYMALLOC
Expand Down Expand Up @@ -1835,8 +1836,16 @@ Py_FinalizeEx(void)
* Alas, a lot of stuff may still be alive now that will be cleaned
* up later.
*/

FILE *fp = stderr;
if (dump_file) {
char dump_file_name[255];
time_t now = time(NULL);
sprintf(dump_file_name, "cpython-tracerefs-%ld.dump", now);
fp = fopen(dump_file_name,"w");
}
if (dump_refs) {
_Py_PrintReferences(stderr);
_Py_PrintReferences(fp);
}
#endif /* Py_TRACE_REFS */

Expand All @@ -1849,7 +1858,10 @@ Py_FinalizeEx(void)
* above by _Py_PrintReferences.
*/
if (dump_refs) {
_Py_PrintReferenceAddresses(stderr);
_Py_PrintReferenceAddresses(fp);
}
if (fp != NULL && fp != stderr) {
fclose(fp);
}
#endif /* Py_TRACE_REFS */
#ifdef WITH_PYMALLOC
Expand Down

0 comments on commit b0edcd4

Please sign in to comment.