Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ function setpropertyonce!(x::Module, f::Symbol, desired, success_order::Symbol=:
return Core.setglobalonce!(x, f, val, success_order, fail_order)
end

julia_debug::Bool = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me a bit nervous to have a compiler time setting that I can easily change by writing a global.


"""
isdebug() -> Bool

Return whether the julia session is running in debug mode.
In debug mode `@assert`s are enabled and `isdebug()` returns `true`.
This is a compile time setting so and hence a useful pattern is the following:

```
@static if isdebug()
# code that is only enabled in debug mode
end
```
"""
isdebug() = julia_debug


convert(::Type{Any}, Core.@nospecialize x) = x
convert(::Type{T}, x::T) where {T} = x
Expand Down Expand Up @@ -649,6 +666,7 @@ end
function __init__()
# Base library init
global _atexit_hooks_finished = false
init_julia_debug()
Filesystem.__postinit__()
reinit_stdio()
Multimedia.reinit_displays() # since Multimedia.displays uses stdout as fallback
Expand Down
6 changes: 5 additions & 1 deletion base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,13 @@ might decide to check anyways, as an aid to debugging if they fail.
The optional message `text` is displayed upon assertion failure.

!!! warning
An assert might be disabled at some optimization levels.
Assertions can be enabled/disabled depending on the `--debug` flag passed to Julia.
Assert should therefore only be used as a debugging tool
and not used for authentication verification (e.g., verifying passwords or checking array bounds).
The code must not rely on the side effects of running `cond` for the correct behavior
of a function.


# Examples
```jldoctest
julia> @assert iseven(3) "3 is an odd number!"
Expand All @@ -218,6 +219,9 @@ julia> @assert isodd(3) "What even are numbers?"
```
"""
macro assert(ex, msgs...)
enable_asserts = !isdefined(Main, :Base) || Main.Base.julia_debug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!isdefined(Main, :Base)

I assume this is the canonical way to check if something is running as a script? possibly should factor that out into an independent iscurrentscript function somewhere

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not checking if it is a script actually, it is making sure that asserts are emitted when e.g. building the compiler (and Base is not defined).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha

enable_asserts || return nothing

msg = isempty(msgs) ? ex : msgs[1]
if isa(msg, AbstractString)
msg = msg # pass-through
Expand Down
3 changes: 2 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1177,4 +1177,5 @@ public
# misc
notnothing,
runtests,
text_colors
text_colors,
isdebug
8 changes: 8 additions & 0 deletions base/initdefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,11 @@ function disable_library_threading()
end
return
end

function init_julia_debug()
opt_julia_debug = Base.JLOptions().julia_debug
# `julia_debug` is disabled everywhere or we are in a package and `--debug="script"` is used (default)
if opt_julia_debug == 0 || (Base.generating_output() && opt_julia_debug == 1)
global julia_debug = false
end
end
42 changes: 24 additions & 18 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1497,31 +1497,36 @@ end


struct CacheFlags
# OOICCDDP - see jl_cache_flags
# OOJICCDDP - see jl_cache_flags
use_pkgimages::Bool
debug_level::Int
check_bounds::Int
inline::Bool
julia_debug::Bool
opt_level::Int
end
function CacheFlags(f::UInt8)

function CacheFlags(f::UInt16)
use_pkgimages = Bool(f & 1)
debug_level = Int((f >> 1) & 3)
check_bounds = Int((f >> 3) & 3)
inline = Bool((f >> 5) & 1)
opt_level = Int((f >> 6) & 3) # define OPT_LEVEL in statiddata_utils
CacheFlags(use_pkgimages, debug_level, check_bounds, inline, opt_level)
end
CacheFlags(f::Int) = CacheFlags(UInt8(f))
CacheFlags() = CacheFlags(ccall(:jl_cache_flags, UInt8, ()))

function _cacheflag_to_uint8(cf::CacheFlags)::UInt8
f = UInt8(0)
f |= cf.use_pkgimages << 0
f |= cf.debug_level << 1
f |= cf.check_bounds << 3
f |= cf.inline << 5
f |= cf.opt_level << 6
julia_debug = Bool((f >> 6) & 1)
opt_level = Int((f >> 7) & 3)
CacheFlags(use_pkgimages, debug_level, check_bounds, inline, julia_debug, opt_level)
end

CacheFlags(f::Int) = CacheFlags(UInt16(f))
CacheFlags() = CacheFlags(ccall(:jl_cache_flags, UInt16, ()))

function _cacheflag_to_uint16(cf::CacheFlags)::UInt16
f = UInt16(0)
f |= UInt16(cf.use_pkgimages) << 0
f |= UInt16(cf.debug_level) << 1
f |= UInt16(cf.check_bounds) << 3
f |= UInt16(cf.inline) << 5
f |= UInt16(cf.julia_debug) << 6
f |= UInt16(cf.opt_level) << 7
return f
end

Expand All @@ -1530,6 +1535,7 @@ function show(io::IO, cf::CacheFlags)
print(io, ", debug_level = ", cf.debug_level)
print(io, ", check_bounds = ", cf.check_bounds)
print(io, ", inline = ", cf.inline)
print(io, ", julia_debug = ", cf.julia_debug)
print(io, ", opt_level = ", cf.opt_level)
end

Expand Down Expand Up @@ -2948,7 +2954,7 @@ end


function parse_cache_header(f::IO, cachefile::AbstractString)
flags = read(f, UInt8)
flags = read(f, UInt16)
modules = Vector{Pair{PkgId, UInt64}}()
while true
n = read(f, Int32)
Expand Down Expand Up @@ -3404,10 +3410,10 @@ end
if isempty(modules)
return true # ignore empty file
end
if @ccall(jl_match_cache_flags(_cacheflag_to_uint8(requested_flags)::UInt8, actual_flags::UInt8)::UInt8) == 0
if @ccall(jl_match_cache_flags(_cacheflag_to_uint16(requested_flags)::UInt16, actual_flags::UInt16)::UInt16) == 0
@debug """
Rejecting cache file $cachefile for $modkey since the flags are mismatched
requested flags: $(requested_flags) [$(_cacheflag_to_uint8(requested_flags))]
requested flags: $(requested_flags) [$(_cacheflag_to_uint16(requested_flags))]
cache file: $(CacheFlags(actual_flags)) [$actual_flags]
"""
record_reason(reasons, "mismatched flags")
Expand Down
1 change: 1 addition & 0 deletions base/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct JLOptions
tracked_path::Ptr{UInt8}
opt_level::Int8
opt_level_min::Int8
julia_debug::Int8
debug_level::Int8
check_bounds::Int8
depwarn::Int8
Expand Down
6 changes: 4 additions & 2 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ See also [`print`](@ref), [`println`](@ref), [`show`](@ref).

Return a julia command similar to the one of the running process.
Propagates any of the `--cpu-target`, `--sysimage`, `--compile`, `--sysimage-native-code`,
`--compiled-modules`, `--pkgimages`, `--inline`, `--check-bounds`, `--optimize`, `--min-optlevel`, `-g`,
`--code-coverage`, `--track-allocation`, `--color`, `--startup-file`, and `--depwarn`
`--compiled-modules`, `--pkgimages`, `--inline`, `--check-bounds`, `--optimize`, `--min-optlevel`, `--debug`,
`-g`, `--code-coverage`, `--track-allocation`, `--color`, `--startup-file`, and `--depwarn`
command line arguments that are not at their default values.

Among others, `--math-mode`, `--warn-overwrite`, and `--trace-compile` are notably not propagated currently.
Expand Down Expand Up @@ -212,6 +212,8 @@ function julia_cmd(julia=joinpath(Sys.BINDIR, julia_exename()); cpu_target::Unio
opts.use_pkgimages == 2 && push!(addflags, "--pkgimages=existing")
opts.opt_level == 2 || push!(addflags, "-O$(opts.opt_level)")
opts.opt_level_min == 0 || push!(addflags, "--min-optlevel=$(opts.opt_level_min)")
opts.julia_debug == 0 && push!(addflags, "--debug=no")
opts.julia_debug == 2 && push!(addflags, "--debug=yes")
push!(addflags, "-g$(opts.debug_level)")
if opts.code_coverage != 0
# Forward the code-coverage flag only if applicable (if the filename is pid-dependent)
Expand Down
6 changes: 6 additions & 0 deletions doc/man/julia.1
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ Set the optimization level (level 3 if `-O` is used without a level)
--min-optlevel={0*,1,2,3}
Set a lower bound on the optimization level

.TP
--debug={no|script*|yes}
Control if the `@assert` macro and `isdebug` is `true` nowhere, only in scripts (not packages)\n
or everywhere, respectively


.TP
-g {0,1*,2}
Set the level of debug info generation (level 2 if `-g` is used without a level)
Expand Down
4 changes: 2 additions & 2 deletions pkgimage.mk
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ $$(BUILDDIR)/stdlib/$1.release.image: export JULIA_CPU_TARGET=$(JULIA_CPU_TARGET
$$(BUILDDIR)/stdlib/$1.debug.image: export JULIA_CPU_TARGET=$(JULIA_CPU_TARGET)
ifneq ($(filter $(1),$(INDEPENDENT_STDLIBS)),)
$$(BUILDDIR)/stdlib/$1.release.image: $$($1_SRCS) $$(addsuffix .release.image,$$(addprefix $$(BUILDDIR)/stdlib/,$2)) $(build_private_libdir)/sys.$(SHLIB_EXT)
@$$(call PRINT_JULIA, $$(call spawn,$$(JULIA_EXECUTABLE)) --startup-file=no --check-bounds=yes -e 'Base.compilecache(Base.identify_package("$1"))')
@$$(call PRINT_JULIA, $$(call spawn,$$(JULIA_EXECUTABLE)) --startup-file=no --check-bounds=yes --debug=yes -e 'Base.compilecache(Base.identify_package("$1"))')
@$$(call PRINT_JULIA, $$(call spawn,$$(JULIA_EXECUTABLE)) --startup-file=no -e 'Base.compilecache(Base.identify_package("$1"))')
touch $$@
$$(BUILDDIR)/stdlib/$1.debug.image: $$($1_SRCS) $$(addsuffix .debug.image,$$(addprefix $$(BUILDDIR)/stdlib/,$2)) $(build_private_libdir)/sys-debug.$(SHLIB_EXT)
@$$(call PRINT_JULIA, $$(call spawn,$$(JULIA_EXECUTABLE)) --startup-file=no --check-bounds=yes -e 'Base.compilecache(Base.identify_package("$1"))')
@$$(call PRINT_JULIA, $$(call spawn,$$(JULIA_EXECUTABLE)) --startup-file=no --check-bounds=yes --debug=yes -e 'Base.compilecache(Base.identify_package("$1"))')
@$$(call PRINT_JULIA, $$(call spawn,$$(JULIA_EXECUTABLE)) --startup-file=no -e 'Base.compilecache(Base.identify_package("$1"))')
touch $$@
endif
Expand Down
38 changes: 31 additions & 7 deletions src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ JL_DLLEXPORT void jl_init_options(void)
NULL, // tracked_path
2, // opt_level
0, // opt_level_min
JL_OPTIONS_JULIA_DEBUG_SCRIPT, // julia_debug
#ifdef JL_DEBUG_BUILD
2, // debug_level [debug build]
#else
Expand Down Expand Up @@ -113,12 +114,14 @@ static const char opts[] =
" --compiled-modules={yes*|no|existing|strict}\n"
" Enable or disable incremental precompilation of modules\n"
" --pkgimages={yes*|no|existing}\n"
" Enable or disable usage of native code caching in the form of pkgimages ($)\n\n"
" Enable or disable usage of native code caching in the form of pkgimages ($)\n"
"\n"

// actions
" -e, --eval <expr> Evaluate <expr>\n"
" -E, --print <expr> Evaluate <expr> and display the result\n"
" -L, --load <file> Load <file> immediately on all processors\n\n"
" -L, --load <file> Load <file> immediately on all processors\n"
"\n"

// parallel options
" -t, --threads {auto|N[,auto|M]}\n"
Expand All @@ -135,25 +138,30 @@ static const char opts[] =
" N is set to half of the number of compute threads and M is set to 0 if unspecified.\n"
" -p, --procs {N|auto} Integer value N launches N additional local worker processes\n"
" \"auto\" launches as many workers as the number of local CPU threads (logical cores)\n"
" --machine-file <file> Run processes on hosts listed in <file>\n\n"
" --machine-file <file> Run processes on hosts listed in <file>\n"
"\n"

// interactive options
" -i, --interactive Interactive mode; REPL runs and `isinteractive()` is true\n"
" -q, --quiet Quiet startup: no banner, suppress REPL warnings\n"
" --banner={yes|no|short|auto*}\n"
" Enable or disable startup banner\n"
" --color={yes|no|auto*} Enable or disable color text\n"
" --history-file={yes*|no} Load or save history\n\n"
" --history-file={yes*|no} Load or save history\n"
"\n"

// error and warning options
" --depwarn={yes|no*|error} Enable or disable syntax and method deprecation warnings (`error` turns warnings into errors)\n"
" --warn-overwrite={yes|no*} Enable or disable method overwrite warnings\n"
" --warn-scope={yes*|no} Enable or disable warning for ambiguous top-level scope\n\n"
" --warn-scope={yes*|no} Enable or disable warning for ambiguous top-level scope\n"
"\n"

// code generation options
" -C, --cpu-target <target> Limit usage of CPU features up to <target>; set to `help` to see the available options\n"
" -O, --optimize={0,1,2*,3} Set the optimization level (level 3 if `-O` is used without a level) ($)\n"
" --min-optlevel={0*,1,2,3} Set a lower bound on the optimization level\n"
" --debug={no|script*|yes} Control if the `@assert` macro and `isdebug` is `true` nowhere, only in scripts (not packages)\n"
" or everywhere, respectively\n"
#ifdef JL_DEBUG_BUILD
" -g, --debug-info=[{0,1,2*}] Set the level of debug info generation in the julia-debug build ($)\n"
#else
Expand All @@ -165,6 +173,7 @@ static const char opts[] =
#ifdef USE_POLLY
" --polly={yes*|no} Enable or disable the polyhedral optimizer Polly (overrides @polly declaration)\n"
#endif
"\n"

// instrumentation options
" --code-coverage[={none*|user|all}]\n"
Expand All @@ -183,6 +192,8 @@ static const char opts[] =
" Count bytes but only in files that fall under the given file path/directory.\n"
" The `@` prefix is required to select this option. A `@` with no path will track the\n"
" current directory.\n"
"\n"

" --bug-report=KIND Launch a bug report session. It can be used to start a REPL, run a script, or evaluate\n"
" expressions. It first tries to use BugReporting.jl installed in current environment and\n"
" fallbacks to the latest compatible BugReporting.jl if not. For more information, see\n"
Expand Down Expand Up @@ -259,9 +270,9 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
opt_strip_ir,
opt_heap_size_hint,
opt_gc_threads,
opt_permalloc_pkgimg
opt_permalloc_pkgimg,
};
static const char* const shortopts = "+vhqH:e:E:L:J:C:it:p:O:g:";
static const char* const shortopts = "+vhqHd:e:E:L:J:C:it:p:O:g:";
static const struct option longopts[] = {
// exposed command line options
// NOTE: This set of required arguments need to be kept in sync
Expand Down Expand Up @@ -295,6 +306,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
{ "track-allocation",optional_argument, 0, opt_track_allocation },
{ "optimize", optional_argument, 0, 'O' },
{ "min-optlevel", optional_argument, 0, opt_optlevel_min },
{ "debug", required_argument, 0, 'd' },
{ "debug-info", optional_argument, 0, 'g' },
{ "check-bounds", required_argument, 0, opt_check_bounds },
{ "output-bc", required_argument, 0, opt_output_bc },
Expand Down Expand Up @@ -469,6 +481,18 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
else
jl_errorf("julia: invalid argument to --compiled-modules={yes|no|existing|strict} (%s)", optarg);
break;
case 'd':
if (!strcmp(optarg,"yes"))
jl_options.julia_debug = JL_OPTIONS_JULIA_DEBUG_YES;
else if (!strcmp(optarg,"no"))
jl_options.julia_debug = JL_OPTIONS_JULIA_DEBUG_NO;
else if (!strcmp(optarg,"script")) {
jl_options.julia_debug = JL_OPTIONS_JULIA_DEBUG_SCRIPT;
printf("opt: %d", jl_options.julia_debug);
}
else
jl_errorf("julia: invalid argument to --debug={yes|script|no} (%s)", optarg);
break;
case opt_pkgimages:
if (!strcmp(optarg,"yes"))
jl_options.use_pkgimages = JL_OPTIONS_USE_PKGIMAGES_YES;
Expand Down
1 change: 1 addition & 0 deletions src/jloptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct {
const char *tracked_path;
int8_t opt_level;
int8_t opt_level_min;
int8_t julia_debug;
int8_t debug_level;
int8_t check_bounds;
int8_t depwarn;
Expand Down
4 changes: 4 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,10 @@ JL_DLLEXPORT int jl_generating_output(void) JL_NOTSAFEPOINT;
#define JL_OPTIONS_USE_PKGIMAGES_YES 1
#define JL_OPTIONS_USE_PKGIMAGES_NO 0

#define JL_OPTIONS_JULIA_DEBUG_YES 2
#define JL_OPTIONS_JULIA_DEBUG_SCRIPT 1
#define JL_OPTIONS_JULIA_DEBUG_NO 0

// Version information
#include <julia_version.h> // Generated file

Expand Down
8 changes: 4 additions & 4 deletions src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -2898,7 +2898,7 @@ static void jl_write_header_for_incremental(ios_t *f, jl_array_t *worklist, jl_a
jl_precompile_toplevel_module = (jl_module_t*)jl_array_ptr_ref(worklist, jl_array_len(worklist)-1);

*checksumpos = write_header(f, 0);
write_uint8(f, jl_cache_flags());
write_uint16(f, jl_cache_flags());
// write description of contents (name, uuid, buildid)
write_worklist_for_header(f, worklist);
// Determine unique (module, abspath, fsize, hash, mtime) dependencies for the files defining modules in the worklist
Expand Down Expand Up @@ -2957,7 +2957,7 @@ JL_DLLEXPORT void jl_create_system_image(void **_native_data, jl_array_t *workli
jl_write_header_for_incremental(f, worklist, mod_array, udeps, srctextpos, &checksumpos);
if (emit_split) {
checksumpos_ff = write_header(ff, 1);
write_uint8(ff, jl_cache_flags());
write_uint16(ff, jl_cache_flags());
write_mod_list(ff, mod_array);
}
else {
Expand Down Expand Up @@ -3569,9 +3569,9 @@ static jl_value_t *jl_validate_cache_file(ios_t *f, jl_array_t *depmods, uint64_
return jl_get_exceptionf(jl_errorexception_type,
"Precompile file header verification checks failed.");
}
uint8_t flags = read_uint8(f);
uint16_t flags = read_uint16(f);
if (pkgimage && !jl_match_cache_flags_current(flags)) {
return jl_get_exceptionf(jl_errorexception_type, "Pkgimage flags mismatch");
return jl_get_exceptionf(jl_errorexception_type, "Pkgimage flags mismatch, current:%d, got:%d", jl_cache_flags(), flags);
}
if (!pkgimage) {
// skip past the worklist
Expand Down
Loading