Skip to content

Commit b0edcd4

Browse files
committed
bpo-44895: Introduce PYTHONDUMPFILE variable for refcount dumping
1 parent e4ed9d2 commit b0edcd4

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

Doc/using/cmdline.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,3 +975,10 @@ Debug-mode variables
975975
shutting down the interpreter.
976976

977977
Need Python configured with the :option:`--with-trace-refs` build option.
978+
979+
.. envvar:: PYTHONDUMPFILE
980+
981+
If set, Python will create a file 'cpython-tracerefs-<timestamp>.dump` as dump file
982+
which is generated by :envvar:`PYTHONDUMPREFS`.
983+
984+
Need Python configured with the :option:`--with-trace-refs` build option.

Include/cpython/initconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ typedef struct PyConfig {
144144
int show_ref_count;
145145
int dump_refs;
146146
int malloc_stats;
147+
int dump_file;
147148
wchar_t *filesystem_encoding;
148149
wchar_t *filesystem_errors;
149150
wchar_t *pycache_prefix;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A debug variable :envvar:`PYTHONDUMPFILE` is added for creating a dump file
2+
which is generated by :envvar:`PYTHONDUMPREFS`. Patch by Dong-hee Na.

Python/initconfig.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ config_check_consistency(const PyConfig *config)
611611
assert(config->show_ref_count >= 0);
612612
assert(config->dump_refs >= 0);
613613
assert(config->malloc_stats >= 0);
614+
assert(config->dump_file >= 0);
614615
assert(config->site_import >= 0);
615616
assert(config->bytes_warning >= 0);
616617
assert(config->warn_default_encoding >= 0);
@@ -898,6 +899,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
898899
COPY_ATTR(no_debug_ranges);
899900
COPY_ATTR(show_ref_count);
900901
COPY_ATTR(dump_refs);
902+
COPY_ATTR(dump_file);
901903
COPY_ATTR(malloc_stats);
902904

903905
COPY_WSTR_ATTR(pycache_prefix);
@@ -1700,6 +1702,9 @@ config_read_env_vars(PyConfig *config)
17001702
if (config_get_env(config, "PYTHONMALLOCSTATS")) {
17011703
config->malloc_stats = 1;
17021704
}
1705+
if (config_get_env(config, "PYTHONDUMPFILE")) {
1706+
config->dump_file = 1;
1707+
}
17031708

17041709
if (config->pythonpath_env == NULL) {
17051710
status = CONFIG_GET_ENV_DUP(config, &config->pythonpath_env,

Python/pylifecycle.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,7 @@ Py_FinalizeEx(void)
17361736
int show_ref_count = tstate->interp->config.show_ref_count;
17371737
#endif
17381738
#ifdef Py_TRACE_REFS
1739+
int dump_file = tstate->interp->config.dump_file;
17391740
int dump_refs = tstate->interp->config.dump_refs;
17401741
#endif
17411742
#ifdef WITH_PYMALLOC
@@ -1835,8 +1836,16 @@ Py_FinalizeEx(void)
18351836
* Alas, a lot of stuff may still be alive now that will be cleaned
18361837
* up later.
18371838
*/
1839+
1840+
FILE *fp = stderr;
1841+
if (dump_file) {
1842+
char dump_file_name[255];
1843+
time_t now = time(NULL);
1844+
sprintf(dump_file_name, "cpython-tracerefs-%ld.dump", now);
1845+
fp = fopen(dump_file_name,"w");
1846+
}
18381847
if (dump_refs) {
1839-
_Py_PrintReferences(stderr);
1848+
_Py_PrintReferences(fp);
18401849
}
18411850
#endif /* Py_TRACE_REFS */
18421851

@@ -1849,7 +1858,10 @@ Py_FinalizeEx(void)
18491858
* above by _Py_PrintReferences.
18501859
*/
18511860
if (dump_refs) {
1852-
_Py_PrintReferenceAddresses(stderr);
1861+
_Py_PrintReferenceAddresses(fp);
1862+
}
1863+
if (fp != NULL && fp != stderr) {
1864+
fclose(fp);
18531865
}
18541866
#endif /* Py_TRACE_REFS */
18551867
#ifdef WITH_PYMALLOC

0 commit comments

Comments
 (0)