Skip to content

Commit bf2d3c7

Browse files
committed
improve DEBUG_BORROW printouts
1 parent 0ff8200 commit bf2d3c7

File tree

5 files changed

+109
-52
lines changed

5 files changed

+109
-52
lines changed

src/libcore/cleanup.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ fn debug_mem() -> bool {
167167
#[cfg(notest)]
168168
#[lang="annihilate"]
169169
pub unsafe fn annihilate() {
170-
use unstable::lang::{local_free, debug_ptr};
170+
use unstable::lang::{local_free};
171+
use unstable::lang;
171172
use io::WriterUtil;
172173
use io;
173174
use libc;
@@ -191,10 +192,10 @@ pub unsafe fn annihilate() {
191192
for each_live_alloc(true) |box, uniq| {
192193
stats.n_total_boxes += 1;
193194
if uniq {
194-
debug_ptr("Managed-uniq: ", &*box);
195+
lang::debug_mem("Managed-uniq: ", &*box);
195196
stats.n_unique_boxes += 1;
196197
} else {
197-
debug_ptr("Immortalizing: ", &*box);
198+
lang::debug_mem("Immortalizing: ", &*box);
198199
(*box).header.ref_count = managed::raw::RC_IMMORTAL;
199200
}
200201
}
@@ -206,13 +207,13 @@ pub unsafe fn annihilate() {
206207
// callback, as the original value may have been freed.
207208
for each_live_alloc(false) |box, uniq| {
208209
if !uniq {
209-
debug_ptr("Invoking tydesc/glue on: ", &*box);
210+
lang::debug_mem("Invoking tydesc/glue on: ", &*box);
210211
let tydesc: *TypeDesc = transmute(copy (*box).header.type_desc);
211212
let drop_glue: DropGlue = transmute(((*tydesc).drop_glue, 0));
212-
debug_ptr("Box data: ", &(*box).data);
213-
debug_ptr("Type descriptor: ", tydesc);
213+
lang::debug_mem("Box data: ", &(*box).data);
214+
lang::debug_mem("Type descriptor: ", tydesc);
214215
drop_glue(to_unsafe_ptr(&tydesc), transmute(&(*box).data));
215-
debug_ptr("Dropped ", &*box);
216+
lang::debug_mem("Dropped ", &*box);
216217
}
217218
}
218219

@@ -224,7 +225,7 @@ pub unsafe fn annihilate() {
224225
// not be valid after.
225226
for each_live_alloc(true) |box, uniq| {
226227
if !uniq {
227-
debug_ptr("About to free: ", &*box);
228+
lang::debug_mem("About to free: ", &*box);
228229
stats.n_bytes_freed +=
229230
(*((*box).header.type_desc)).size
230231
+ sys::size_of::<BoxRepr>();

src/libcore/rt/env.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ 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+
/// Print GC debugging info (true if env var RUST_DEBUG_BORROW is set)
37+
debug_borrow: bool,
3638
}
3739

3840
/// Get the global environment settings

src/libcore/unstable/lang.rs

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use unstable::exchange_alloc;
2020
use cast::transmute;
2121
use task::rt::rust_get_task;
2222
use option::{Option, Some, None};
23+
use io;
2324

2425
#[allow(non_camel_case_types)]
2526
pub type rust_task = c_void;
@@ -109,8 +110,8 @@ pub unsafe fn clear_task_borrow_list() {
109110
let _ = try_take_task_borrow_list();
110111
}
111112

112-
fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
113-
debug_ptr("fail_borrowed: ", box);
113+
unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
114+
debug_borrow("fail_borrowed: ", box, 0, 0, file, line);
114115

115116
match try_take_task_borrow_list() {
116117
None => { // not recording borrows
@@ -145,42 +146,95 @@ fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
145146
#[inline(always)]
146147
pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
147148
let result = transmute(exchange_alloc::malloc(transmute(td), transmute(size)));
148-
debug_ptr("exchange_malloc: ", result);
149+
debug_mem("exchange_malloc: ", result);
149150
return result;
150151
}
151152

152153
/// Because this code is so perf. sensitive, use a static constant so that
153154
/// debug printouts are compiled out most of the time.
154-
static ENABLE_DEBUG_PTR: bool = true;
155+
static ENABLE_DEBUG: bool = true;
155156

156157
#[inline]
157-
pub fn debug_ptr<T>(tag: &'static str, p: *const T) {
158+
pub fn debug_mem<T>(tag: &'static str, p: *const T) {
158159
//! A useful debugging function that prints a pointer + tag + newline
159160
//! without allocating memory.
160161
161-
if ENABLE_DEBUG_PTR && ::rt::env::get().debug_mem {
162-
debug_ptr_slow(tag, p);
162+
if ENABLE_DEBUG && ::rt::env::get().debug_mem {
163+
debug_mem_slow(tag, p);
163164
}
164165

165-
fn debug_ptr_slow<T>(tag: &'static str, p: *const T) {
166-
use io;
166+
fn debug_mem_slow<T>(tag: &'static str, p: *const T) {
167167
let dbg = STDERR_FILENO as io::fd_t;
168-
let letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
169-
'9', 'a', 'b', 'c', 'd', 'e', 'f'];
170168
dbg.write_str(tag);
169+
dbg.write_hex(p as uint);
170+
dbg.write_str("\n");
171+
}
172+
}
173+
174+
#[inline]
175+
unsafe fn debug_borrow<T>(tag: &'static str,
176+
p: *const T,
177+
old_bits: uint,
178+
new_bits: uint,
179+
filename: *c_char,
180+
line: size_t) {
181+
//! A useful debugging function that prints a pointer + tag + newline
182+
//! without allocating memory.
183+
184+
if ENABLE_DEBUG && ::rt::env::get().debug_borrow {
185+
debug_borrow_slow(tag, p, old_bits, new_bits, filename, line);
186+
}
187+
188+
unsafe fn debug_borrow_slow<T>(tag: &'static str,
189+
p: *const T,
190+
old_bits: uint,
191+
new_bits: uint,
192+
filename: *c_char,
193+
line: size_t) {
194+
let dbg = STDERR_FILENO as io::fd_t;
195+
dbg.write_str(tag);
196+
dbg.write_hex(p as uint);
197+
dbg.write_str(" ");
198+
dbg.write_hex(old_bits);
199+
dbg.write_str(" ");
200+
dbg.write_hex(new_bits);
201+
dbg.write_str(" ");
202+
dbg.write_cstr(filename);
203+
dbg.write_str(":");
204+
dbg.write_hex(line as uint);
205+
dbg.write_str("\n");
206+
}
207+
}
208+
209+
trait DebugPrints {
210+
fn write_hex(&self, val: uint);
211+
unsafe fn write_cstr(&self, str: *c_char);
212+
}
171213

214+
impl DebugPrints for io::fd_t {
215+
fn write_hex(&self, mut i: uint) {
216+
let letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
217+
'9', 'a', 'b', 'c', 'd', 'e', 'f'];
172218
static uint_nibbles: uint = ::uint::bytes << 1;
173219
let mut buffer = [0_u8, ..uint_nibbles+1];
174-
let mut i = p as uint;
175220
let mut c = uint_nibbles;
176221
while c > 0 {
177222
c -= 1;
178223
buffer[c] = letters[i & 0xF] as u8;
179224
i >>= 4;
180225
}
181-
dbg.write(buffer.slice(0, uint_nibbles));
226+
self.write(buffer.slice(0, uint_nibbles));
227+
}
182228

183-
dbg.write_str("\n");
229+
unsafe fn write_cstr(&self, p: *c_char) {
230+
use libc::strlen;
231+
use vec;
232+
233+
let len = strlen(p);
234+
let p: *u8 = transmute(p);
235+
do vec::raw::buf_as_slice(p, len as uint) |s| {
236+
self.write(s);
237+
}
184238
}
185239
}
186240

@@ -190,15 +244,15 @@ pub fn debug_ptr<T>(tag: &'static str, p: *const T) {
190244
#[lang="exchange_free"]
191245
#[inline(always)]
192246
pub unsafe fn exchange_free(ptr: *c_char) {
193-
debug_ptr("exchange_free: ", ptr);
247+
debug_mem("exchange_free: ", ptr);
194248
exchange_alloc::free(transmute(ptr))
195249
}
196250

197251
#[lang="malloc"]
198252
#[inline(always)]
199253
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
200254
let result = rustrt::rust_upcall_malloc_noswitch(td, size);
201-
debug_ptr("local_malloc: ", result);
255+
debug_mem("local_malloc: ", result);
202256
return result;
203257
}
204258

@@ -208,7 +262,7 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
208262
#[lang="free"]
209263
#[inline(always)]
210264
pub unsafe fn local_free(ptr: *c_char) {
211-
debug_ptr("local_free: ", ptr);
265+
debug_mem("local_free: ", ptr);
212266
rustrt::rust_upcall_free_noswitch(ptr);
213267
}
214268

@@ -225,38 +279,37 @@ pub unsafe fn borrow_as_imm(a: *u8) {
225279
#[inline(always)]
226280
pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
227281
let a: *mut BoxRepr = transmute(a);
228-
let ref_count = (*a).header.ref_count;
282+
let old_ref_count = (*a).header.ref_count;
283+
let new_ref_count = old_ref_count | FROZEN_BIT;
229284

230-
debug_ptr("borrow_as_imm (ptr) :", a);
231-
debug_ptr(" (ref) :", ref_count as *());
232-
debug_ptr(" (line): ", line as *());
285+
debug_borrow("borrow_as_imm:", a, old_ref_count, new_ref_count, file, line);
233286

234-
if (ref_count & MUT_BIT) != 0 {
287+
if (old_ref_count & MUT_BIT) != 0 {
235288
fail_borrowed(a, file, line);
236289
}
237290

238-
(*a).header.ref_count = ref_count | FROZEN_BIT;
291+
(*a).header.ref_count = new_ref_count;
239292

240-
ref_count
293+
old_ref_count
241294
}
242295

243296
#[cfg(not(stage0))]
244297
#[lang="borrow_as_mut"]
245298
#[inline(always)]
246299
pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
247300
let a: *mut BoxRepr = transmute(a);
301+
let old_ref_count = (*a).header.ref_count;
302+
let new_ref_count = old_ref_count | MUT_BIT | FROZEN_BIT;
248303

249-
debug_ptr("borrow_as_mut (ptr): ", a);
250-
debug_ptr(" (line): ", line as *());
304+
debug_borrow("borrow_as_mut:", a, old_ref_count, new_ref_count, file, line);
251305

252-
let ref_count = (*a).header.ref_count;
253-
if (ref_count & (MUT_BIT|FROZEN_BIT)) != 0 {
306+
if (old_ref_count & (MUT_BIT|FROZEN_BIT)) != 0 {
254307
fail_borrowed(a, file, line);
255308
}
256309

257-
(*a).header.ref_count = ref_count | MUT_BIT | FROZEN_BIT;
310+
(*a).header.ref_count = new_ref_count;
258311

259-
ref_count
312+
old_ref_count
260313
}
261314

262315

@@ -267,6 +320,7 @@ pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
267320
if (old_ref_count & ALL_BITS) == 0 {
268321
// was not borrowed before
269322
let a: *mut BoxRepr = transmute(a);
323+
debug_borrow("record_borrow:", a, old_ref_count, 0, file, line);
270324
do swap_task_borrow_list |borrow_list| {
271325
let mut borrow_list = borrow_list;
272326
borrow_list.push(BorrowRecord {box: a, file: file, line: line});
@@ -282,6 +336,7 @@ pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
282336
if (old_ref_count & ALL_BITS) == 0 {
283337
// was not borrowed before
284338
let a: *mut BoxRepr = transmute(a);
339+
debug_borrow("unrecord_borrow:", a, old_ref_count, 0, file, line);
285340
do swap_task_borrow_list |borrow_list| {
286341
let mut borrow_list = borrow_list;
287342
let br = BorrowRecord {box: a, file: file, line: line};
@@ -317,21 +372,20 @@ pub unsafe fn return_to_mut(a: *u8) {
317372
#[cfg(not(stage0))]
318373
#[lang="return_to_mut"]
319374
#[inline(always)]
320-
pub unsafe fn return_to_mut(a: *u8, old_ref_count: uint,
375+
pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
321376
file: *c_char, line: size_t) {
322377
// Sometimes the box is null, if it is conditionally frozen.
323378
// See e.g. #4904.
324379
if !a.is_null() {
325380
let a: *mut BoxRepr = transmute(a);
326-
let ref_count = (*a).header.ref_count;
327-
let combined = (ref_count & !ALL_BITS) | (old_ref_count & ALL_BITS);
328-
(*a).header.ref_count = combined;
329-
330-
debug_ptr("return_to_mut (ptr) : ", a);
331-
debug_ptr(" (line): ", line as *());
332-
debug_ptr(" (old) : ", old_ref_count as *());
333-
debug_ptr(" (new) : ", ref_count as *());
334-
debug_ptr(" (comb): ", combined as *());
381+
let old_ref_count = (*a).header.ref_count;
382+
let new_ref_count =
383+
(old_ref_count & !ALL_BITS) | (orig_ref_count & ALL_BITS);
384+
385+
debug_borrow("return_to_mut:",
386+
a, old_ref_count, new_ref_count, file, line);
387+
388+
(*a).header.ref_count = new_ref_count;
335389
}
336390
}
337391

@@ -355,10 +409,7 @@ pub unsafe fn check_not_borrowed(a: *u8,
355409
line: size_t) {
356410
let a: *mut BoxRepr = transmute(a);
357411
let ref_count = (*a).header.ref_count;
358-
debug_ptr("check_not_borrowed (ptr) : ", a);
359-
debug_ptr(" (line): ", line as *());
360-
debug_ptr(" (rc) : ", ref_count as *());
361-
412+
debug_borrow("check_not_borrowed:", a, ref_count, 0, file, line);
362413
if (ref_count & FROZEN_BIT) != 0 {
363414
fail_borrowed(a, file, line);
364415
}

src/rt/rust_env.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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_BORROW "RUST_DEBUG_BORROW"
2728

2829
#if defined(__WIN32__)
2930
static int
@@ -130,6 +131,7 @@ load_env(int argc, char **argv) {
130131
env->argc = argc;
131132
env->argv = argv;
132133
env->debug_mem = getenv(RUST_DEBUG_MEM) != NULL;
134+
env->debug_borrow = getenv(RUST_DEBUG_BORROW) != NULL;
133135
return env;
134136
}
135137

src/rt/rust_env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct rust_env {
2828
int argc;
2929
char **argv;
3030
rust_bool debug_mem;
31+
rust_bool debug_borrow;
3132
};
3233

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

0 commit comments

Comments
 (0)