Skip to content

Commit b53da4b

Browse files
committed
auto merge of #5342 : brson/rust/debug-mem, r=brson
Fixes #5341
2 parents ebba8b4 + 63d1865 commit b53da4b

File tree

7 files changed

+63
-7
lines changed

7 files changed

+63
-7
lines changed

src/libcore/cleanup.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ unsafe fn each_live_alloc(f: &fn(box: *mut BoxRepr, uniq: bool) -> bool) {
145145

146146
#[cfg(unix)]
147147
fn debug_mem() -> bool {
148-
use os;
149-
use libc;
150-
do os::as_c_charp("RUST_DEBUG_MEM") |p| {
151-
unsafe { libc::getenv(p) != null() }
152-
}
148+
::rt::env::get().debug_mem
153149
}
154150

155151
#[cfg(windows)]

src/libcore/rt/env.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Runtime environment settings
12+
13+
use libc::{size_t, c_char, c_int};
14+
15+
pub struct Environment {
16+
/// The number of threads to use by default
17+
num_sched_threads: size_t,
18+
/// The minimum size of a stack segment
19+
min_stack_size: size_t,
20+
/// The maximum amount of total stack per task before aborting
21+
max_stack_size: size_t,
22+
/// The default logging configuration
23+
logspec: *c_char,
24+
/// Record and report detailed information about memory leaks
25+
detailed_leaks: bool,
26+
/// Seed the random number generator
27+
rust_seed: *c_char,
28+
/// Poison allocations on free
29+
poison_on_free: bool,
30+
/// The argc value passed to main
31+
argc: c_int,
32+
/// The argv value passed to main
33+
argv: **c_char,
34+
/// Print GC debugging info
35+
debug_mem: bool
36+
}
37+
38+
/// Get the global environment settings
39+
/// # Safety Note
40+
/// This will abort the process if run outside of task context
41+
pub fn get() -> &Environment {
42+
unsafe { rust_get_rt_env() }
43+
}
44+
45+
extern {
46+
fn rust_get_rt_env() -> &Environment;
47+
}

src/libcore/rt/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ mod work_queue;
4545
mod stack;
4646
mod context;
4747
mod thread;
48+
pub mod env;

src/rt/rust_builtin.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,11 @@ rust_dbg_extern_identity_u8(char u) {
876876
return u;
877877
}
878878

879+
extern "C" rust_env*
880+
rust_get_rt_env() {
881+
rust_task *task = rust_get_current_task();
882+
return task->kernel->env;
883+
}
879884

880885
//
881886
// Local Variables:

src/rt/rust_env.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define DETAILED_LEAKS "DETAILED_LEAKS"
2424
#define RUST_SEED "RUST_SEED"
2525
#define RUST_POISON_ON_FREE "RUST_POISON_ON_FREE"
26+
#define RUST_DEBUG_MEM "RUST_DEBUG_MEM"
2627

2728
#if defined(__WIN32__)
2829
static int
@@ -128,6 +129,7 @@ load_env(int argc, char **argv) {
128129
env->poison_on_free = getenv(RUST_POISON_ON_FREE) != NULL;
129130
env->argc = argc;
130131
env->argv = argv;
132+
env->debug_mem = getenv(RUST_DEBUG_MEM) != NULL;
131133
return env;
132134
}
133135

src/rt/rust_env.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414

1515
#include "rust_globals.h"
1616

17+
// Avoiding 'bool' type here since I'm not sure it has a standard size
18+
typedef uint8_t rust_bool;
19+
1720
struct rust_env {
1821
size_t num_sched_threads;
1922
size_t min_stack_size;
2023
size_t max_stack_size;
2124
char* logspec;
22-
bool detailed_leaks;
25+
rust_bool detailed_leaks;
2326
char* rust_seed;
24-
bool poison_on_free;
27+
rust_bool poison_on_free;
2528
int argc;
2629
char **argv;
30+
rust_bool debug_mem;
2731
};
2832

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

src/rt/rustrt.def.in

+1
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,4 @@ rust_dbg_extern_identity_u64
201201
rust_dbg_extern_identity_TwoU64s
202202
rust_dbg_extern_identity_double
203203
rust_dbg_extern_identity_u8
204+
rust_get_rt_env

0 commit comments

Comments
 (0)