blob: 0e910dca3fc6d507bc38fe9a3a9efab82f7c2ddb [file] [log] [blame]
Jason Evansc0cc5db2017-01-19 21:41:41 -08001#define JEMALLOC_WITNESS_C_
David Goldblatt743d9402017-04-10 18:17:55 -07002#include "jemalloc/internal/jemalloc_preamble.h"
3#include "jemalloc/internal/jemalloc_internal_includes.h"
Jason Evansb2c0d632016-04-13 23:36:15 -07004
David Goldblattd9ec36e2017-04-11 14:43:12 -07005#include "jemalloc/internal/assert.h"
David Goldblatt54373be2017-04-11 13:06:31 -07006#include "jemalloc/internal/malloc_io.h"
7
Jason Evansb2c0d632016-04-13 23:36:15 -07008void
9witness_init(witness_t *witness, const char *name, witness_rank_t rank,
Jason Evansc4c25922017-01-15 16:56:30 -080010 witness_comp_t *comp, void *opaque) {
Jason Evansb2c0d632016-04-13 23:36:15 -070011 witness->name = name;
12 witness->rank = rank;
13 witness->comp = comp;
Jason Evanse75e9be2016-04-17 12:55:10 -070014 witness->opaque = opaque;
Jason Evansb2c0d632016-04-13 23:36:15 -070015}
16
Jason Evansa268af52017-05-01 23:10:42 -070017static void
18witness_lock_error_impl(const witness_list_t *witnesses,
19 const witness_t *witness) {
Jason Evansb2c0d632016-04-13 23:36:15 -070020 witness_t *w;
21
22 malloc_printf("<jemalloc>: Lock rank order reversal:");
23 ql_foreach(w, witnesses, link) {
24 malloc_printf(" %s(%u)", w->name, w->rank);
25 }
26 malloc_printf(" %s(%u)\n", witness->name, witness->rank);
27 abort();
28}
Jason Evansa268af52017-05-01 23:10:42 -070029witness_lock_error_t *JET_MUTABLE witness_lock_error = witness_lock_error_impl;
Jason Evansb2c0d632016-04-13 23:36:15 -070030
Jason Evansa268af52017-05-01 23:10:42 -070031static void
32witness_owner_error_impl(const witness_t *witness) {
Jason Evansb2c0d632016-04-13 23:36:15 -070033 malloc_printf("<jemalloc>: Should own %s(%u)\n", witness->name,
34 witness->rank);
35 abort();
36}
Jason Evansa268af52017-05-01 23:10:42 -070037witness_owner_error_t *JET_MUTABLE witness_owner_error =
38 witness_owner_error_impl;
Jason Evansb2c0d632016-04-13 23:36:15 -070039
Jason Evansa268af52017-05-01 23:10:42 -070040static void
41witness_not_owner_error_impl(const witness_t *witness) {
Jason Evansb2c0d632016-04-13 23:36:15 -070042 malloc_printf("<jemalloc>: Should not own %s(%u)\n", witness->name,
43 witness->rank);
44 abort();
45}
Jason Evansa268af52017-05-01 23:10:42 -070046witness_not_owner_error_t *JET_MUTABLE witness_not_owner_error =
47 witness_not_owner_error_impl;
Jason Evansb2c0d632016-04-13 23:36:15 -070048
Jason Evansa268af52017-05-01 23:10:42 -070049static void
50witness_depth_error_impl(const witness_list_t *witnesses,
Jason Evansd0e93ad2017-01-21 15:12:03 -080051 witness_rank_t rank_inclusive, unsigned depth) {
Jason Evansb2c0d632016-04-13 23:36:15 -070052 witness_t *w;
53
Jason Evansd0e93ad2017-01-21 15:12:03 -080054 malloc_printf("<jemalloc>: Should own %u lock%s of rank >= %u:", depth,
55 (depth != 1) ? "s" : "", rank_inclusive);
Jason Evansb2c0d632016-04-13 23:36:15 -070056 ql_foreach(w, witnesses, link) {
57 malloc_printf(" %s(%u)", w->name, w->rank);
58 }
59 malloc_printf("\n");
60 abort();
61}
Jason Evansa268af52017-05-01 23:10:42 -070062witness_depth_error_t *JET_MUTABLE witness_depth_error =
63 witness_depth_error_impl;
Jason Evansb2c0d632016-04-13 23:36:15 -070064
65void
Jason Evansc4c25922017-01-15 16:56:30 -080066witnesses_cleanup(tsd_t *tsd) {
Jason Evansc1e00ef2016-05-10 22:21:10 -070067 witness_assert_lockless(tsd_tsdn(tsd));
Jason Evansb2c0d632016-04-13 23:36:15 -070068
69 /* Do nothing. */
70}
Jason Evans174c0c32016-04-25 23:14:40 -070071
72void
Jason Evansc4c25922017-01-15 16:56:30 -080073witness_prefork(tsd_t *tsd) {
Qi Wang4dec5072017-04-05 22:04:12 -070074 if (!config_debug) {
75 return;
76 }
Jason Evans174c0c32016-04-25 23:14:40 -070077 tsd_witness_fork_set(tsd, true);
78}
79
80void
Jason Evansc4c25922017-01-15 16:56:30 -080081witness_postfork_parent(tsd_t *tsd) {
Qi Wang4dec5072017-04-05 22:04:12 -070082 if (!config_debug) {
83 return;
84 }
Jason Evans174c0c32016-04-25 23:14:40 -070085 tsd_witness_fork_set(tsd, false);
86}
Jason Evans108c4a12016-04-26 10:47:22 -070087
88void
Jason Evansc4c25922017-01-15 16:56:30 -080089witness_postfork_child(tsd_t *tsd) {
Qi Wang4dec5072017-04-05 22:04:12 -070090 if (!config_debug) {
91 return;
92 }
Jason Evans108c4a12016-04-26 10:47:22 -070093#ifndef JEMALLOC_MUTEX_INIT_CB
94 witness_list_t *witnesses;
95
96 witnesses = tsd_witnessesp_get(tsd);
97 ql_new(witnesses);
98#endif
99 tsd_witness_fork_set(tsd, false);
100}