| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include "hashtable.h"
 | 
					
						
							|  |  |  | #include "frameobject.h"
 | 
					
						
							|  |  |  | #include "pythread.h"
 | 
					
						
							|  |  |  | #include "osdefs.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Trace memory blocks allocated by PyMem_RawMalloc() */ | 
					
						
							|  |  |  | #define TRACE_RAW_MALLOC
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Forward declaration */ | 
					
						
							|  |  |  | static void tracemalloc_stop(void); | 
					
						
							|  |  |  | static void* raw_malloc(size_t size); | 
					
						
							|  |  |  | static void raw_free(void *ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_DEBUG
 | 
					
						
							|  |  |  | #  define TRACE_DEBUG
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Protected by the GIL */ | 
					
						
							|  |  |  | static struct { | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |     PyMemAllocatorEx mem; | 
					
						
							|  |  |  |     PyMemAllocatorEx raw; | 
					
						
							|  |  |  |     PyMemAllocatorEx obj; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } allocators; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct { | 
					
						
							|  |  |  |     /* Module initialized?
 | 
					
						
							|  |  |  |        Variable protected by the GIL */ | 
					
						
							|  |  |  |     enum { | 
					
						
							|  |  |  |         TRACEMALLOC_NOT_INITIALIZED, | 
					
						
							|  |  |  |         TRACEMALLOC_INITIALIZED, | 
					
						
							|  |  |  |         TRACEMALLOC_FINALIZED | 
					
						
							|  |  |  |     } initialized; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Is tracemalloc tracing memory allocations?
 | 
					
						
							|  |  |  |        Variable protected by the GIL */ | 
					
						
							|  |  |  |     int tracing; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* limit of the number of frames in a traceback, 1 by default.
 | 
					
						
							|  |  |  |        Variable protected by the GIL. */ | 
					
						
							|  |  |  |     int max_nframe; | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* use domain in trace key?
 | 
					
						
							|  |  |  |        Variable protected by the GIL. */ | 
					
						
							|  |  |  |     int use_domain; | 
					
						
							|  |  |  | } tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1, 1}; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(TRACE_RAW_MALLOC) && defined(WITH_THREAD)
 | 
					
						
							|  |  |  | /* This lock is needed because tracemalloc_free() is called without
 | 
					
						
							|  |  |  |    the GIL held from PyMem_RawFree(). It cannot acquire the lock because it | 
					
						
							|  |  |  |    would introduce a deadlock in PyThreadState_DeleteCurrent(). */ | 
					
						
							|  |  |  | static PyThread_type_lock tables_lock; | 
					
						
							|  |  |  | #  define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
 | 
					
						
							|  |  |  | #  define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |    /* variables are protected by the GIL */ | 
					
						
							|  |  |  | #  define TABLES_LOCK()
 | 
					
						
							|  |  |  | #  define TABLES_UNLOCK()
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define DEFAULT_DOMAIN 0
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Pack the frame_t structure to reduce the memory footprint. */ | 
					
						
							|  |  |  | typedef struct | 
					
						
							|  |  |  | #ifdef __GNUC__
 | 
					
						
							|  |  |  | __attribute__((packed)) | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_uintptr_t ptr; | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  |     _PyTraceMalloc_domain_t domain; | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | } pointer_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | /* Pack the frame_t structure to reduce the memory footprint on 64-bit
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |    architectures: 12 bytes instead of 16. */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | typedef struct | 
					
						
							|  |  |  | #ifdef __GNUC__
 | 
					
						
							|  |  |  | __attribute__((packed)) | 
					
						
							| 
									
										
										
										
											2014-02-01 03:43:58 +01:00
										 |  |  | #elif defined(_MSC_VER)
 | 
					
						
							|  |  |  | _declspec(align(4)) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-15 14:28:04 +01:00
										 |  |  |     /* filename cannot be NULL: "<unknown>" is used if the Python frame
 | 
					
						
							|  |  |  |        filename is NULL */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     PyObject *filename; | 
					
						
							| 
									
										
										
										
											2016-03-15 21:57:02 +01:00
										 |  |  |     unsigned int lineno; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } frame_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     Py_uhash_t hash; | 
					
						
							|  |  |  |     int nframe; | 
					
						
							|  |  |  |     frame_t frames[1]; | 
					
						
							|  |  |  | } traceback_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TRACEBACK_SIZE(NFRAME) \
 | 
					
						
							|  |  |  |         (sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1)) | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define MAX_NFRAME \
 | 
					
						
							| 
									
										
										
										
											2014-08-16 15:44:02 +02:00
										 |  |  |         ((INT_MAX - (int)sizeof(traceback_t)) / (int)sizeof(frame_t) + 1) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyObject *unknown_filename = NULL; | 
					
						
							|  |  |  | static traceback_t tracemalloc_empty_traceback; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-26 01:06:02 +01:00
										 |  |  | /* Trace of a memory block */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2013-11-26 01:06:02 +01:00
										 |  |  |     /* Size of the memory block in bytes */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     size_t size; | 
					
						
							| 
									
										
										
										
											2013-11-26 01:06:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Traceback where the memory block was allocated */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     traceback_t *traceback; | 
					
						
							|  |  |  | } trace_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | /* Size in bytes of currently traced memory.
 | 
					
						
							|  |  |  |    Protected by TABLES_LOCK(). */ | 
					
						
							|  |  |  | static size_t tracemalloc_traced_memory = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 21:39:49 +01:00
										 |  |  | /* Peak size in bytes of traced memory.
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |    Protected by TABLES_LOCK(). */ | 
					
						
							| 
									
										
										
										
											2013-11-27 21:39:49 +01:00
										 |  |  | static size_t tracemalloc_peak_traced_memory = 0; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 18:28:43 +02:00
										 |  |  | /* Hash table used as a set to intern filenames:
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |    PyObject* => PyObject*. | 
					
						
							|  |  |  |    Protected by the GIL */ | 
					
						
							|  |  |  | static _Py_hashtable_t *tracemalloc_filenames = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  | /* Buffer to store a new traceback in traceback_new().
 | 
					
						
							|  |  |  |    Protected by the GIL. */ | 
					
						
							|  |  |  | static traceback_t *tracemalloc_traceback = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | /* Hash table used as a set to intern tracebacks:
 | 
					
						
							|  |  |  |    traceback_t* => traceback_t* | 
					
						
							|  |  |  |    Protected by the GIL */ | 
					
						
							|  |  |  | static _Py_hashtable_t *tracemalloc_tracebacks = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* pointer (void*) => trace (trace_t).
 | 
					
						
							|  |  |  |    Protected by TABLES_LOCK(). */ | 
					
						
							|  |  |  | static _Py_hashtable_t *tracemalloc_traces = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | tracemalloc_error(const char *format, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     va_list ap; | 
					
						
							|  |  |  |     fprintf(stderr, "tracemalloc: "); | 
					
						
							|  |  |  |     va_start(ap, format); | 
					
						
							|  |  |  |     vfprintf(stderr, format, ap); | 
					
						
							|  |  |  |     va_end(ap); | 
					
						
							|  |  |  |     fprintf(stderr, "\n"); | 
					
						
							|  |  |  |     fflush(stderr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC)
 | 
					
						
							|  |  |  | #define REENTRANT_THREADLOCAL
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* If your OS does not provide native thread local storage, you can implement
 | 
					
						
							|  |  |  |    it manually using a lock. Functions of thread.c cannot be used because | 
					
						
							|  |  |  |    they use PyMem_RawMalloc() which leads to a reentrant call. */ | 
					
						
							|  |  |  | #if !(defined(_POSIX_THREADS) || defined(NT_THREADS))
 | 
					
						
							|  |  |  | #  error "need native thread local storage (TLS)"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 17:45:09 +01:00
										 |  |  | static int tracemalloc_reentrant_key = -1; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Any non-NULL pointer can be used */ | 
					
						
							|  |  |  | #define REENTRANT Py_True
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | get_reentrant(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-22 17:45:09 +01:00
										 |  |  |     void *ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(tracemalloc_reentrant_key != -1); | 
					
						
							|  |  |  |     ptr = PyThread_get_key_value(tracemalloc_reentrant_key); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (ptr != NULL) { | 
					
						
							|  |  |  |         assert(ptr == REENTRANT); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | set_reentrant(int reentrant) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-26 01:08:53 +01:00
										 |  |  |     assert(reentrant == 0 || reentrant == 1); | 
					
						
							| 
									
										
										
										
											2016-03-22 17:45:09 +01:00
										 |  |  |     assert(tracemalloc_reentrant_key != -1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (reentrant) { | 
					
						
							| 
									
										
										
										
											2016-03-22 17:40:07 +01:00
										 |  |  |         assert(!get_reentrant()); | 
					
						
							| 
									
										
										
										
											2013-12-13 11:08:56 +01:00
										 |  |  |         PyThread_set_key_value(tracemalloc_reentrant_key, REENTRANT); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2016-03-22 17:40:07 +01:00
										 |  |  |         assert(get_reentrant()); | 
					
						
							| 
									
										
										
										
											2013-12-13 11:08:56 +01:00
										 |  |  |         PyThread_set_key_value(tracemalloc_reentrant_key, NULL); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* WITH_THREAD not defined: Python compiled without threads,
 | 
					
						
							|  |  |  |    or TRACE_RAW_MALLOC not defined: variable protected by the GIL */ | 
					
						
							|  |  |  | static int tracemalloc_reentrant = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | get_reentrant(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return tracemalloc_reentrant; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | set_reentrant(int reentrant) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(!reentrant || !get_reentrant()); | 
					
						
							|  |  |  |     tracemalloc_reentrant = reentrant; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | static Py_uhash_t | 
					
						
							|  |  |  | hashtable_hash_pyobject(size_t key_size, const void *pkey) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *obj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_HASHTABLE_READ_KEY(key_size, pkey, obj); | 
					
						
							|  |  |  |     return PyObject_Hash(obj); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | hashtable_compare_unicode(size_t key_size, const void *pkey, | 
					
						
							|  |  |  |                           const _Py_hashtable_entry_t *entry) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     PyObject *key1, *key2; | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     _Py_HASHTABLE_READ_KEY(key_size, pkey, key1); | 
					
						
							|  |  |  |     _Py_HASHTABLE_ENTRY_READ_KEY(key_size, entry, key2); | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (key1 != NULL && key2 != NULL) | 
					
						
							|  |  |  |         return (PyUnicode_Compare(key1, key2) == 0); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |         return key1 == key2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Py_uhash_t | 
					
						
							|  |  |  | hashtable_hash_pointer_t(size_t key_size, const void *pkey) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     pointer_t ptr; | 
					
						
							|  |  |  |     Py_uhash_t hash; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_HASHTABLE_READ_KEY(key_size, pkey, ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hash = (Py_uhash_t)_Py_HashPointer((void*)ptr.ptr); | 
					
						
							|  |  |  |     hash ^= ptr.domain; | 
					
						
							|  |  |  |     return hash; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | hashtable_compare_pointer_t(size_t key_size, const void *pkey, | 
					
						
							|  |  |  |                             const _Py_hashtable_entry_t *entry) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     pointer_t ptr1, ptr2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_HASHTABLE_READ_KEY(key_size, pkey, ptr1); | 
					
						
							|  |  |  |     _Py_HASHTABLE_ENTRY_READ_KEY(key_size, entry, ptr2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* compare pointer before domain, because pointer is more likely to be
 | 
					
						
							|  |  |  |        different */ | 
					
						
							|  |  |  |     return (ptr1.ptr == ptr2.ptr && ptr1.domain == ptr2.domain); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static _Py_hashtable_t * | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | hashtable_new(size_t key_size, size_t data_size, | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |               _Py_hashtable_hash_func hash_func, | 
					
						
							|  |  |  |               _Py_hashtable_compare_func compare_func) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-22 12:13:01 +01:00
										 |  |  |     _Py_hashtable_allocator_t hashtable_alloc = {malloc, free}; | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     return _Py_hashtable_new_full(key_size, data_size, 0, | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |                                   hash_func, compare_func, | 
					
						
							| 
									
										
										
										
											2016-03-22 12:13:01 +01:00
										 |  |  |                                   &hashtable_alloc); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void* | 
					
						
							|  |  |  | raw_malloc(size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return allocators.raw.malloc(allocators.raw.ctx, size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | raw_free(void *ptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     allocators.raw.free(allocators.raw.ctx, ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static Py_uhash_t | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | hashtable_hash_traceback(size_t key_size, const void *pkey) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-21 23:05:08 +01:00
										 |  |  |     traceback_t *traceback; | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     _Py_HASHTABLE_READ_KEY(key_size, pkey, traceback); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     return traceback->hash; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | hashtable_compare_traceback(size_t key_size, const void *pkey, | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |                             const _Py_hashtable_entry_t *he) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     traceback_t *traceback1, *traceback2; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     const frame_t *frame1, *frame2; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     _Py_HASHTABLE_READ_KEY(key_size, pkey, traceback1); | 
					
						
							|  |  |  |     _Py_HASHTABLE_ENTRY_READ_KEY(key_size, he, traceback2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (traceback1->nframe != traceback2->nframe) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i=0; i < traceback1->nframe; i++) { | 
					
						
							|  |  |  |         frame1 = &traceback1->frames[i]; | 
					
						
							|  |  |  |         frame2 = &traceback2->frames[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (frame1->lineno != frame2->lineno) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (frame1->filename != frame2->filename) { | 
					
						
							|  |  |  |             assert(PyUnicode_Compare(frame1->filename, frame2->filename) != 0); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyCodeObject *code; | 
					
						
							|  |  |  |     PyObject *filename; | 
					
						
							|  |  |  |     _Py_hashtable_entry_t *entry; | 
					
						
							| 
									
										
										
										
											2016-03-15 21:57:02 +01:00
										 |  |  |     int lineno; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     frame->filename = unknown_filename; | 
					
						
							| 
									
										
										
										
											2016-03-15 21:57:02 +01:00
										 |  |  |     lineno = PyFrame_GetLineNumber(pyframe); | 
					
						
							|  |  |  |     if (lineno < 0) | 
					
						
							|  |  |  |         lineno = 0; | 
					
						
							|  |  |  |     frame->lineno = (unsigned int)lineno; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     code = pyframe->f_code; | 
					
						
							|  |  |  |     if (code == NULL) { | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							| 
									
										
										
										
											2013-11-26 01:18:52 +01:00
										 |  |  |         tracemalloc_error("failed to get the code object of the frame"); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (code->co_filename == NULL) { | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  |         tracemalloc_error("failed to get the filename of the code object"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     filename = code->co_filename; | 
					
						
							|  |  |  |     assert(filename != NULL); | 
					
						
							|  |  |  |     if (filename == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyUnicode_Check(filename)) { | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  |         tracemalloc_error("filename is not an unicode string"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!PyUnicode_IS_READY(filename)) { | 
					
						
							|  |  |  |         /* Don't make a Unicode string ready to avoid reentrant calls
 | 
					
						
							|  |  |  |            to tracemalloc_malloc() or tracemalloc_realloc() */ | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  |         tracemalloc_error("filename is not a ready unicode string"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* intern the filename */ | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_filenames, filename); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (entry != NULL) { | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |         _Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_filenames->key_size, entry, | 
					
						
							|  |  |  |                                      filename); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         /* tracemalloc_filenames is responsible to keep a reference
 | 
					
						
							|  |  |  |            to the filename */ | 
					
						
							|  |  |  |         Py_INCREF(filename); | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |         if (_Py_HASHTABLE_SET_NODATA(tracemalloc_filenames, filename) < 0) { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |             Py_DECREF(filename); | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  |             tracemalloc_error("failed to intern the filename"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* the tracemalloc_filenames table keeps a reference to the filename */ | 
					
						
							|  |  |  |     frame->filename = filename; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static Py_uhash_t | 
					
						
							|  |  |  | traceback_hash(traceback_t *traceback) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* code based on tuplehash() of Objects/tupleobject.c */ | 
					
						
							| 
									
										
										
										
											2013-12-16 23:05:13 +01:00
										 |  |  |     Py_uhash_t x, y;  /* Unsigned for defined overflow behavior. */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     int len = traceback->nframe; | 
					
						
							|  |  |  |     Py_uhash_t mult = _PyHASH_MULTIPLIER; | 
					
						
							|  |  |  |     frame_t *frame; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     x = 0x345678UL; | 
					
						
							|  |  |  |     frame = traceback->frames; | 
					
						
							|  |  |  |     while (--len >= 0) { | 
					
						
							| 
									
										
										
										
											2013-12-16 23:05:13 +01:00
										 |  |  |         y = (Py_uhash_t)PyObject_Hash(frame->filename); | 
					
						
							|  |  |  |         y ^= (Py_uhash_t)frame->lineno; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         frame++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         x = (x ^ y) * mult; | 
					
						
							|  |  |  |         /* the cast might truncate len; that doesn't change hash stability */ | 
					
						
							| 
									
										
										
										
											2013-12-16 23:05:13 +01:00
										 |  |  |         mult += (Py_uhash_t)(82520UL + len + len); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     x += 97531UL; | 
					
						
							|  |  |  |     return x; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | traceback_get_frames(traceback_t *traceback) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyThreadState *tstate; | 
					
						
							|  |  |  |     PyFrameObject *pyframe; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     tstate = PyGILState_GetThisThreadState(); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     tstate = PyThreadState_Get(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     if (tstate == NULL) { | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  |         tracemalloc_error("failed to get the current thread state"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (pyframe = tstate->frame; pyframe != NULL; pyframe = pyframe->f_back) { | 
					
						
							|  |  |  |         tracemalloc_get_frame(pyframe, &traceback->frames[traceback->nframe]); | 
					
						
							|  |  |  |         assert(traceback->frames[traceback->nframe].filename != NULL); | 
					
						
							|  |  |  |         traceback->nframe++; | 
					
						
							|  |  |  |         if (traceback->nframe == tracemalloc_config.max_nframe) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static traceback_t * | 
					
						
							|  |  |  | traceback_new(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     traceback_t *traceback; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     _Py_hashtable_entry_t *entry; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     assert(PyGILState_Check()); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* get frames */ | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     traceback = tracemalloc_traceback; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     traceback->nframe = 0; | 
					
						
							|  |  |  |     traceback_get_frames(traceback); | 
					
						
							|  |  |  |     if (traceback->nframe == 0) | 
					
						
							|  |  |  |         return &tracemalloc_empty_traceback; | 
					
						
							|  |  |  |     traceback->hash = traceback_hash(traceback); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* intern the traceback */ | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_tracebacks, traceback); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (entry != NULL) { | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |         _Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_tracebacks->key_size, entry, | 
					
						
							|  |  |  |                                      traceback); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         traceback_t *copy; | 
					
						
							|  |  |  |         size_t traceback_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         traceback_size = TRACEBACK_SIZE(traceback->nframe); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         copy = raw_malloc(traceback_size); | 
					
						
							|  |  |  |         if (copy == NULL) { | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  |             tracemalloc_error("failed to intern the traceback: malloc failed"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         memcpy(copy, traceback, traceback_size); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |         if (_Py_HASHTABLE_SET_NODATA(tracemalloc_tracebacks, copy) < 0) { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |             raw_free(copy); | 
					
						
							|  |  |  | #ifdef TRACE_DEBUG
 | 
					
						
							|  |  |  |             tracemalloc_error("failed to intern the traceback: putdata failed"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         traceback = copy; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return traceback; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  | tracemalloc_remove_trace(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr) | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     trace_t trace; | 
					
						
							|  |  |  |     int removed; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  |     assert(tracemalloc_config.tracing); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (tracemalloc_config.use_domain) { | 
					
						
							|  |  |  |         pointer_t key = {ptr, domain}; | 
					
						
							|  |  |  |         removed = _Py_HASHTABLE_POP(tracemalloc_traces, key, trace); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         removed = _Py_HASHTABLE_POP(tracemalloc_traces, ptr, trace); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!removed) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(tracemalloc_traced_memory >= trace.size); | 
					
						
							|  |  |  |     tracemalloc_traced_memory -= trace.size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define REMOVE_TRACE(ptr) \
 | 
					
						
							|  |  |  |             tracemalloc_remove_trace(DEFAULT_DOMAIN, (Py_uintptr_t)(ptr)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-24 11:37:15 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  | tracemalloc_add_trace(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr, | 
					
						
							|  |  |  |                       size_t size) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     traceback_t *traceback; | 
					
						
							|  |  |  |     trace_t trace; | 
					
						
							| 
									
										
										
										
											2013-11-24 11:37:15 +01:00
										 |  |  |     int res; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  |     assert(tracemalloc_config.tracing); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     /* first, remove the previous trace (if any) */ | 
					
						
							|  |  |  |     tracemalloc_remove_trace(domain, ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     traceback = traceback_new(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (traceback == NULL) { | 
					
						
							| 
									
										
										
										
											2013-11-24 11:37:15 +01:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     trace.size = size; | 
					
						
							|  |  |  |     trace.traceback = traceback; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (tracemalloc_config.use_domain) { | 
					
						
							|  |  |  |         pointer_t key = {ptr, domain}; | 
					
						
							|  |  |  |         res = _Py_HASHTABLE_SET(tracemalloc_traces, key, trace); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         res = _Py_HASHTABLE_SET(tracemalloc_traces, ptr, trace); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (res != 0) { | 
					
						
							|  |  |  |         return res; | 
					
						
							| 
									
										
										
										
											2013-11-24 11:28:20 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-24 11:37:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     assert(tracemalloc_traced_memory <= PY_SIZE_MAX - size); | 
					
						
							|  |  |  |     tracemalloc_traced_memory += size; | 
					
						
							|  |  |  |     if (tracemalloc_traced_memory > tracemalloc_peak_traced_memory) | 
					
						
							|  |  |  |         tracemalloc_peak_traced_memory = tracemalloc_traced_memory; | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | #define ADD_TRACE(ptr, size) \
 | 
					
						
							|  |  |  |             tracemalloc_add_trace(DEFAULT_DOMAIN, (Py_uintptr_t)(ptr), size) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void* | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |     PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     void *ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 21:40:22 +02:00
										 |  |  |     assert(elsize == 0 || nelem <= PY_SIZE_MAX / elsize); | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (use_calloc) | 
					
						
							|  |  |  |         ptr = alloc->calloc(alloc->ctx, nelem, elsize); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         ptr = alloc->malloc(alloc->ctx, nelem * elsize); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     if (ptr == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |     TABLES_LOCK(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (ADD_TRACE(ptr, nelem * elsize) < 0) { | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |         /* Failed to allocate a trace for the new memory block */ | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |         TABLES_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |         alloc->free(alloc->ctx, ptr); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2013-11-24 11:37:15 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |     TABLES_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     return ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void* | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  | tracemalloc_realloc(void *ctx, void *ptr, size_t new_size) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |     PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     void *ptr2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     if (ptr2 == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     if (ptr != NULL) { | 
					
						
							|  |  |  |         /* an existing memory block has been resized */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |         TABLES_LOCK(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |         REMOVE_TRACE(ptr); | 
					
						
							| 
									
										
										
										
											2013-11-24 12:27:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |         if (ADD_TRACE(ptr2, new_size) < 0) { | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |             /* Memory allocation failed. The error cannot be reported to
 | 
					
						
							|  |  |  |                the caller, because realloc() may already have shrinked the | 
					
						
							|  |  |  |                memory block and so removed bytes. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |                This case is very unlikely: an hash entry has just been | 
					
						
							|  |  |  |                released, so the hash table should have at least one free entry. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                The GIL and the table lock ensures that only one thread is | 
					
						
							|  |  |  |                allocating memory. */ | 
					
						
							|  |  |  |             assert(0 && "should never happen"); | 
					
						
							| 
									
										
										
										
											2013-11-24 11:37:15 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |         TABLES_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     else { | 
					
						
							|  |  |  |         /* new allocation */ | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |         TABLES_LOCK(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |         if (ADD_TRACE(ptr2, new_size) < 0) { | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |             /* Failed to allocate a trace for the new memory block */ | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |             TABLES_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |             alloc->free(alloc->ctx, ptr2); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |         TABLES_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     return ptr2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | tracemalloc_free(void *ctx, void *ptr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |     PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (ptr == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      /* GIL cannot be locked in PyMem_RawFree() because it would introduce
 | 
					
						
							|  |  |  |         a deadlock in PyThreadState_DeleteCurrent(). */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     alloc->free(alloc->ctx, ptr); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     REMOVE_TRACE(ptr); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |     TABLES_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void* | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     void *ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (get_reentrant()) { | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |         PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  |         if (use_calloc) | 
					
						
							|  |  |  |             return alloc->calloc(alloc->ctx, nelem, elsize); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             return alloc->malloc(alloc->ctx, nelem * elsize); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Ignore reentrant call. PyObjet_Malloc() calls PyMem_Malloc() for
 | 
					
						
							|  |  |  |        allocations larger than 512 bytes, don't trace the same memory | 
					
						
							|  |  |  |        allocation twice. */ | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  |     ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     set_reentrant(0); | 
					
						
							|  |  |  |     return ptr; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | static void* | 
					
						
							|  |  |  | tracemalloc_malloc_gil(void *ctx, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return tracemalloc_alloc_gil(0, ctx, 1, size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | static void* | 
					
						
							|  |  |  | tracemalloc_calloc_gil(void *ctx, size_t nelem, size_t elsize) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return tracemalloc_alloc_gil(1, ctx, nelem, elsize); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void* | 
					
						
							|  |  |  | tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     void *ptr2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (get_reentrant()) { | 
					
						
							|  |  |  |         /* Reentrant call to PyMem_Realloc() and PyMem_RawRealloc().
 | 
					
						
							|  |  |  |            Example: PyMem_RawRealloc() is called internally by pymalloc | 
					
						
							|  |  |  |            (_PyObject_Malloc() and  _PyObject_Realloc()) to allocate a new | 
					
						
							|  |  |  |            arena (new_arena()). */ | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |         PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |         if (ptr2 != NULL && ptr != NULL) { | 
					
						
							|  |  |  |             TABLES_LOCK(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |             REMOVE_TRACE(ptr); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |             TABLES_UNLOCK(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |         return ptr2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Ignore reentrant call. PyObjet_Realloc() calls PyMem_Realloc() for
 | 
					
						
							|  |  |  |        allocations larger than 512 bytes. Don't trace the same memory | 
					
						
							|  |  |  |        allocation twice. */ | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ptr2 = tracemalloc_realloc(ctx, ptr, new_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_reentrant(0); | 
					
						
							|  |  |  |     return ptr2; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #ifdef TRACE_RAW_MALLOC
 | 
					
						
							|  |  |  | static void* | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     PyGILState_STATE gil_state; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     void *ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (get_reentrant()) { | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |         PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  |         if (use_calloc) | 
					
						
							|  |  |  |             return alloc->calloc(alloc->ctx, nelem, elsize); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             return alloc->malloc(alloc->ctx, nelem * elsize); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc()
 | 
					
						
							|  |  |  |        indirectly which would call PyGILState_Ensure() if reentrant are not | 
					
						
							|  |  |  |        disabled. */ | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     gil_state = PyGILState_Ensure(); | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  |     ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     PyGILState_Release(gil_state); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  |     ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_reentrant(0); | 
					
						
							|  |  |  |     return ptr; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | static void* | 
					
						
							|  |  |  | tracemalloc_raw_malloc(void *ctx, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return tracemalloc_raw_alloc(0, ctx, 1, size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  | static void* | 
					
						
							|  |  |  | tracemalloc_raw_calloc(void *ctx, size_t nelem, size_t elsize) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return tracemalloc_raw_alloc(1, ctx, nelem, elsize); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void* | 
					
						
							|  |  |  | tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     PyGILState_STATE gil_state; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     void *ptr2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (get_reentrant()) { | 
					
						
							|  |  |  |         /* Reentrant call to PyMem_RawRealloc(). */ | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |         PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ptr2 = alloc->realloc(alloc->ctx, ptr, new_size); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |         if (ptr2 != NULL && ptr != NULL) { | 
					
						
							|  |  |  |             TABLES_LOCK(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |             REMOVE_TRACE(ptr); | 
					
						
							| 
									
										
										
										
											2013-12-04 01:47:46 +01:00
										 |  |  |             TABLES_UNLOCK(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |         return ptr2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc()
 | 
					
						
							|  |  |  |        indirectly which would call PyGILState_Ensure() if reentrant calls are | 
					
						
							|  |  |  |        not disabled. */ | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     gil_state = PyGILState_Ensure(); | 
					
						
							|  |  |  |     ptr2 = tracemalloc_realloc(ctx, ptr, new_size); | 
					
						
							|  |  |  |     PyGILState_Release(gil_state); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     ptr2 = tracemalloc_realloc(ctx, ptr, new_size); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 01:29:35 +01:00
										 |  |  |     set_reentrant(0); | 
					
						
							|  |  |  |     return ptr2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif   /* TRACE_RAW_MALLOC */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | tracemalloc_clear_filename(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry, | 
					
						
							|  |  |  |                            void *user_data) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     PyObject *filename; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_HASHTABLE_ENTRY_READ_KEY(ht->key_size, entry, filename); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     Py_DECREF(filename); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | traceback_free_traceback(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry, | 
					
						
							|  |  |  |                          void *user_data) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     traceback_t *traceback; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_HASHTABLE_ENTRY_READ_KEY(ht->key_size, entry, traceback); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     raw_free(traceback); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | /* reentrant flag must be set to call this function and GIL must be held */ | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | tracemalloc_clear_traces(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     /* The GIL protects variables againt concurrent access */ | 
					
						
							|  |  |  |     assert(PyGILState_Check()); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Disable also reentrant calls to tracemalloc_malloc() to not add a new
 | 
					
						
							|  |  |  |        trace while we are clearing traces */ | 
					
						
							|  |  |  |     assert(get_reentrant()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							|  |  |  |     _Py_hashtable_clear(tracemalloc_traces); | 
					
						
							|  |  |  |     tracemalloc_traced_memory = 0; | 
					
						
							| 
									
										
										
										
											2013-11-27 21:39:49 +01:00
										 |  |  |     tracemalloc_peak_traced_memory = 0; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     TABLES_UNLOCK(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_hashtable_foreach(tracemalloc_tracebacks, traceback_free_traceback, NULL); | 
					
						
							|  |  |  |     _Py_hashtable_clear(tracemalloc_tracebacks); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_hashtable_foreach(tracemalloc_filenames, tracemalloc_clear_filename, NULL); | 
					
						
							|  |  |  |     _Py_hashtable_clear(tracemalloc_filenames); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | tracemalloc_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (tracemalloc_config.initialized == TRACEMALLOC_FINALIZED) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_RuntimeError, | 
					
						
							|  |  |  |                         "the tracemalloc module has been unloaded"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 23:54:42 +01:00
										 |  |  |     if (tracemalloc_config.initialized == TRACEMALLOC_INITIALIZED) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef REENTRANT_THREADLOCAL
 | 
					
						
							|  |  |  |     tracemalloc_reentrant_key = PyThread_create_key(); | 
					
						
							|  |  |  |     if (tracemalloc_reentrant_key == -1) { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |         PyErr_SetFromWindowsErr(0); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |         PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC)
 | 
					
						
							|  |  |  |     if (tables_lock == NULL) { | 
					
						
							|  |  |  |         tables_lock = PyThread_allocate_lock(); | 
					
						
							|  |  |  |         if (tables_lock == NULL) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_RuntimeError, "cannot allocate lock"); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     tracemalloc_filenames = hashtable_new(sizeof(PyObject *), 0, | 
					
						
							|  |  |  |                                           hashtable_hash_pyobject, | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |                                           hashtable_compare_unicode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     tracemalloc_tracebacks = hashtable_new(sizeof(traceback_t *), 0, | 
					
						
							|  |  |  |                                            hashtable_hash_traceback, | 
					
						
							|  |  |  |                                            hashtable_compare_traceback); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (tracemalloc_config.use_domain) { | 
					
						
							|  |  |  |         tracemalloc_traces = hashtable_new(sizeof(pointer_t), | 
					
						
							|  |  |  |                                            sizeof(trace_t), | 
					
						
							|  |  |  |                                            hashtable_hash_pointer_t, | 
					
						
							|  |  |  |                                            hashtable_compare_pointer_t); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         tracemalloc_traces = hashtable_new(sizeof(Py_uintptr_t), | 
					
						
							|  |  |  |                                            sizeof(trace_t), | 
					
						
							|  |  |  |                                            _Py_hashtable_hash_ptr, | 
					
						
							|  |  |  |                                            _Py_hashtable_compare_direct); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (tracemalloc_filenames == NULL || tracemalloc_tracebacks == NULL | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |        || tracemalloc_traces == NULL) { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         PyErr_NoMemory(); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unknown_filename = PyUnicode_FromString("<unknown>"); | 
					
						
							|  |  |  |     if (unknown_filename == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     PyUnicode_InternInPlace(&unknown_filename); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tracemalloc_empty_traceback.nframe = 1; | 
					
						
							|  |  |  |     /* borrowed reference */ | 
					
						
							|  |  |  |     tracemalloc_empty_traceback.frames[0].filename = unknown_filename; | 
					
						
							|  |  |  |     tracemalloc_empty_traceback.frames[0].lineno = 0; | 
					
						
							|  |  |  |     tracemalloc_empty_traceback.hash = traceback_hash(&tracemalloc_empty_traceback); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Disable tracing allocations until hooks are installed. Set
 | 
					
						
							|  |  |  |        also the reentrant flag to detect bugs: fail with an assertion error | 
					
						
							|  |  |  |        if set_reentrant(1) is called while tracing is disabled. */ | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tracemalloc_config.initialized = TRACEMALLOC_INITIALIZED; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | tracemalloc_deinit(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-22 23:54:42 +01:00
										 |  |  |     if (tracemalloc_config.initialized != TRACEMALLOC_INITIALIZED) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     tracemalloc_config.initialized = TRACEMALLOC_FINALIZED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tracemalloc_stop(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* destroy hash tables */ | 
					
						
							|  |  |  |     _Py_hashtable_destroy(tracemalloc_tracebacks); | 
					
						
							|  |  |  |     _Py_hashtable_destroy(tracemalloc_filenames); | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     _Py_hashtable_destroy(tracemalloc_traces); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC)
 | 
					
						
							|  |  |  |     if (tables_lock != NULL) { | 
					
						
							|  |  |  |         PyThread_free_lock(tables_lock); | 
					
						
							|  |  |  |         tables_lock = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef REENTRANT_THREADLOCAL
 | 
					
						
							|  |  |  |     PyThread_delete_key(tracemalloc_reentrant_key); | 
					
						
							| 
									
										
										
										
											2016-03-22 17:45:09 +01:00
										 |  |  |     tracemalloc_reentrant_key = -1; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_XDECREF(unknown_filename); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  | tracemalloc_start(int max_nframe) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-02 21:57:10 +02:00
										 |  |  |     PyMemAllocatorEx alloc; | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     size_t size; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 23:54:42 +01:00
										 |  |  |     if (tracemalloc_init() < 0) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (tracemalloc_config.tracing) { | 
					
						
							|  |  |  |         /* hook already installed: do nothing */ | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 15:44:02 +02:00
										 |  |  |     assert(1 <= max_nframe && max_nframe <= MAX_NFRAME); | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     tracemalloc_config.max_nframe = max_nframe; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* allocate a buffer to store a new traceback */ | 
					
						
							|  |  |  |     size = TRACEBACK_SIZE(max_nframe); | 
					
						
							|  |  |  |     assert(tracemalloc_traceback == NULL); | 
					
						
							|  |  |  |     tracemalloc_traceback = raw_malloc(size); | 
					
						
							|  |  |  |     if (tracemalloc_traceback == NULL) { | 
					
						
							|  |  |  |         PyErr_NoMemory(); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | #ifdef TRACE_RAW_MALLOC
 | 
					
						
							|  |  |  |     alloc.malloc = tracemalloc_raw_malloc; | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  |     alloc.calloc = tracemalloc_raw_calloc; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     alloc.realloc = tracemalloc_raw_realloc; | 
					
						
							|  |  |  |     alloc.free = tracemalloc_free; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     alloc.ctx = &allocators.raw; | 
					
						
							|  |  |  |     PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); | 
					
						
							|  |  |  |     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     alloc.malloc = tracemalloc_malloc_gil; | 
					
						
							| 
									
										
										
										
											2014-05-02 22:31:14 +02:00
										 |  |  |     alloc.calloc = tracemalloc_calloc_gil; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     alloc.realloc = tracemalloc_realloc_gil; | 
					
						
							|  |  |  |     alloc.free = tracemalloc_free; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     alloc.ctx = &allocators.mem; | 
					
						
							|  |  |  |     PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem); | 
					
						
							|  |  |  |     PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     alloc.ctx = &allocators.obj; | 
					
						
							|  |  |  |     PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj); | 
					
						
							|  |  |  |     PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* everything is ready: start tracing Python memory allocations */ | 
					
						
							|  |  |  |     tracemalloc_config.tracing = 1; | 
					
						
							|  |  |  |     set_reentrant(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | tracemalloc_stop(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-22 23:54:42 +01:00
										 |  |  |     if (!tracemalloc_config.tracing) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* stop tracing Python memory allocations */ | 
					
						
							|  |  |  |     tracemalloc_config.tracing = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* set the reentrant flag to detect bugs: fail with an assertion error if
 | 
					
						
							|  |  |  |        set_reentrant(1) is called while tracing is disabled. */ | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* unregister the hook on memory allocators */ | 
					
						
							|  |  |  | #ifdef TRACE_RAW_MALLOC
 | 
					
						
							|  |  |  |     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem); | 
					
						
							|  |  |  |     PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tracemalloc_clear_traces(); | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* release memory */ | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     raw_free(tracemalloc_traceback); | 
					
						
							|  |  |  |     tracemalloc_traceback = NULL; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(tracemalloc_is_tracing_doc, | 
					
						
							|  |  |  |     "is_tracing()->bool\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "True if the tracemalloc module is tracing Python memory allocations,\n" | 
					
						
							|  |  |  |     "False otherwise."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyObject* | 
					
						
							|  |  |  | py_tracemalloc_is_tracing(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyBool_FromLong(tracemalloc_config.tracing); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(tracemalloc_clear_traces_doc, | 
					
						
							|  |  |  |     "clear_traces()\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "Clear traces of memory blocks allocated by Python."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyObject* | 
					
						
							|  |  |  | py_tracemalloc_clear_traces(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!tracemalloc_config.tracing) | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  |     tracemalloc_clear_traces(); | 
					
						
							|  |  |  |     set_reentrant(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyObject* | 
					
						
							|  |  |  | frame_to_pyobject(frame_t *frame) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *frame_obj, *lineno_obj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     frame_obj = PyTuple_New(2); | 
					
						
							|  |  |  |     if (frame_obj == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_INCREF(frame->filename); | 
					
						
							|  |  |  |     PyTuple_SET_ITEM(frame_obj, 0, frame->filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 21:57:02 +01:00
										 |  |  |     lineno_obj = PyLong_FromUnsignedLong(frame->lineno); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (lineno_obj == NULL) { | 
					
						
							|  |  |  |         Py_DECREF(frame_obj); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyTuple_SET_ITEM(frame_obj, 1, lineno_obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return frame_obj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyObject* | 
					
						
							|  |  |  | traceback_to_pyobject(traceback_t *traceback, _Py_hashtable_t *intern_table) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     PyObject *frames, *frame; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (intern_table != NULL) { | 
					
						
							|  |  |  |         if (_Py_HASHTABLE_GET(intern_table, traceback, frames)) { | 
					
						
							|  |  |  |             Py_INCREF(frames); | 
					
						
							|  |  |  |             return frames; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     frames = PyTuple_New(traceback->nframe); | 
					
						
							|  |  |  |     if (frames == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i=0; i < traceback->nframe; i++) { | 
					
						
							|  |  |  |         frame = frame_to_pyobject(&traceback->frames[i]); | 
					
						
							|  |  |  |         if (frame == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(frames); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyTuple_SET_ITEM(frames, i, frame); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (intern_table != NULL) { | 
					
						
							|  |  |  |         if (_Py_HASHTABLE_SET(intern_table, traceback, frames) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(frames); | 
					
						
							|  |  |  |             PyErr_NoMemory(); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /* intern_table keeps a new reference to frames */ | 
					
						
							|  |  |  |         Py_INCREF(frames); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return frames; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyObject* | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  | trace_to_pyobject(_PyTraceMalloc_domain_t domain, trace_t *trace, | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |                   _Py_hashtable_t *intern_tracebacks) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *trace_obj = NULL; | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     PyObject *obj; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     trace_obj = PyTuple_New(3); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (trace_obj == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     obj = PyLong_FromSize_t(domain); | 
					
						
							|  |  |  |     if (obj == NULL) { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         Py_DECREF(trace_obj); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     PyTuple_SET_ITEM(trace_obj, 0, obj); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     obj = PyLong_FromSize_t(trace->size); | 
					
						
							|  |  |  |     if (obj == NULL) { | 
					
						
							|  |  |  |         Py_DECREF(trace_obj); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyTuple_SET_ITEM(trace_obj, 1, obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     obj = traceback_to_pyobject(trace->traceback, intern_tracebacks); | 
					
						
							|  |  |  |     if (obj == NULL) { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         Py_DECREF(trace_obj); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     PyTuple_SET_ITEM(trace_obj, 2, obj); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return trace_obj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     _Py_hashtable_t *traces; | 
					
						
							|  |  |  |     _Py_hashtable_t *tracebacks; | 
					
						
							|  |  |  |     PyObject *list; | 
					
						
							|  |  |  | } get_traces_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | tracemalloc_get_traces_fill(_Py_hashtable_t *traces, _Py_hashtable_entry_t *entry, | 
					
						
							|  |  |  |                             void *user_data) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     get_traces_t *get_traces = user_data; | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  |     _PyTraceMalloc_domain_t domain; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     trace_t *trace; | 
					
						
							|  |  |  |     PyObject *tracemalloc_obj; | 
					
						
							|  |  |  |     int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (tracemalloc_config.use_domain) { | 
					
						
							|  |  |  |         pointer_t key; | 
					
						
							|  |  |  |         _Py_HASHTABLE_ENTRY_READ_KEY(traces->key_size, entry, key); | 
					
						
							|  |  |  |         domain = key.domain; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         domain = DEFAULT_DOMAIN; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |     trace = (trace_t *)_Py_HASHTABLE_ENTRY_DATA(traces, entry); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     tracemalloc_obj = trace_to_pyobject(domain, trace, get_traces->tracebacks); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     if (tracemalloc_obj == NULL) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = PyList_Append(get_traces->list, tracemalloc_obj); | 
					
						
							|  |  |  |     Py_DECREF(tracemalloc_obj); | 
					
						
							|  |  |  |     if (res < 0) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  | tracemalloc_pyobject_decref_cb(_Py_hashtable_t *tracebacks, | 
					
						
							|  |  |  |                                _Py_hashtable_entry_t *entry, | 
					
						
							|  |  |  |                                void *user_data) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-22 12:13:01 +01:00
										 |  |  |     PyObject *obj; | 
					
						
							|  |  |  |     _Py_HASHTABLE_ENTRY_READ_DATA(tracebacks, entry, sizeof(obj), &obj); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     Py_DECREF(obj); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | PyDoc_STRVAR(tracemalloc_get_traces_doc, | 
					
						
							| 
									
										
										
										
											2013-11-25 09:33:18 +01:00
										 |  |  |     "_get_traces() -> list\n" | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     "\n" | 
					
						
							|  |  |  |     "Get traces of all memory blocks allocated by Python.\n" | 
					
						
							|  |  |  |     "Return a list of (size: int, traceback: tuple) tuples.\n" | 
					
						
							|  |  |  |     "traceback is a tuple of (filename: str, lineno: int) tuples.\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "Return an empty list if the tracemalloc module is disabled."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							|  |  |  | py_tracemalloc_get_traces(PyObject *self, PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     get_traces_t get_traces; | 
					
						
							|  |  |  |     int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     get_traces.traces = NULL; | 
					
						
							|  |  |  |     get_traces.tracebacks = NULL; | 
					
						
							|  |  |  |     get_traces.list = PyList_New(0); | 
					
						
							|  |  |  |     if (get_traces.list == NULL) | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!tracemalloc_config.tracing) | 
					
						
							|  |  |  |         return get_traces.list; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-26 00:26:23 +01:00
										 |  |  |     /* the traceback hash table is used temporarily to intern traceback tuple
 | 
					
						
							|  |  |  |        of (filename, lineno) tuples */ | 
					
						
							| 
									
										
										
										
											2016-03-22 12:13:01 +01:00
										 |  |  |     get_traces.tracebacks = hashtable_new(sizeof(traceback_t *), | 
					
						
							|  |  |  |                                           sizeof(PyObject *), | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |                                           _Py_hashtable_hash_ptr, | 
					
						
							|  |  |  |                                           _Py_hashtable_compare_direct); | 
					
						
							|  |  |  |     if (get_traces.tracebacks == NULL) { | 
					
						
							|  |  |  |         PyErr_NoMemory(); | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							|  |  |  |     get_traces.traces = _Py_hashtable_copy(tracemalloc_traces); | 
					
						
							|  |  |  |     TABLES_UNLOCK(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (get_traces.traces == NULL) { | 
					
						
							|  |  |  |         PyErr_NoMemory(); | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_reentrant(1); | 
					
						
							|  |  |  |     err = _Py_hashtable_foreach(get_traces.traces, | 
					
						
							|  |  |  |                                 tracemalloc_get_traces_fill, &get_traces); | 
					
						
							|  |  |  |     set_reentrant(0); | 
					
						
							|  |  |  |     if (err) | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  |     Py_CLEAR(get_traces.list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | finally: | 
					
						
							|  |  |  |     if (get_traces.tracebacks != NULL) { | 
					
						
							|  |  |  |         _Py_hashtable_foreach(get_traces.tracebacks, | 
					
						
							| 
									
										
										
										
											2016-03-21 22:00:58 +01:00
										 |  |  |                               tracemalloc_pyobject_decref_cb, NULL); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         _Py_hashtable_destroy(get_traces.tracebacks); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-22 12:13:01 +01:00
										 |  |  |     if (get_traces.traces != NULL) { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         _Py_hashtable_destroy(get_traces.traces); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:13:01 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return get_traces.list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  | static traceback_t* | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  | tracemalloc_get_traceback(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr) | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     trace_t trace; | 
					
						
							|  |  |  |     int found; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!tracemalloc_config.tracing) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |     if (tracemalloc_config.use_domain) { | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  |         pointer_t key = {ptr, domain}; | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  |         found = _Py_HASHTABLE_GET(tracemalloc_traces, key, trace); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         found = _Py_HASHTABLE_GET(tracemalloc_traces, ptr, trace); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  |     TABLES_UNLOCK(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!found) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return trace.traceback; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | PyDoc_STRVAR(tracemalloc_get_object_traceback_doc, | 
					
						
							| 
									
										
										
										
											2013-11-25 09:33:18 +01:00
										 |  |  |     "_get_object_traceback(obj)\n" | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     "\n" | 
					
						
							|  |  |  |     "Get the traceback where the Python object obj was allocated.\n" | 
					
						
							|  |  |  |     "Return a tuple of (filename: str, lineno: int) tuples.\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "Return None if the tracemalloc module is disabled or did not\n" | 
					
						
							|  |  |  |     "trace the allocation of the object."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							|  |  |  | py_tracemalloc_get_object_traceback(PyObject *self, PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyTypeObject *type; | 
					
						
							|  |  |  |     void *ptr; | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  |     traceback_t *traceback; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     type = Py_TYPE(obj); | 
					
						
							|  |  |  |     if (PyType_IS_GC(type)) | 
					
						
							|  |  |  |         ptr = (void *)((char *)obj - sizeof(PyGC_Head)); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         ptr = (void *)obj; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  |     traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (Py_uintptr_t)ptr); | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  |     if (traceback == NULL) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  |     return traceback_to_pyobject(traceback, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  | #define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | _PyMem_DumpFrame(int fd, frame_t * frame) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PUTS(fd, "  File \""); | 
					
						
							|  |  |  |     _Py_DumpASCII(fd, frame->filename); | 
					
						
							|  |  |  |     PUTS(fd, "\", line "); | 
					
						
							|  |  |  |     _Py_DumpDecimal(fd, frame->lineno); | 
					
						
							|  |  |  |     PUTS(fd, "\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Dump the traceback where a memory block was allocated into file descriptor
 | 
					
						
							|  |  |  |    fd. The function may block on TABLES_LOCK() but it is unlikely. */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _PyMem_DumpTraceback(int fd, const void *ptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     traceback_t *traceback; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  |     traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (Py_uintptr_t)ptr); | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  |     if (traceback == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PUTS(fd, "Memory block allocated at (most recent call first):\n"); | 
					
						
							|  |  |  |     for (i=0; i < traceback->nframe; i++) { | 
					
						
							|  |  |  |         _PyMem_DumpFrame(fd, &traceback->frames[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PUTS(fd, "\n"); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:22:13 +01:00
										 |  |  | #undef PUTS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | PyDoc_STRVAR(tracemalloc_start_doc, | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  |     "start(nframe: int=1)\n" | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     "\n" | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  |     "Start tracing Python memory allocations. Set also the maximum number \n" | 
					
						
							|  |  |  |     "of frames stored in the traceback of a trace to nframe."); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  | py_tracemalloc_start(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  |     Py_ssize_t nframe = 1; | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     int nframe_int; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!PyArg_ParseTuple(args, "|n:start", &nframe)) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 15:44:02 +02:00
										 |  |  |     if (nframe < 1 || nframe > MAX_NFRAME) { | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  |         PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  |                      "the number of frames must be in range [1; %i]", | 
					
						
							| 
									
										
										
										
											2014-08-16 15:44:02 +02:00
										 |  |  |                      MAX_NFRAME); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 23:54:42 +01:00
										 |  |  |     if (tracemalloc_start(nframe_int) < 0) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(tracemalloc_stop_doc, | 
					
						
							|  |  |  |     "stop()\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "Stop tracing Python memory allocations and clear traces\n" | 
					
						
							|  |  |  |     "of memory blocks allocated by Python."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyObject* | 
					
						
							|  |  |  | py_tracemalloc_stop(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     tracemalloc_stop(); | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | PyDoc_STRVAR(tracemalloc_get_traceback_limit_doc, | 
					
						
							|  |  |  |     "get_traceback_limit() -> int\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "Get the maximum number of frames stored in the traceback\n" | 
					
						
							|  |  |  |     "of a trace.\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "By default, a trace of an allocated memory block only stores\n" | 
					
						
							|  |  |  |     "the most recent frame: the limit is 1."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							|  |  |  | py_tracemalloc_get_traceback_limit(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyLong_FromLong(tracemalloc_config.max_nframe); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc, | 
					
						
							|  |  |  |     "get_tracemalloc_memory() -> int\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "Get the memory usage in bytes of the tracemalloc module\n" | 
					
						
							|  |  |  |     "used internally to trace memory allocations."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							|  |  |  | tracemalloc_get_tracemalloc_memory(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     size_t size; | 
					
						
							|  |  |  |     PyObject *size_obj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size = _Py_hashtable_size(tracemalloc_tracebacks); | 
					
						
							|  |  |  |     size += _Py_hashtable_size(tracemalloc_filenames); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							|  |  |  |     size += _Py_hashtable_size(tracemalloc_traces); | 
					
						
							|  |  |  |     TABLES_UNLOCK(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_obj = PyLong_FromSize_t(size); | 
					
						
							|  |  |  |     return Py_BuildValue("N", size_obj); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | PyDoc_STRVAR(tracemalloc_get_traced_memory_doc, | 
					
						
							| 
									
										
										
										
											2013-11-26 10:46:06 +01:00
										 |  |  |     "get_traced_memory() -> (int, int)\n" | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     "\n" | 
					
						
							| 
									
										
										
										
											2013-11-27 21:39:49 +01:00
										 |  |  |     "Get the current size and peak size of memory blocks traced\n" | 
					
						
							|  |  |  |     "by the tracemalloc module as a tuple: (current: int, peak: int)."); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							|  |  |  | tracemalloc_get_traced_memory(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-27 21:39:49 +01:00
										 |  |  |     Py_ssize_t size, peak_size; | 
					
						
							|  |  |  |     PyObject *size_obj, *peak_size_obj; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!tracemalloc_config.tracing) | 
					
						
							|  |  |  |         return Py_BuildValue("ii", 0, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							|  |  |  |     size = tracemalloc_traced_memory; | 
					
						
							| 
									
										
										
										
											2013-11-27 21:39:49 +01:00
										 |  |  |     peak_size = tracemalloc_peak_traced_memory; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     TABLES_UNLOCK(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_obj = PyLong_FromSize_t(size); | 
					
						
							| 
									
										
										
										
											2013-11-27 21:39:49 +01:00
										 |  |  |     peak_size_obj = PyLong_FromSize_t(peak_size); | 
					
						
							|  |  |  |     return Py_BuildValue("NN", size_obj, peak_size_obj); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static PyMethodDef module_methods[] = { | 
					
						
							|  |  |  |     {"is_tracing", (PyCFunction)py_tracemalloc_is_tracing, | 
					
						
							|  |  |  |      METH_NOARGS, tracemalloc_is_tracing_doc}, | 
					
						
							|  |  |  |     {"clear_traces", (PyCFunction)py_tracemalloc_clear_traces, | 
					
						
							|  |  |  |      METH_NOARGS, tracemalloc_clear_traces_doc}, | 
					
						
							|  |  |  |     {"_get_traces", (PyCFunction)py_tracemalloc_get_traces, | 
					
						
							|  |  |  |      METH_NOARGS, tracemalloc_get_traces_doc}, | 
					
						
							|  |  |  |     {"_get_object_traceback", (PyCFunction)py_tracemalloc_get_object_traceback, | 
					
						
							|  |  |  |      METH_O, tracemalloc_get_object_traceback_doc}, | 
					
						
							|  |  |  |     {"start", (PyCFunction)py_tracemalloc_start, | 
					
						
							| 
									
										
										
										
											2013-11-23 12:37:20 +01:00
										 |  |  |       METH_VARARGS, tracemalloc_start_doc}, | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |     {"stop", (PyCFunction)py_tracemalloc_stop, | 
					
						
							|  |  |  |       METH_NOARGS, tracemalloc_stop_doc}, | 
					
						
							|  |  |  |     {"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit, | 
					
						
							|  |  |  |      METH_NOARGS, tracemalloc_get_traceback_limit_doc}, | 
					
						
							|  |  |  |     {"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory, | 
					
						
							|  |  |  |      METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc}, | 
					
						
							|  |  |  |     {"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory, | 
					
						
							|  |  |  |      METH_NOARGS, tracemalloc_get_traced_memory_doc}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* sentinel */ | 
					
						
							|  |  |  |     {NULL, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(module_doc, | 
					
						
							|  |  |  | "Debug module to trace memory blocks allocated by Python."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef module_def = { | 
					
						
							|  |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     "_tracemalloc", | 
					
						
							|  |  |  |     module_doc, | 
					
						
							|  |  |  |     0, /* non-negative size to be able to unload the module */ | 
					
						
							|  |  |  |     module_methods, | 
					
						
							|  |  |  |     NULL, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyMODINIT_FUNC | 
					
						
							|  |  |  | PyInit__tracemalloc(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *m; | 
					
						
							|  |  |  |     m = PyModule_Create(&module_def); | 
					
						
							|  |  |  |     if (m == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (tracemalloc_init() < 0) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | parse_sys_xoptions(PyObject *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *valuelong; | 
					
						
							|  |  |  |     long nframe; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (value == Py_True) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(PyUnicode_Check(value)); | 
					
						
							|  |  |  |     if (PyUnicode_GetLength(value) == 0) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     valuelong = PyLong_FromUnicodeObject(value, 10); | 
					
						
							|  |  |  |     if (valuelong == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     nframe = PyLong_AsLong(valuelong); | 
					
						
							|  |  |  |     Py_DECREF(valuelong); | 
					
						
							|  |  |  |     if (nframe == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 15:44:02 +02:00
										 |  |  |     if (nframe < 1 || nframe > MAX_NFRAME) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Py_SAFE_DOWNCAST(nframe, long, int); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | int | 
					
						
							|  |  |  | _PyTraceMalloc_Init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     char *p; | 
					
						
							|  |  |  |     int nframe; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     assert(PyGILState_Check()); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((p = Py_GETENV("PYTHONTRACEMALLOC")) && *p != '\0') { | 
					
						
							|  |  |  |         char *endptr = p; | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |         long value; | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-16 23:06:19 +01:00
										 |  |  |         errno = 0; | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |         value = strtol(p, &endptr, 10); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         if (*endptr != '\0' | 
					
						
							|  |  |  |             || value < 1 | 
					
						
							| 
									
										
										
										
											2014-08-16 15:44:02 +02:00
										 |  |  |             || value > MAX_NFRAME | 
					
						
							| 
									
										
										
										
											2013-12-16 23:06:19 +01:00
										 |  |  |             || errno == ERANGE) | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |             Py_FatalError("PYTHONTRACEMALLOC: invalid number of frames"); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         nframe = (int)value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         PyObject *xoptions, *key, *value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         xoptions = PySys_GetXOptions(); | 
					
						
							|  |  |  |         if (xoptions == NULL) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         key = PyUnicode_FromString("tracemalloc"); | 
					
						
							|  |  |  |         if (key == NULL) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         value = PyDict_GetItemWithError(xoptions, key); | 
					
						
							|  |  |  |         Py_DECREF(key); | 
					
						
							|  |  |  |         if (value == NULL) { | 
					
						
							|  |  |  |             if (PyErr_Occurred()) | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* -X tracemalloc is not used */ | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         nframe = parse_sys_xoptions(value); | 
					
						
							|  |  |  |         Py_DECREF(value); | 
					
						
							|  |  |  |         if (nframe < 0) { | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |             Py_FatalError("-X tracemalloc=NFRAME: invalid number of frames"); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 22:27:13 +01:00
										 |  |  |     return tracemalloc_start(nframe); | 
					
						
							| 
									
										
										
										
											2013-11-23 12:27:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 12:58:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 10:03:26 +01:00
										 |  |  | void | 
					
						
							|  |  |  | _PyTraceMalloc_Fini(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     assert(PyGILState_Check()); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     tracemalloc_deinit(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-22 13:39:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _PyTraceMalloc_Track(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr, | 
					
						
							|  |  |  |                      size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int res; | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     PyGILState_STATE gil_state; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!tracemalloc_config.tracing) { | 
					
						
							|  |  |  |         /* tracemalloc is not tracing: do nothing */ | 
					
						
							|  |  |  |         return -2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     gil_state = PyGILState_Ensure(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							|  |  |  |     res = tracemalloc_add_trace(domain, ptr, size); | 
					
						
							|  |  |  |     TABLES_UNLOCK(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  |     PyGILState_Release(gil_state); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _PyTraceMalloc_Untrack(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!tracemalloc_config.tracing) { | 
					
						
							|  |  |  |         /* tracemalloc is not tracing: do nothing */ | 
					
						
							|  |  |  |         return -2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TABLES_LOCK(); | 
					
						
							|  |  |  |     tracemalloc_remove_trace(domain, ptr); | 
					
						
							|  |  |  |     TABLES_UNLOCK(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject* | 
					
						
							|  |  |  | _PyTraceMalloc_GetTraceback(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     traceback_t *traceback; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     traceback = tracemalloc_get_traceback(domain, ptr); | 
					
						
							|  |  |  |     if (traceback == NULL) | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return traceback_to_pyobject(traceback, NULL); | 
					
						
							|  |  |  | } |