blob: f42b72ad1a2cd5e19585eb14a73d710db3a9c6fd [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
David Goldblatt9f822a12017-05-22 19:32:04 -070066witnesses_cleanup(witness_tsd_t *witness_tsd) {
67 witness_assert_lockless(witness_tsd_tsdn(witness_tsd));
Jason Evansb2c0d632016-04-13 23:36:15 -070068
69 /* Do nothing. */
70}
Jason Evans174c0c32016-04-25 23:14:40 -070071
72void
David Goldblatt9f822a12017-05-22 19:32:04 -070073witness_prefork(witness_tsd_t *witness_tsd) {
Qi Wang4dec5072017-04-05 22:04:12 -070074 if (!config_debug) {
75 return;
76 }
David Goldblatt9f822a12017-05-22 19:32:04 -070077 witness_tsd->forking = true;
Jason Evans174c0c32016-04-25 23:14:40 -070078}
79
80void
David Goldblatt9f822a12017-05-22 19:32:04 -070081witness_postfork_parent(witness_tsd_t *witness_tsd) {
Qi Wang4dec5072017-04-05 22:04:12 -070082 if (!config_debug) {
83 return;
84 }
David Goldblatt9f822a12017-05-22 19:32:04 -070085 witness_tsd->forking = false;
Jason Evans174c0c32016-04-25 23:14:40 -070086}
Jason Evans108c4a12016-04-26 10:47:22 -070087
88void
David Goldblatt9f822a12017-05-22 19:32:04 -070089witness_postfork_child(witness_tsd_t *witness_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
David Goldblatt9f822a12017-05-22 19:32:04 -070096 witnesses = &witness_tsd->witnesses;
Jason Evans108c4a12016-04-26 10:47:22 -070097 ql_new(witnesses);
98#endif
David Goldblatt9f822a12017-05-22 19:32:04 -070099 witness_tsd->forking = false;
Jason Evans108c4a12016-04-26 10:47:22 -0700100}