Class Cref

Declare a new class "Cref"

Functions

Cref.__misc.clone (t) Table shallow-copy of in-argument
Cref.__misc.mtbest (t) Best effort meta-table return
Cref.__misc.selmetatable (A, B) Return (a deep-copy if needed) of "the best" meta-table of A and B

With "the best" (or "the better") is meant a mt on either side which is inherited from Cref.

Tables

cref.mt Assisting Meta-table
cref.mt Primary helper-class (meta-table) of Cref for operator overloading and pretty-printing.
cref.spretty Internal helper class (meta-table) for pretty-printing indexed tables of single values of any type.

Methods

cref:version () version
cref:whatis (Probe) whatis

Look-up code reference for a certain probe-ID.

cref:sel ([Instance], filename, funcname, kind[, idx1[, idx2]]) sel

Return a selection of probes in a table based a human-readable specifiers of source-code references.

cref:idx ([s=self], idx1[, idx2]) idx

Return Cref at index, or a subset-range between two indexes

In-argument(s) are numerical indexes in a 1-indexed table of Cref:s with negative values permitted.

cref:cast (elem) cast
cref:new (arg1, funcname, kind[, idx1[, idx2]])

new

Selection or copy/cast CTOR

  • Return a new Cref object of selected probes.
cref:valid_ref (Indexed) Simple detection that it could ONE valid raw source-ref
cref:valid_keyval (Key) Valid c-ref key-value (rtt)
cref:convert (Indexed) Converts to one C-ref key-value from one raw source reference
cref:union (A, B) operator +: Join (union) two Cref selections and return result into a new 3'rd.
cref:subtract (A, B) operator -: Set-difference Cref sets into a new 3'rd Symbolic: A – B ≠ B – A
cref:intersect (A, B) operator %: True intersection of two Cref sets into new 3'rd I.e.
cref:is_strict_subset (A, B) Relational operator <:
cref:is_subset (A, B) Relational operator <=:
cref:is_equal (A, B) Relational operator ==:
cref:files () Return a pretty-printable single-value indexed table of which "function names" exists in a/any set
cref:functions () Return a pretty-printable single-value indexed table of which "function names" exists in a/any set
cref:kinds () Return a pretty-printable single-value indexed table of which "kinds" exists in a/any set
cref:sel_restrict (filename, funcname, kind) Restricted version of sel where arguments are expected to be restricted to be single-valves only

Metamethods

cref:__mtcopy (_s) Get a "deep-copy" of meta-table of self or in-argument

Used for internal deep-copies of objects without loosing object-type, i.e.



Functions

Methods
Cref.__misc.clone (t)
Table shallow-copy of in-argument

Parameters:

  • t
Cref.__misc.mtbest (t)
Best effort meta-table return

Parameters:

  • t
Cref.__misc.selmetatable (A, B)
Return (a deep-copy if needed) of "the best" meta-table of A and B

With "the best" (or "the better") is meant a mt on either side which is inherited from Cref. Of two which are both derived, it doesn't know which one, for which case LHS is used (A)

Deep-copies are returned unless clearly unnecessary or obviously needed

Parameters:

  • A
  • B

Tables

cref.mt
Assisting Meta-table Operators
cref.mt
Primary helper-class (meta-table) of Cref for operator overloading and pretty-printing.

Pretty-print format is compatible with Lua "load()" (or "loadstring()" for Lua <5.2), meaning it can be sent to another Lua VM for it's JIT to convert it into a local variable.

Fields:

  • __name Symbolic
  • __index
  • __tostring
  • l
  • end
  • __add
  • __sub
  • __div (A ∪ B) - (A ∩ B)
  • __idiv
  • __lt
  • __le
  • __eq
cref.spretty
Internal helper class (meta-table) for pretty-printing indexed tables of single values of any type.

Note that a variable of this type (i.e. a class instance) is a plain Lua-table in every sense except for this additional capability. I.e. in scripts this meta-table is not used/impacting unless explicit/implicit calls to "tostring()"/"print()" are made.

For interactive use on Lua >= 5.2 however, expression results are always printed unless muted using ';' as a convenient and very useful help to the user/script-developer.

Pretty-print format is compatible with Lua "load()" (or "loadstring()" for Lua <5.2), meaning it can be sent to another Lua VM for it's JIT to convert it into a local variable without further protocol inventions.

Instances of this sub-class are created as results of the following functions:

Fields:

  • __name
  • __index
  • __tostring
  • v
  • end

See also:

Methods

cref:version ()
version Returns (1) version string

Returns:

    String version

Usage:

    Cref:version()
    -- utrace <0.3.4-8269c16>
cref:whatis (Probe)
whatis

Look-up code reference for a certain probe-ID. Note: not selection (sel) based but ID-specific. Useful after determining which probe is live p_stat like in the example below.

Parameters:

  • Probe Integer -ID (address)

Returns:

  1. String kind What kind of probe is it (T="trace", L="log")
  2. String filename File name
  3. String funcname Function name
  4. Number lineno Line number
  5. Integer Linked Probe-ID (conditional)

Usage:

    -- Enable probes of selection defined by arguments
    Cref:set({Cref:sel("test_main.c","thread_A")}, 1)
    -- 1       94651015758336  T       1       0       0
    -- 2       94651015758400  T       1       0       0
    
    -- Print statuses of selection defined by arguments
    Cref:p_stat("test_main.c")
    -- 1       94376905458432  T       0       0       0
    -- 2       94376905458496  T       0       0       0
    -- 3       94376905458304  T       0       0       0
    -- 4       94376905458368  T       0       0       0
    -- 5       94376905458176  T       1       405     96874004698502
    -- 6       94376905458240  T       1       406     96874009765001
    
    -- Look-up one of the probes from the list above
    Cref:whatis(94376905458240)
    -- T       test_main.c     thread_A        59
cref:sel ([Instance], filename, funcname, kind[, idx1[, idx2]])

sel

Return a selection of probes in a table based a human-readable specifiers of source-code references.

Arguments are Lua regexp used to define a "selection" by category: "file-name", "function-name" or "kind". A "selection" is one or more commonly, a group of probe-ID's having the search (or "selection") criteria in common. All arguments are optional (varargs) and can be omitted from the right (i.e. if from the left is needed, the preceding arguments must be nil)

To compensate for Lua languages regexp implementation lack of "word boundary" matcher and lack of alternative matching (i.e. regexp "or"), each argument can be a table of regexp-strings implementing an "or" match (i.e. alternative matcher). In other words and with set-theory nomenclature: unions of matches. Per sub-selector (i.e. argument selector) as the same applies for all arguments.

This is in other words a alternative to produce bigger sets of broader and narrower match-sets simultaneously instead or making unions and splits of several mach-sets of more explicit match result sets. The latter being quite non-intuitive when more than one argument contains regular expression alternatives.

For all arguments, the following rules apply:

  • No argument or nil matches all probes for that category. No arguments in other words matches all probes in process (the "root-set")
  • Arguments are expected to be of String-type are matched using Lua regular expressions. I.e. strings don't need to be exact matches which enables generalized scripting for variants of same or similar code and where the final probes are selected using Lua index operator of Cref:sub instead.
  • Each argument can be either a single value Lua regexp string, or a table of the same for alternative matches to compensate for Lua regexp lack thereof.

Positional argument order matters. I.e. if any preceding arguments don't need specifying (in this context meaning: "include all of what that arguments specifier stands for" or "category"), it/they still have to be given but with the value nil (see previous explanation of left-to-right argument omission)

Return is set of probes as a result the category matches given as arguments, which is an index table of key-value tables of the following:

ptr      (Integer) Address of probe used as unique ID
kind     (String)  A SINGLE-letter string of "T"=trace-probe,
                   "L"/"VDIWE"=log-probe etc (more letters possible
                   depending on child-classes/back-ends)
filename (String)  File-name
funcname (String)  Function-name
line     (Integer) Line-number

Parameters:

  • Instance Cref with selection (optional)
  • filename String
  • funcname String
  • kind String
  • idx1 Integer Start of sub-selection (optional)
  • idx2 Integer End of sub-selection (optional)

Returns:

  1. Integer Number of matches for selection
  2. Table {ID, kind, filename, funcname, line}

See also:

Usage:

  •  -- Make a trial selection. No or all nil arguments returns all probes
    require('Cref')(loadfile('probes/utrace.lua')())
    -- {
    --   {110300811507680,  "T",  "test_extra_thread.c",  "thread_C",  40},
    --   {110300811507616,  "T",  "test_extra_thread.c",  "thread_C",  41},
    --   {110300811507552,  "T",  "test_extra_thread.c",  "thread_C",  42},
    --   {110300811507488,  "T",  "test_extra_thread.c",  "thread_C",  43},
    --   {110300811507424,  "T",  "test_extra_thread.c",  "thread_C",  44},
    --   {110300811507360,  "T",  "test_extra_thread.c",  "thread_C",  45},
    --   {110300811507296,  "T",  "test_extra_thread.c",  "thread_C",  46},
    --   {110300811507232,  "T",  "test_extra_thread.c",  "thread_C",  47},
    --   {110300811507168,  "T",  "test_extra_thread.c",  "thread_C",  48},
    --   {110300811507104,  "T",  "test_extra_thread.c",  "thread_C",  49},
    --   {110300811506144,  "T",  "test_main.c",  "thread_A",  106},
    --   {110300811506080,  "T",  "test_main.c",  "thread_A",  108},
    --   {110300811506272,  "T",  "test_main.c",  "thread_B",  119},
    --   {110300811506208,  "T",  "test_main.c",  "thread_B",  121},
    --   {110300811506400,  "T",  "test_main.c",  "main",  186},
    --   {110300811506336,  "T",  "test_main.c",  "main",  189}
    -- }
    --
    t, _ = require('Cref')(loadfile('probes/utrace.lua')());
    -- Repeat but this time assign the result, then print the first one
    -- Note that the table returned is a 2D index-table of data-dictionaries
    print(t[1].filename, t[1].funcname, t[1].line)
    -- test_extra_thread.c     thread_C        40
  •  -- Multi alternative-category regexp match
    -- This example demonstrates the intuitive multi-category alternative
    -- matching which would be quite cumbersome by other means
    --
    X = require('Cref')(loadfile('probes/mycalc.lua')());
    X:sel({'sym','sig'},{'sym','sigh'},{'E','D'})
    -- {
    --   {97053892017056,  "E",  "sig.c",  "mycalc_sighndlr",  65},
    --   {97053893159808,  "D",  "sym.c",  "sym_lookup_in",  63},
    --   {97053893159872,  "D",  "sym.c",  "sym_lookup_in",  74},
    --   {97053893159936,  "D",  "sym.c",  "sym_lookup_in",  79},
    --   {97053893160000,  "D",  "sym.c",  "sym_lookup_in",  84},
    --   {97053893160192,  "E",  "sym.c",  "sym_rhs_liberate",  116},
    --   {97053893160256,  "D",  "sym.c",  "sym_rhs_xref",  131},
    --   {97053893160320,  "D",  "sym.c",  "sym_rhs_xref",  139}
    -- }
cref:idx ([s=self], idx1[, idx2])
idx

Return Cref at index, or a subset-range between two indexes

In-argument(s) are numerical indexes in a 1-indexed table of Cref:s with negative values permitted.

If given one argument, this method is identical to the __index-operator referencing it's first dimension. I.e. always a singular leaf entity, or none if OoB.

If given two arguments, the return will be a subset of objects of the object performed on, meaning:

  • From (A) - the first index
  • To (B) - the second index

First and second argument always have the same meaning, indexes "from" -> "to"

Despite similarities with the index operator, there are some important differences.

Differences to the index-operator:

  • Negative indexes are allowed, meaning referencing from the last element instead of from the first.
    • I.e. the actual real index a negative index refers to, depends on the current number of C-references in CRef.
    • Despite this incoherence affecting script-robustness, it's surprisingly useful when developing scripts
    • The value of the indexes always determine order regardless their sign
      • A <= B: Normal
      • A > B: Reversed
  • Ordered entities has no relevance in sub-set theory, but can be used as a side-effect when iterating over a range.
  • The result is always a Cref, which is a table AND an instance. I.e. de-referring a Cref probe-set using this method never looses it's inheritance.
  • In other words:
    • The entity of one is still a CRef, even if it holds only one C-reference.
    • It has all it's abilities meaning if de-referring an instance of a child-class, it has access to it's methods (kike enabling a log for Log() or working with time-counters/establishing probe dependencies for Trace() e.t.c.)

Hint

When mixing signs, it helps picturing a table still as a normal 1-indexed table from 1-m. If any in-index A or B is negative, they're internally transformed to it's corresponding positive: a = m - abs(A)

Advantage:

The advantage of indexed results keeping their type besides set-theory operations, is that children of Cref can operate on the result, i.e. applying their own methods directly without having to create new temporary instances.

Parameters:

  • s Cref Use selection from Cref-instance (default self)
  • idx1 Integer Start index of sub-set
  • idx2 Integer End index of sub-set (optional)

Returns:

    Cref New instance with idx-selection

Usage:

  •  -- Operation on sub-set of full-set with "file" containing "main.c"
    -- Note that .* can be omitted because of Lua search-patterns
    m = Cref('.*main.c')
    A = m:idx(1, 2)
    B = m:idx(4)
    R = m:idx(6, 5) -- reversed
    print(A + B + R)
    -- {
    --   {106297695531040,  "T",  "test_main.c",  "thread_A",  57},
    --   {106297695531104,  "T",  "test_main.c",  "thread_A",  59},
    --   {106297695531232,  "T",  "test_main.c",  "thread_B",  71},
    --   {106297695531360,  "T",  "test_main.c",  "main",  134},
    --   {106297695531296,  "T",  "test_main.c",  "main",  131}
    -- }
  •  -- Class-static usage
    m = Cref('.*main.c')
    D = Cref:idx(m, 2)
    print(D)
    -- {
    --   {110968930517088,  "T",  "test_main.c",  "thread_A",  59}
    -- }
cref:cast (elem)

cast Conditionally upcast if possible and needed

For a selection element

  • Sub-scope it as a sub.table
  • Decorate that table with a Cref meta-table, effectively making into a Cref-instance of 1

Parameters:

  • elem Table A selection element

Returns:

    Cref An upcast Cref object

See also:

Usage:

    A = Cref('.*','thread_A')
    a1 = A[1]
    print(A:cast(a1)
    -- {
    --   {100250159223264,  "T",  "test_main.c",  "thread_A",  57}
    -- })
cref:new (arg1, funcname, kind[, idx1[, idx2]])

new

Selection or copy/cast CTOR

  • Return a new Cref object of selected probes. Identical to sel except that the return-type is a Cref-object for which methods can be applied
  • If 1:st argument is a table, return a copy, optionally with sub-selected selection

Parameters:

  • arg1 String
  • funcname String
  • kind String
  • idx1 Integer Selection start index (optional)
  • idx2 Integer Selection end index (optional)

Returns:

    Cref A Cref object instance

See also:

Usage:

  •  -- Instance of a selection
    a = Cref('test_main.c','thread_A',nil,1)
    b = Cref('test_main.c','thread_A',nil,2)
    print(a + b)
    -- {
    --   {94660412502144,  "T",  "test_main.c",  "thread_A",  56},
    --   {94660412502208,  "T",  "test_main.c",  "thread_A",  59}
    -- }
  •  -- Instance of a sub-selection of selection
    A = Cref('.*main.c', nil, nil, 1, 2)
    print( A )
    -- {
    --   {106297695531040,  "T",  "test_main.c",  "thread_A",  57},
    --   {106297695531104,  "T",  "test_main.c",  "thread_A",  59}
    -- }
  •  -- First probe from all threads matching filenames
    th = Cref('.*' , 'thread.*')
    p  = Cref(th, '.*extra.*'):sub(1)
    print(p)
    -- {
    --   {106297695531808,  "T",  "test_extra_thread.c",  "thread_C",  38}
    -- }
cref:valid_ref (Indexed)
Simple detection that it could ONE valid raw source-ref

Parameters:

  • Indexed Table table of 5 elements

Returns:

    Boolean True is valid
cref:valid_keyval (Key)
Valid c-ref key-value (rtt)

Parameters:

  • Key Table -value table with expected key-names

Returns:

    Boolean True is valid
cref:convert (Indexed)
Converts to one C-ref key-value from one raw source reference

Parameters:

  • Indexed Table table of 5 elements

Returns:

    Table Correcponding key-value
cref:union (A, B)
operator +: Join (union) two Cref selections and return result into a new 3'rd. Union == duplicates from either side, not included in result.

Symbolic: A ∪ B

Parameters:

  • A
  • B
cref:subtract (A, B)
operator -: Set-difference Cref sets into a new 3'rd Symbolic: A – B ≠ B – A

Parameters:

  • A
  • B
cref:intersect (A, B)
operator %: True intersection of two Cref sets into new 3'rd I.e. return what is common to both sets Symbolic: A ∩ B

Parameters:

  • A
  • B
cref:is_strict_subset (A, B)
Relational operator <: Symbolic: A ⊂ B Proper/strict subset LHS has fewer elements than the RHS set

Parameters:

  • A
  • B
cref:is_subset (A, B)
Relational operator <=: Symbolic: A ⊆ B Subset LHS has fewer or equal elements than the RHS set

Parameters:

  • A
  • B
cref:is_equal (A, B)
Relational operator ==: Symbolic: A = B Equality, both sets have the same members

Parameters:

  • A
  • B
cref:files ()
Return a pretty-printable single-value indexed table of which "function names" exists in a/any set

Usage:

    Cref(loadfile('probes/utrace.lua')()):files()
    --{
    --  "test_main.c",
    --  "test_extra_thread.c"
    --}
cref:functions ()
Return a pretty-printable single-value indexed table of which "function names" exists in a/any set

Usage:

    Cref(loadfile('probes/utrace.lua')()):functions()
    --{
    --  "thread_B",
    --  "main",
    --  "thread_C",
    --  "thread_A"
    --}
cref:kinds ()
Return a pretty-printable single-value indexed table of which "kinds" exists in a/any set

Usage:

    Cref(loadfile('probes/utrace.lua')()):kinds()
    --{
    --  "T"
    --}
cref:sel_restrict (filename, funcname, kind)
Restricted version of sel where arguments are expected to be restricted to be single-valves only

Parameters:

  • filename
  • funcname
  • kind

Metamethods

cref:__mtcopy (_s)
Get a "deep-copy" of meta-table of self or in-argument

Used for internal deep-copies of objects without loosing object-type, i.e. when class-children are using inherited methods making deep-copes, internally or as results.

Parameters:

  • _s
generated by LDoc 1.5.0 Last updated 2026-08-30 13:32:16