Take the following code: :::::::::::::: distance.c :::::::::::::: static int value; void setdistance(int v) { value = v; } int getdistance() { return value; } :::::::::::::: speed.c :::::::::::::: static int value; void setspeed(int v) { value = v; } int getspeed() { return value; } :::::::::::::: main.c :::::::::::::: #include <stdio.h> void setspeed(int v); int getspeed(); void setdistance(int v); int getdistance(); static print_value (int value) { printf("value: %d\n", value); } static int calculate_value () { return getdistance () / getspeed (); } int main (int argc, char **argv) { int value; setspeed (6); setdistance (60); value = calculate_value (); print_value (value); return 0; } Note that there are various variables "value" in play here. The example above is deliberately setup to show all the pitfalls. But the issue is the same for having a global variable in one CU/file and wanting to use/probe it in another CU. A simple $value will not find it.
See for an extended usage (accessing vars from functions, not probes) PR11096.
commit 179a00c3efb5ceac52a70debb17a91ee0c07722d Author: Mark Wielaard <mjw@redhat.com> Date: Mon Mar 12 13:00:40 2012 +0100 Document new @var construct in NEWS, langref, beginners and stapprobes. commit acf4b3b10e9d70cbf8a0a681ae3c17c9681bdfbe Author: Mark Wielaard <mjw@redhat.com> Date: Sat Mar 10 19:15:35 2012 +0100 global_var_kernel.exp: tz_minuteswest can be negative. commit 3965e105b73e196ec4f9b15c0b192c59bb14a2f8 Author: Mark Wielaard <mjw@redhat.com> Date: Fri Mar 9 15:57:31 2012 +0100 Global vars don't need (cannot use) frame base in location descriptor. And so they also don't need cfa_ops (CFI) to calculate the frame base. commit 3d9381fc814162b914288d45ff52017bf0a83fb4 Author: Mark Wielaard <mjw@redhat.com> Date: Fri Mar 9 15:19:12 2012 +0100 Update verbose log and semantic exception messages for local vs global vars. Global @vars (with @cu/src_name.c) have a fixed scope/CU and don't depend on location/PC. Make that clean in verbose log and semantic error messages. commit 8fa5f2719e4ec115486e69364e110a5048c7dfd7 Author: Mark Wielaard <mjw@redhat.com> Date: Fri Mar 9 00:10:52 2012 +0100 Cannot use @var DWARF variables in tracepoints, warn, but don't crash. commit 49131a6df3a031f0011256cfa33130afc63c001c Author: Mark Wielaard <mjw@redhat.com> Date: Thu Mar 8 23:32:49 2012 +0100 Make sure @var works for process.mark probes. commit 54e9f0623713e7f32c9870baad5a5d5c06c2fd22 Author: Mark Wielaard <mjw@redhat.com> Date: Thu Mar 8 15:20:08 2012 +0100 Select most specific CU srcfile that matches @var("var@src") specification. dwarf_var_expanding_visitor::getcuscope: If there is a perfect match select that CU, otherwise, select the CU srcfile that is most specific (matching path/src file lenght is shortest). commit bfa7e523ab8cd891a2a3f24c1540021665947b5d Author: Mark Wielaard <mjw@redhat.com> Date: Wed Mar 7 19:58:12 2012 +0100 PR13784 Allow defining a diffent CU source in @var. Add cu_name to struct target_symbol. Set it in parse_target_symbol to the string after @ in @var("somevar@some/src/file.c"). Make target_symbol::sym_name() aware of @cu_name postfix (don't return it). Add dwarf_var_expanding_visitor::getcuscope() which figures out the CU DIE to use as scope if target_symbol has cu_name part. Extend global_var.exp test to show new capability. commit cc9001af0a5fa811ddf0e44439c7cddf465db945 Author: Mark Wielaard <mjw@redhat.com> Date: Tue Mar 6 17:34:36 2012 +0100 Introduce @var("varname") as alternative syntax for $varname. Adds a field target_name and a method sym_name() to struct target_symbol to be used by the parser and tapsets translator so $name and @var("name") can be used interchanably. Adds a testcase at_var.exp to test this for various combinations of uprobes, kprobes, parameters, variables, arrays, fields access, pretty printing and taking addresses.