Merge pull request #54473 from briansemrau/no-debug-functions-on-thread

This commit is contained in:
Rémi Verschelde 2021-11-08 19:37:48 +01:00 committed by GitHub
commit 236fd3305b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View file

@ -98,6 +98,7 @@
[codeblock] [codeblock]
[{function:bar, line:12, source:res://script.gd}, {function:foo, line:9, source:res://script.gd}, {function:_ready, line:6, source:res://script.gd}] [{function:bar, line:12, source:res://script.gd}, {function:foo, line:9, source:res://script.gd}, {function:_ready, line:6, source:res://script.gd}]
[/codeblock] [/codeblock]
[b]Note:[/b] Not supported for calling from threads. Instead, this will return an empty array.
</description> </description>
</method> </method>
<method name="inst2dict"> <method name="inst2dict">
@ -160,17 +161,24 @@
<method name="print_debug" qualifiers="vararg"> <method name="print_debug" qualifiers="vararg">
<return type="void" /> <return type="void" />
<description> <description>
Like [method @GlobalScope.print], but prints only when used in debug mode. Like [method @GlobalScope.print], but includes the current stack frame when running with the debugger turned on.
Output in the console would look something like this:
[codeblock]
Test print
At: res://test.gd:15:_process()
[/codeblock]
[b]Note:[/b] Not supported for calling from threads. Instead of the stack frame, this will print the thread ID.
</description> </description>
</method> </method>
<method name="print_stack"> <method name="print_stack">
<return type="void" /> <return type="void" />
<description> <description>
Prints a stack track at code location, only works when running with debugger turned on. Prints a stack trace at the current code location. Only works when running with debugger turned on.
Output in the console would look something like this: Output in the console would look something like this:
[codeblock] [codeblock]
Frame 0 - res://test.gd:16 in function '_process' Frame 0 - res://test.gd:16 in function '_process'
[/codeblock] [/codeblock]
[b]Note:[/b] Not supported for calling from threads. Instead of the stack trace, this will print the thread ID.
</description> </description>
</method> </method>
<method name="range" qualifiers="vararg"> <method name="range" qualifiers="vararg">

View file

@ -437,9 +437,13 @@ struct GDScriptUtilityFunctionsDefinitions {
str += p_args[i]->operator String(); str += p_args[i]->operator String();
} }
ScriptLanguage *script = GDScriptLanguage::get_singleton(); if (Thread::get_caller_id() == Thread::get_main_id()) {
if (script->debug_get_stack_level_count() > 0) { ScriptLanguage *script = GDScriptLanguage::get_singleton();
str += "\n At: " + script->debug_get_stack_level_source(0) + ":" + itos(script->debug_get_stack_level_line(0)) + ":" + script->debug_get_stack_level_function(0) + "()"; if (script->debug_get_stack_level_count() > 0) {
str += "\n At: " + script->debug_get_stack_level_source(0) + ":" + itos(script->debug_get_stack_level_line(0)) + ":" + script->debug_get_stack_level_function(0) + "()";
}
} else {
str += "\n At: Cannot retrieve debug info outside the main thread. Thread ID: " + itos(Thread::get_caller_id());
} }
print_line(str); print_line(str);
@ -448,15 +452,24 @@ struct GDScriptUtilityFunctionsDefinitions {
static inline void print_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void print_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(0); VALIDATE_ARG_COUNT(0);
if (Thread::get_caller_id() != Thread::get_main_id()) {
print_line("Cannot retrieve debug info outside the main thread. Thread ID: " + itos(Thread::get_caller_id()));
return;
}
ScriptLanguage *script = GDScriptLanguage::get_singleton(); ScriptLanguage *script = GDScriptLanguage::get_singleton();
for (int i = 0; i < script->debug_get_stack_level_count(); i++) { for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
print_line("Frame " + itos(i) + " - " + script->debug_get_stack_level_source(i) + ":" + itos(script->debug_get_stack_level_line(i)) + " in function '" + script->debug_get_stack_level_function(i) + "'"); print_line("Frame " + itos(i) + " - " + script->debug_get_stack_level_source(i) + ":" + itos(script->debug_get_stack_level_line(i)) + " in function '" + script->debug_get_stack_level_function(i) + "'");
}; };
*r_ret = Variant();
} }
static inline void get_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void get_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(0); VALIDATE_ARG_COUNT(0);
if (Thread::get_caller_id() != Thread::get_main_id()) {
*r_ret = Array();
return;
}
ScriptLanguage *script = GDScriptLanguage::get_singleton(); ScriptLanguage *script = GDScriptLanguage::get_singleton();
Array ret; Array ret;