Skip to content

Commit 3402435

Browse files
committed
Change borrow debugging so it is disabled by -O
1 parent 9bded76 commit 3402435

File tree

7 files changed

+129
-73
lines changed

7 files changed

+129
-73
lines changed

src/libcore/rt/env.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ pub struct Environment {
3333
argv: **c_char,
3434
/// Print GC debugging info (true if env var RUST_DEBUG_MEM is set)
3535
debug_mem: bool,
36-
/// Track origin of `@mut` borrows (true if env var RUST_DEBUG_BORROWS is set)
37-
debug_borrows: bool
3836
}
3937

4038
/// Get the global environment settings

src/libcore/unstable/lang.rs

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use sys;
1919
use unstable::exchange_alloc;
2020
use cast::transmute;
2121
use task::rt::rust_get_task;
22-
use option::{Some, None};
22+
use option::{Option, Some, None};
2323

2424
#[allow(non_camel_case_types)]
2525
pub type rust_task = c_void;
@@ -79,6 +79,19 @@ struct BorrowRecord {
7979
line: size_t
8080
}
8181

82+
fn try_take_task_borrow_list() -> Option<~[BorrowRecord]> {
83+
unsafe {
84+
let cur_task = rust_get_task();
85+
let ptr = rustrt::rust_take_task_borrow_list(cur_task);
86+
if ptr.is_null() {
87+
None
88+
} else {
89+
let v: ~[BorrowRecord] = transmute(ptr);
90+
Some(v)
91+
}
92+
}
93+
}
94+
8295
fn swap_task_borrow_list(f: &fn(~[BorrowRecord]) -> ~[BorrowRecord]) {
8396
unsafe {
8497
let cur_task = rust_get_task();
@@ -93,23 +106,20 @@ fn swap_task_borrow_list(f: &fn(~[BorrowRecord]) -> ~[BorrowRecord]) {
93106

94107
pub unsafe fn clear_task_borrow_list() {
95108
// pub because it is used by the box annihilator.
96-
let cur_task = rust_get_task();
97-
let ptr = rustrt::rust_take_task_borrow_list(cur_task);
98-
if !ptr.is_null() {
99-
let _: ~[BorrowRecord] = transmute(ptr);
100-
}
109+
let _ = try_take_task_borrow_list();
101110
}
102111

103112
fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
104113
debug_ptr("fail_borrowed: ", box);
105114

106-
if !::rt::env::get().debug_borrows {
107-
let msg = "borrowed";
108-
do str::as_buf(msg) |msg_p, _| {
109-
fail_(msg_p as *c_char, file, line);
115+
match try_take_task_borrow_list() {
116+
None => { // not recording borrows
117+
let msg = "borrowed";
118+
do str::as_buf(msg) |msg_p, _| {
119+
fail_(msg_p as *c_char, file, line);
120+
}
110121
}
111-
} else {
112-
do swap_task_borrow_list |borrow_list| {
122+
Some(borrow_list) => { // recording borrows
113123
let mut msg = ~"borrowed";
114124
let mut sep = " at ";
115125
for borrow_list.each_reverse |entry| {
@@ -126,7 +136,6 @@ fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
126136
do str::as_buf(msg) |msg_p, _| {
127137
fail_(msg_p as *c_char, file, line)
128138
}
129-
borrow_list
130139
}
131140
}
132141
}
@@ -211,34 +220,6 @@ pub unsafe fn borrow_as_imm(a: *u8) {
211220
(*a).header.ref_count |= FROZEN_BIT;
212221
}
213222

214-
fn add_borrow_to_task_list(a: *mut BoxRepr, file: *c_char, line: size_t) {
215-
do swap_task_borrow_list |borrow_list| {
216-
let mut borrow_list = borrow_list;
217-
borrow_list.push(BorrowRecord {box: a, file: file, line: line});
218-
borrow_list
219-
}
220-
}
221-
222-
fn remove_borrow_from_task_list(a: *mut BoxRepr, file: *c_char, line: size_t) {
223-
do swap_task_borrow_list |borrow_list| {
224-
let mut borrow_list = borrow_list;
225-
let br = BorrowRecord {box: a, file: file, line: line};
226-
match borrow_list.rposition_elem(&br) {
227-
Some(idx) => {
228-
borrow_list.remove(idx);
229-
borrow_list
230-
}
231-
None => {
232-
let err = fmt!("no borrow found, br=%?, borrow_list=%?",
233-
br, borrow_list);
234-
do str::as_buf(err) |msg_p, _| {
235-
fail_(msg_p as *c_char, file, line)
236-
}
237-
}
238-
}
239-
}
240-
}
241-
242223
#[cfg(not(stage0))]
243224
#[lang="borrow_as_imm"]
244225
#[inline(always)]
@@ -252,12 +233,8 @@ pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
252233

253234
if (ref_count & MUT_BIT) != 0 {
254235
fail_borrowed(a, file, line);
255-
} else {
256-
(*a).header.ref_count |= FROZEN_BIT;
257-
if ::rt::env::get().debug_borrows {
258-
add_borrow_to_task_list(a, file, line);
259-
}
260236
}
237+
261238
ref_count
262239
}
263240

@@ -273,15 +250,53 @@ pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
273250
let ref_count = (*a).header.ref_count;
274251
if (ref_count & (MUT_BIT|FROZEN_BIT)) != 0 {
275252
fail_borrowed(a, file, line);
276-
} else {
277-
(*a).header.ref_count |= (MUT_BIT|FROZEN_BIT);
278-
if ::rt::env::get().debug_borrows {
279-
add_borrow_to_task_list(a, file, line);
280-
}
281253
}
282254
ref_count
283255
}
284256

257+
258+
#[cfg(not(stage0))]
259+
#[lang="record_borrow"]
260+
pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
261+
file: *c_char, line: size_t) {
262+
if (old_ref_count & ALL_BITS) == 0 {
263+
// was not borrowed before
264+
let a: *mut BoxRepr = transmute(a);
265+
do swap_task_borrow_list |borrow_list| {
266+
let mut borrow_list = borrow_list;
267+
borrow_list.push(BorrowRecord {box: a, file: file, line: line});
268+
borrow_list
269+
}
270+
}
271+
}
272+
273+
#[cfg(not(stage0))]
274+
#[lang="unrecord_borrow"]
275+
pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
276+
file: *c_char, line: size_t) {
277+
if (old_ref_count & ALL_BITS) == 0 {
278+
// was not borrowed before
279+
let a: *mut BoxRepr = transmute(a);
280+
do swap_task_borrow_list |borrow_list| {
281+
let mut borrow_list = borrow_list;
282+
let br = BorrowRecord {box: a, file: file, line: line};
283+
match borrow_list.rposition_elem(&br) {
284+
Some(idx) => {
285+
borrow_list.remove(idx);
286+
borrow_list
287+
}
288+
None => {
289+
let err = fmt!("no borrow found, br=%?, borrow_list=%?",
290+
br, borrow_list);
291+
do str::as_buf(err) |msg_p, _| {
292+
fail_(msg_p as *c_char, file, line)
293+
}
294+
}
295+
}
296+
}
297+
}
298+
}
299+
285300
#[cfg(stage0)]
286301
#[lang="return_to_mut"]
287302
#[inline(always)]
@@ -312,10 +327,6 @@ pub unsafe fn return_to_mut(a: *u8, old_ref_count: uint,
312327
debug_ptr(" (old) : ", old_ref_count as *());
313328
debug_ptr(" (new) : ", ref_count as *());
314329
debug_ptr(" (comb): ", combined as *());
315-
316-
if ::rt::env::get().debug_borrows {
317-
remove_borrow_from_task_list(a, file, line);
318-
}
319330
}
320331
}
321332

src/librustc/middle/lang_items.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,20 @@ pub enum LangItem {
7070
ReturnToMutFnLangItem, // 32
7171
CheckNotBorrowedFnLangItem, // 33
7272
StrDupUniqFnLangItem, // 34
73+
RecordBorrowFnLangItem, // 35
74+
UnrecordBorrowFnLangItem, // 36
7375

74-
StartFnLangItem, // 35
76+
StartFnLangItem, // 37
7577
}
7678

7779
pub struct LanguageItems {
78-
items: [Option<def_id>, ..36]
80+
items: [Option<def_id>, ..38]
7981
}
8082

8183
pub impl LanguageItems {
8284
pub fn new() -> LanguageItems {
8385
LanguageItems {
84-
items: [ None, ..36 ]
86+
items: [ None, ..38 ]
8587
}
8688
}
8789

@@ -133,8 +135,10 @@ pub impl LanguageItems {
133135
32 => "return_to_mut",
134136
33 => "check_not_borrowed",
135137
34 => "strdup_uniq",
138+
35 => "record_borrow",
139+
36 => "unrecord_borrow",
136140

137-
35 => "start",
141+
37 => "start",
138142

139143
_ => "???"
140144
}
@@ -251,6 +255,12 @@ pub impl LanguageItems {
251255
pub fn strdup_uniq_fn(&const self) -> def_id {
252256
self.items[StrDupUniqFnLangItem as uint].get()
253257
}
258+
pub fn record_borrow_fn(&const self) -> def_id {
259+
self.items[RecordBorrowFnLangItem as uint].get()
260+
}
261+
pub fn unrecord_borrow_fn(&const self) -> def_id {
262+
self.items[UnrecordBorrowFnLangItem as uint].get()
263+
}
254264
pub fn start_fn(&const self) -> def_id {
255265
self.items[StartFnLangItem as uint].get()
256266
}
@@ -302,6 +312,8 @@ fn LanguageItemCollector(crate: @crate,
302312
item_refs.insert(@~"check_not_borrowed",
303313
CheckNotBorrowedFnLangItem as uint);
304314
item_refs.insert(@~"strdup_uniq", StrDupUniqFnLangItem as uint);
315+
item_refs.insert(@~"record_borrow", RecordBorrowFnLangItem as uint);
316+
item_refs.insert(@~"unrecord_borrow", UnrecordBorrowFnLangItem as uint);
305317
item_refs.insert(@~"start", StartFnLangItem as uint);
306318

307319
LanguageItemCollector {

src/librustc/middle/trans/common.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,37 @@ pub fn add_clean_return_to_mut(bcx: block,
489489
clean_temp(
490490
frozen_val_ref,
491491
|bcx| {
492+
let mut bcx = bcx;
493+
494+
let box_ptr =
495+
build::Load(bcx,
496+
build::PointerCast(bcx,
497+
frozen_val_ref,
498+
T_ptr(T_ptr(T_i8()))));
499+
500+
let bits_val =
501+
build::Load(bcx,
502+
bits_val_ref);
503+
504+
if bcx.tcx().sess.opts.optimize == session::No {
505+
bcx = callee::trans_lang_call(
506+
bcx,
507+
bcx.tcx().lang_items.unrecord_borrow_fn(),
508+
~[
509+
box_ptr,
510+
bits_val,
511+
filename_val,
512+
line_val
513+
],
514+
expr::Ignore);
515+
}
516+
492517
callee::trans_lang_call(
493518
bcx,
494519
bcx.tcx().lang_items.return_to_mut_fn(),
495520
~[
496-
build::Load(bcx,
497-
build::PointerCast(bcx,
498-
frozen_val_ref,
499-
T_ptr(T_ptr(T_i8())))),
500-
build::Load(bcx, bits_val_ref),
521+
box_ptr,
522+
bits_val,
501523
filename_val,
502524
line_val
503525
],

src/librustc/middle/trans/datum.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ use middle::trans::type_of;
101101
use middle::ty;
102102
use util::common::indenter;
103103
use util::ppaux::ty_to_str;
104+
use driver::session;
104105

105106
use core::container::Set; // XXX: this should not be necessary
106107
use core::to_bytes;
@@ -564,19 +565,34 @@ pub impl Datum {
564565
DynaMut => bcx.tcx().lang_items.borrow_as_mut_fn(),
565566
};
566567

568+
let box_ptr = Load(bcx,
569+
PointerCast(bcx,
570+
scratch.val,
571+
T_ptr(T_ptr(T_i8()))));
572+
567573
bcx = callee::trans_lang_call(
568574
bcx,
569575
freeze_did,
570576
~[
571-
Load(bcx,
572-
PointerCast(bcx,
573-
scratch.val,
574-
T_ptr(T_ptr(T_i8())))),
577+
box_ptr,
575578
filename,
576579
line
577580
],
578581
expr::SaveIn(scratch_bits.val));
579582

583+
if bcx.tcx().sess.opts.optimize == session::No {
584+
bcx = callee::trans_lang_call(
585+
bcx,
586+
bcx.tcx().lang_items.record_borrow_fn(),
587+
~[
588+
box_ptr,
589+
Load(bcx, scratch_bits.val),
590+
filename,
591+
line
592+
],
593+
expr::Ignore);
594+
}
595+
580596
add_clean_return_to_mut(
581597
cleanup_bcx, scratch.val, scratch_bits.val,
582598
filename, line);

src/rt/rust_env.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#define RUST_SEED "RUST_SEED"
2525
#define RUST_POISON_ON_FREE "RUST_POISON_ON_FREE"
2626
#define RUST_DEBUG_MEM "RUST_DEBUG_MEM"
27-
#define RUST_DEBUG_BORROWS "RUST_DEBUG_BORROWS"
2827

2928
#if defined(__WIN32__)
3029
static int
@@ -131,7 +130,6 @@ load_env(int argc, char **argv) {
131130
env->argc = argc;
132131
env->argv = argv;
133132
env->debug_mem = getenv(RUST_DEBUG_MEM) != NULL;
134-
env->debug_borrows = getenv(RUST_DEBUG_BORROWS) != NULL;
135133
return env;
136134
}
137135

src/rt/rust_env.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct rust_env {
2828
int argc;
2929
char **argv;
3030
rust_bool debug_mem;
31-
rust_bool debug_borrows;
3231
};
3332

3433
rust_env* load_env(int argc, char **argv);

0 commit comments

Comments
 (0)