diff --git a/byond-extools.dll b/byond-extools.dll index 4910fad01b..bd6b34c48e 100644 Binary files a/byond-extools.dll and b/byond-extools.dll differ diff --git a/code/__DEFINES/qdel.dm b/code/__DEFINES/qdel.dm index 4296e3c2e9..63259774fa 100644 --- a/code/__DEFINES/qdel.dm +++ b/code/__DEFINES/qdel.dm @@ -6,10 +6,17 @@ #define QDEL_HINT_IWILLGC 2 //functionally the same as the above. qdel should assume the object will gc on its own, and not check it. #define QDEL_HINT_HARDDEL 3 //qdel should assume this object won't gc, and queue a hard delete using a hard reference. #define QDEL_HINT_HARDDEL_NOW 4 //qdel should assume this object won't gc, and hard del it post haste. -#define QDEL_HINT_FINDREFERENCE 5 //functionally identical to QDEL_HINT_QUEUE if TESTING is not enabled in _compiler_options.dm. - //if TESTING is enabled, qdel will call this object's find_references() verb. -#define QDEL_HINT_IFFAIL_FINDREFERENCE 6 //Above but only if gc fails. -//defines for the gc_destroyed var + +#ifdef LEGACY_REFERENCE_TRACKING +/** If LEGACY_REFERENCE_TRACKING is enabled, qdel will call this object's find_references() verb. + * + * Functionally identical to QDEL_HINT_QUEUE if GC_FAILURE_HARD_LOOKUP is not enabled in _compiler_options.dm. +*/ +#define QDEL_HINT_FINDREFERENCE 5 +/// Behavior as QDEL_HINT_FINDREFERENCE, but only if the GC fails and a hard delete is forced. +#define QDEL_HINT_IFFAIL_FINDREFERENCE 6 +#endif + #define GC_QUEUE_CHECK 1 #define GC_QUEUE_HARDDELETE 2 diff --git a/code/__DEFINES/vv.dm b/code/__DEFINES/vv.dm index 101330cc8b..fe46fdc710 100644 --- a/code/__DEFINES/vv.dm +++ b/code/__DEFINES/vv.dm @@ -75,6 +75,7 @@ #define VV_HK_MARK "mark" #define VV_HK_ADDCOMPONENT "addcomponent" #define VV_HK_MODIFY_TRAITS "modtraits" +#define VV_HK_VIEW_REFERENCES "viewreferences" // /datum/gas_mixture #define VV_HK_SET_MOLES "set_moles" diff --git a/code/_compile_options.dm b/code/_compile_options.dm index 9a36c626ba..0e0bd4ffaa 100644 --- a/code/_compile_options.dm +++ b/code/_compile_options.dm @@ -11,15 +11,28 @@ #ifdef TESTING #define DATUMVAR_DEBUGGING_MODE -//#define GC_FAILURE_HARD_LOOKUP //makes paths that fail to GC call find_references before del'ing. - //implies FIND_REF_NO_CHECK_TICK +/* +* Enables extools-powered reference tracking system, letting you see what is referencing objects that refuse to hard delete. +* +* * Requires TESTING to be defined to work. +*/ +//#define REFERENCE_TRACKING -//#define FIND_REF_NO_CHECK_TICK //Sets world.loop_checks to false and prevents find references from sleeping +///Method of tracking references without using extools. Slower, kept to avoid over-reliance on extools. +//#define LEGACY_REFERENCE_TRACKING +#ifdef LEGACY_REFERENCE_TRACKING +///Use the legacy reference on things hard deleting by default. +//#define GC_FAILURE_HARD_LOOKUP +#ifdef GC_FAILURE_HARD_LOOKUP +#define FIND_REF_NO_CHECK_TICK +#endif //ifdef GC_FAILURE_HARD_LOOKUP + +#endif //ifdef LEGACY_REFERENCE_TRACKING //#define VISUALIZE_ACTIVE_TURFS //Highlights atmos active turfs in green -#endif +#endif //ifdef TESTING //#define UNIT_TESTS //Enables unit tests via TEST_RUN_PARAMETER #ifndef PRELOAD_RSC //set to: diff --git a/code/controllers/subsystem/garbage.dm b/code/controllers/subsystem/garbage.dm index 8a1c08bc35..b46e22c1fa 100644 --- a/code/controllers/subsystem/garbage.dm +++ b/code/controllers/subsystem/garbage.dm @@ -25,7 +25,7 @@ SUBSYSTEM_DEF(garbage) //Queue var/list/queues - #ifdef TESTING + #ifdef LEGACY_REFERENCE_TRACKING var/list/reference_find_on_fail = list() var/list/reference_find_on_fail_types = list() #endif @@ -134,7 +134,7 @@ SUBSYSTEM_DEF(garbage) ++gcedlasttick ++totalgcs pass_counts[level]++ - #ifdef TESTING + #ifdef LEGACY_REFERENCE_TRACKING reference_find_on_fail -= refID //It's deleted we don't care anymore. #endif if (MC_TICK_CHECK) @@ -145,7 +145,9 @@ SUBSYSTEM_DEF(garbage) fail_counts[level]++ switch (level) if (GC_QUEUE_CHECK) - #ifdef TESTING + #ifdef REFERENCE_TRACKING + D.find_references() + #elif defined(LEGACY_REFERENCE_TRACKING) if(reference_find_on_fail[refID]) D.find_references() #ifdef GC_FAILURE_HARD_LOOKUP @@ -156,7 +158,19 @@ SUBSYSTEM_DEF(garbage) #endif var/type = D.type var/datum/qdel_item/I = items[type] + #ifdef TESTING + log_world("## TESTING: GC: -- \ref[D] | [type] was unable to be GC'd --") + for(var/c in GLOB.admins) //Using testing() here would fill the logs with ADMIN_VV garbage + var/client/admin = c + if(!check_rights_for(admin, R_ADMIN)) + continue + to_chat(admin, "## TESTING: GC: -- [ADMIN_VV(D)] | [type] was unable to be GC'd --") testing("GC: -- \ref[src] | [type] was unable to be GC'd --") + #endif + #ifdef REFERENCE_TRACKING + GLOB.deletion_failures += D //It should no longer be bothered by the GC, manual deletion only. + continue + #endif I.failures++ if (GC_QUEUE_HARDDELETE) HardDelete(D) @@ -181,7 +195,7 @@ SUBSYSTEM_DEF(garbage) var/gctime = world.time var/refid = "\ref[D]" -#ifdef TESTING +#ifdef LEGACY_REFERENCE_TRACKING if(reference_find_on_fail_types[D.type]) reference_find_on_fail["\ref[D]"] = TRUE #endif @@ -193,7 +207,7 @@ SUBSYSTEM_DEF(garbage) queue[refid] = gctime -#ifdef TESTING +#ifdef LEGACY_REFERENCE_TRACKING /datum/controller/subsystem/garbage/proc/add_type_to_findref(type) if(!ispath(type)) return "NOT A VAILD PATH" @@ -260,12 +274,6 @@ SUBSYSTEM_DEF(garbage) /datum/qdel_item/New(mytype) name = "[mytype]" -#ifdef TESTING -/proc/qdel_and_find_ref_if_fail(datum/D, force = FALSE) - SSgarbage.reference_find_on_fail["\ref[D]"] = TRUE - qdel(D, force) -#endif - // Should be treated as a replacement for the 'del' keyword. // Datums passed to this will be given a chance to clean up references to allow the GC to collect them. /proc/qdel(datum/D, force=FALSE, ...) @@ -319,16 +327,13 @@ SUBSYSTEM_DEF(garbage) SSgarbage.Queue(D, GC_QUEUE_HARDDELETE) if (QDEL_HINT_HARDDEL_NOW) //qdel should assume this object won't gc, and hard del it post haste. SSgarbage.HardDelete(D) - if (QDEL_HINT_FINDREFERENCE)//qdel will, if TESTING is enabled, display all references to this object, then queue the object for deletion. + #ifdef LEGACY_REFERENCE_TRACKING + if (QDEL_HINT_FINDREFERENCE) //qdel will, if LEGACY_REFERENCE_TRACKING is enabled, display all references to this object, then queue the object for deletion. SSgarbage.Queue(D) - #ifdef TESTING - D.find_references() - #endif if (QDEL_HINT_IFFAIL_FINDREFERENCE) SSgarbage.Queue(D) - #ifdef TESTING SSgarbage.reference_find_on_fail["\ref[D]"] = TRUE - #endif + #endif else #ifdef TESTING if(!I.no_hint) @@ -339,119 +344,6 @@ SUBSYSTEM_DEF(garbage) else if(D.gc_destroyed == GC_CURRENTLY_BEING_QDELETED) CRASH("[D.type] destroy proc was called multiple times, likely due to a qdel loop in the Destroy logic") -#ifdef TESTING - -/datum/verb/find_refs() - set category = "Debug" - set name = "Find References" - set src in world - - find_references(FALSE) - -/datum/proc/find_references(skip_alert) - running_find_references = type - if(usr && usr.client) - if(usr.client.running_find_references) - testing("CANCELLED search for references to a [usr.client.running_find_references].") - usr.client.running_find_references = null - running_find_references = null - //restart the garbage collector - SSgarbage.can_fire = 1 - SSgarbage.next_fire = world.time + world.tick_lag - return - - if(!skip_alert) - if(alert("Running this will lock everything up for about 5 minutes. Would you like to begin the search?", "Find References", "Yes", "No") == "No") - running_find_references = null - return - - //this keeps the garbage collector from failing to collect objects being searched for in here - SSgarbage.can_fire = 0 - - if(usr && usr.client) - usr.client.running_find_references = type - - testing("Beginning search for references to a [type].") - last_find_references = world.time - - DoSearchVar(GLOB) //globals - for(var/datum/thing in world) //atoms (don't beleive it's lies) - DoSearchVar(thing, "World -> [thing]") - - for (var/datum/thing) //datums - DoSearchVar(thing, "World -> [thing]") - - for (var/client/thing) //clients - DoSearchVar(thing, "World -> [thing]") - - testing("Completed search for references to a [type].") - if(usr && usr.client) - usr.client.running_find_references = null - running_find_references = null - - //restart the garbage collector - SSgarbage.can_fire = 1 - SSgarbage.next_fire = world.time + world.tick_lag - -/datum/verb/qdel_then_find_references() - set category = "Debug" - set name = "qdel() then Find References" - set src in world - - qdel(src, TRUE) //Force. - if(!running_find_references) - find_references(TRUE) - -/datum/verb/qdel_then_if_fail_find_references() - set category = "Debug" - set name = "qdel() then Find References if GC failure" - set src in world - - qdel_and_find_ref_if_fail(src, TRUE) - -/datum/proc/DoSearchVar(X, Xname, recursive_limit = 64) - if(usr && usr.client && !usr.client.running_find_references) - return - if (!recursive_limit) - return - - if(istype(X, /datum)) - var/datum/D = X - if(D.last_find_references == last_find_references) - return - - D.last_find_references = last_find_references - var/list/L = D.vars - - for(var/varname in L) - if (varname == "vars") - continue - var/variable = L[varname] - - if(variable == src) - testing("Found [src.type] \ref[src] in [D.type]'s [varname] var. [Xname]") - - else if(islist(variable)) - DoSearchVar(variable, "[Xname] -> list", recursive_limit-1) - - else if(islist(X)) - var/normal = IS_NORMAL_LIST(X) - for(var/I in X) - if (I == src) - testing("Found [src.type] \ref[src] in list [Xname].") - - else if (I && !isnum(I) && normal && X[I] == src) - testing("Found [src.type] \ref[src] in list [Xname]\[[I]\]") - - else if (islist(I)) - DoSearchVar(I, "[Xname] -> list", recursive_limit-1) - -#ifndef FIND_REF_NO_CHECK_TICK - CHECK_TICK -#endif - -#endif - #ifdef TESTING /proc/writeDatumCount() var/list/datums = list() diff --git a/code/datums/datumvars.dm b/code/datums/datumvars.dm index 348f9e6778..a91549ab4c 100644 --- a/code/datums/datumvars.dm +++ b/code/datums/datumvars.dm @@ -31,6 +31,9 @@ VV_DROPDOWN_OPTION(VV_HK_EXPOSE, "Show VV To Player") VV_DROPDOWN_OPTION(VV_HK_ADDCOMPONENT, "Add Component/Element") VV_DROPDOWN_OPTION(VV_HK_MODIFY_TRAITS, "Modify Traits") + #ifdef REFERENCE_TRACKING + VV_DROPDOWN_OPTION(VV_HK_VIEW_REFERENCES, "View References") + #endif //This proc is only called if everything topic-wise is verified. The only verifications that should happen here is things like permission checks! //href_list is a reference, modifying it in these procs WILL change the rest of the proc in topic.dm of admin/view_variables! diff --git a/code/game/world.dm b/code/game/world.dm index 55333fb3e6..a342200b3d 100644 --- a/code/game/world.dm +++ b/code/game/world.dm @@ -12,6 +12,9 @@ GLOBAL_LIST(topic_status_cache) if (fexists(EXTOOLS)) call(EXTOOLS, "maptick_initialize")() enable_debugger() +#ifdef REFERENCE_TRACKING + enable_reference_tracking() +#endif world.Profile(PROFILE_START) diff --git a/code/modules/admin/admin_verbs.dm b/code/modules/admin/admin_verbs.dm index 718f9d4246..e9abb7db87 100644 --- a/code/modules/admin/admin_verbs.dm +++ b/code/modules/admin/admin_verbs.dm @@ -171,7 +171,11 @@ GLOBAL_LIST_INIT(admin_verbs_debug, world.AVerbsDebug()) /client/proc/cmd_display_overlay_log, /client/proc/reload_configuration, /datum/admins/proc/create_or_modify_area, - /client/proc/generate_wikichem_list //DO NOT PRESS UNLESS YOU WANT SUPERLAG +#ifdef REFERENCE_TRACKING + /datum/admins/proc/view_refs, + /datum/admins/proc/view_del_failures, +#endif + /client/proc/generate_wikichem_list, //DO NOT PRESS UNLESS YOU WANT SUPERLAG ) GLOBAL_PROTECT(admin_verbs_debug) GLOBAL_LIST_INIT(admin_verbs_possess, list(/proc/possess, /proc/release)) diff --git a/code/modules/admin/view_variables/reference_tracking.dm b/code/modules/admin/view_variables/reference_tracking.dm new file mode 100644 index 0000000000..70aac2f107 --- /dev/null +++ b/code/modules/admin/view_variables/reference_tracking.dm @@ -0,0 +1,225 @@ +#ifdef REFERENCE_TRACKING + +GLOBAL_LIST_EMPTY(deletion_failures) + +/world/proc/enable_reference_tracking() + if (fexists(EXTOOLS)) + call(EXTOOLS, "ref_tracking_initialize")() + +/proc/get_back_references(datum/D) + CRASH("/proc/get_back_references not hooked by extools, reference tracking will not function!") + +/proc/get_forward_references(datum/D) + CRASH("/proc/get_forward_references not hooked by extools, reference tracking will not function!") + +/proc/clear_references(datum/D) + return + +/datum/admins/proc/view_refs(atom/D in world) //it actually supports datums as well but byond no likey + set category = "Debug" + set name = "View References" + + if(!check_rights(R_DEBUG) || !D) + return + + var/list/backrefs = get_back_references(D) + if(isnull(backrefs)) + var/datum/browser/popup = new(usr, "ref_view", "
Error
") + popup.set_content("Reference tracking not enabled") + popup.open(FALSE) + return + + var/list/frontrefs = get_forward_references(D) + var/list/dat = list() + dat += "

References of \ref[D] - [D]


\[Refresh\]
" + dat += "

Back references - these things hold references to this object.

" + dat += "" + dat += "" + for(var/ref in backrefs) + var/datum/backreference = ref + if(isnull(backreference)) + dat += "" + if(istype(backreference)) + dat += "" + else if(islist(backreference)) + dat += "" + else + dat += "" + dat += "
RefTypeVariable NameFollow
GC'd Reference
[REF(backreference)][backreference.type][backrefs[backreference]]\[Follow\]
[REF(backreference)]list[backrefs[backreference]]\[Follow\]
Weird reference type. Add more debugging checks.

" + dat += "

Forward references - this object is referencing those things.

" + dat += "" + dat += "" + for(var/ref in frontrefs) + var/datum/backreference = frontrefs[ref] + dat += "" + dat += "
Variable nameRefTypeFollow
[ref][REF(backreference)][backreference.type]\[Follow\]

" + dat = dat.Join() + + var/datum/browser/popup = new(usr, "ref_view", "
References of \ref[D]
") + popup.set_content(dat) + popup.open(FALSE) + + +/datum/admins/proc/view_del_failures() + set category = "Debug" + set name = "View Deletion Failures" + + if(!check_rights(R_DEBUG)) + return + + var/list/dat = list("") + for(var/t in GLOB.deletion_failures) + if(isnull(t)) + dat += "" + continue + var/datum/thing = t + dat += "" + dat += "
GC'd Reference | Clear Nulls
\ref[thing] | [thing.type][thing.gc_destroyed ? " (destroyed)" : ""] [ADMIN_VV(thing)]

" + dat = dat.Join() + + var/datum/browser/popup = new(usr, "del_failures", "
Deletion Failures
") + popup.set_content(dat) + popup.open(FALSE) + + +/datum/proc/find_references() + testing("Beginning search for references to a [type].") + var/list/backrefs = get_back_references(src) + for(var/ref in backrefs) + if(isnull(ref)) + log_world("## TESTING: Datum reference found, but gone now.") + continue + if(islist(ref)) + log_world("## TESTING: Found [type] \ref[src] in list.") + continue + var/datum/datum_ref = ref + if(!istype(datum_ref)) + log_world("## TESTING: Found [type] \ref[src] in unknown type reference: [datum_ref].") + return + log_world("## TESTING: Found [type] \ref[src] in [datum_ref.type][datum_ref.gc_destroyed ? " (destroyed)" : ""]") + message_admins("Found [type] \ref[src] [ADMIN_VV(src)] in [datum_ref.type][datum_ref.gc_destroyed ? " (destroyed)" : ""] [ADMIN_VV(datum_ref)]") + testing("Completed search for references to a [type].") + +#endif + +#ifdef LEGACY_REFERENCE_TRACKING + +/datum/verb/legacy_find_refs() + set category = "Debug" + set name = "Find References" + set src in world + + find_references(FALSE) + + +/datum/proc/find_references_legacy(skip_alert) + running_find_references = type + if(usr?.client) + if(usr.client.running_find_references) + testing("CANCELLED search for references to a [usr.client.running_find_references].") + usr.client.running_find_references = null + running_find_references = null + //restart the garbage collector + SSgarbage.can_fire = TRUE + SSgarbage.next_fire = world.time + world.tick_lag + return + + if(!skip_alert && alert("Running this will lock everything up for about 5 minutes. Would you like to begin the search?", "Find References", "Yes", "No") != "Yes") + running_find_references = null + return + + //this keeps the garbage collector from failing to collect objects being searched for in here + SSgarbage.can_fire = FALSE + + if(usr?.client) + usr.client.running_find_references = type + + testing("Beginning search for references to a [type].") + last_find_references = world.time + + DoSearchVar(GLOB) //globals + for(var/datum/thing in world) //atoms (don't beleive its lies) + DoSearchVar(thing, "World -> [thing]") + + for(var/datum/thing) //datums + DoSearchVar(thing, "World -> [thing]") + + for(var/client/thing) //clients + DoSearchVar(thing, "World -> [thing]") + + testing("Completed search for references to a [type].") + if(usr?.client) + usr.client.running_find_references = null + running_find_references = null + + //restart the garbage collector + SSgarbage.can_fire = TRUE + SSgarbage.next_fire = world.time + world.tick_lag + + +/datum/verb/qdel_then_find_references() + set category = "Debug" + set name = "qdel() then Find References" + set src in world + + qdel(src, TRUE) //force a qdel + if(!running_find_references) + find_references(TRUE) + + +/datum/verb/qdel_then_if_fail_find_references() + set category = "Debug" + set name = "qdel() then Find References if GC failure" + set src in world + + qdel_and_find_ref_if_fail(src, TRUE) + + +/datum/proc/DoSearchVar(potential_container, container_name, recursive_limit = 64) + if(usr?.client && !usr.client.running_find_references) + return + + if(!recursive_limit) + return + + if(istype(potential_container, /datum)) + var/datum/datum_container = potential_container + if(datum_container.last_find_references == last_find_references) + return + + datum_container.last_find_references = last_find_references + var/list/vars_list = datum_container.vars + + for(var/varname in vars_list) + if (varname == "vars") + continue + var/variable = vars_list[varname] + + if(variable == src) + testing("Found [type] \ref[src] in [datum_container.type]'s [varname] var. [container_name]") + + else if(islist(variable)) + DoSearchVar(variable, "[container_name] -> list", recursive_limit - 1) + + else if(islist(potential_container)) + var/normal = IS_NORMAL_LIST(potential_container) + for(var/element_in_list in potential_container) + if(element_in_list == src) + testing("Found [type] \ref[src] in list [container_name].") + + else if(element_in_list && !isnum(element_in_list) && normal && potential_container[element_in_list] == src) + testing("Found [type] \ref[src] in list [container_name]\[[element_in_list]\]") + + else if(islist(element_in_list)) + DoSearchVar(element_in_list, "[container_name] -> list", recursive_limit - 1) + + #ifndef FIND_REF_NO_CHECK_TICK + CHECK_TICK + #endif + + +/proc/qdel_and_find_ref_if_fail(datum/thing_to_del, force = FALSE) + SSgarbage.reference_find_on_fail[REF(thing_to_del)] = TRUE + qdel(thing_to_del, force) + +#endif diff --git a/code/modules/admin/view_variables/topic_basic.dm b/code/modules/admin/view_variables/topic_basic.dm index d6e4c2b944..9ee7103562 100644 --- a/code/modules/admin/view_variables/topic_basic.dm +++ b/code/modules/admin/view_variables/topic_basic.dm @@ -45,6 +45,16 @@ usr.client.admin_delete(target) if (isturf(src)) // show the turf that took its place usr.client.debug_variables(src) + return + #ifdef REFERENCE_TRACKING + if(href_list[VV_HK_VIEW_REFERENCES]) + var/datum/D = locate(href_list[VV_HK_TARGET]) + if(!D) + to_chat(usr, "Unable to locate item.") + return + usr.client.holder.view_refs(target) + return + #endif if(href_list[VV_HK_MARK]) usr.client.mark_datum(target) if(href_list[VV_HK_ADDCOMPONENT]) diff --git a/code/modules/admin/view_variables/view_variables.dm b/code/modules/admin/view_variables/view_variables.dm index abe445589f..a4dff725f7 100644 --- a/code/modules/admin/view_variables/view_variables.dm +++ b/code/modules/admin/view_variables/view_variables.dm @@ -61,6 +61,7 @@ "Set len" = VV_HREF_TARGETREF_INTERNAL(refid, VV_HK_LIST_SET_LENGTH), "Shuffle" = VV_HREF_TARGETREF_INTERNAL(refid, VV_HK_LIST_SHUFFLE), "Show VV To Player" = VV_HREF_TARGETREF_INTERNAL(refid, VV_HK_EXPOSE), + "View References" = VV_HREF_TARGETREF_INTERNAL(refid, VV_HK_VIEW_REFERENCES), "---" ) for(var/i in 1 to length(dropdownoptions)) diff --git a/tgstation.dme b/tgstation.dme index 61a7eec79e..11f8b22fdd 100644 --- a/tgstation.dme +++ b/tgstation.dme @@ -1394,6 +1394,7 @@ #include "code\modules\admin\view_variables\mark_datum.dm" #include "code\modules\admin\view_variables\mass_edit_variables.dm" #include "code\modules\admin\view_variables\modify_variables.dm" +#include "code\modules\admin\view_variables\reference_tracking.dm" #include "code\modules\admin\view_variables\topic.dm" #include "code\modules\admin\view_variables\topic_basic.dm" #include "code\modules\admin\view_variables\topic_list.dm"