This is the mail archive of the
systemtap@sourceware.org
mailing list for the systemtap project.
[PATCH 1/1] Add environment variable fetching for current process
- From: "Lars R. Damerow" <lars at pixar dot com>
- To: systemtap at sources dot redhat dot com
- Cc: "Lars R. Damerow" <lars at pixar dot com>
- Date: Wed, 2 Jun 2010 18:08:15 -0700
- Subject: [PATCH 1/1] Add environment variable fetching for current process
- References: <1275527295-4052-1-git-send-email-lars@pixar.com>
* tapset/context.stp: Add env_var.
* testsuite/buildok/context_test.stp: Add test for new function.
---
tapset/context.stp | 46 ++++++++++++++++++++++++++++++++
testsuite/buildok/context-embedded.stp | 1 +
2 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/tapset/context.stp b/tapset/context.stp
index 15fa649..c1ed506 100644
--- a/tapset/context.stp
+++ b/tapset/context.stp
@@ -403,3 +403,49 @@ function cmdline_str:string()
{
return cmdline_args(0, -1, " ");
}
+
+/**
+ * sfunction env_var - Fetch environment variable from current process
+ *
+ * @name: Name of the environment variable to fetch
+ *
+ * Description: Returns the contents of the specified environment value
+ * for the current process. If the variable isn't set an empty string
+ * is returned.
+ */
+function env_var:string(name:string)
+{
+ if (name == "")
+ return ""
+
+ env_value = "";
+ mm = @cast(task_current(), "task_struct", "kernel<linux/sched.h>")->mm;
+ if (mm)
+ {
+ env_start = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->env_start;
+ env_end = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->env_end;
+ if (env_start != 0 && env_end != 0)
+ {
+ len = env_end - env_start;
+ cur = user_string2(env_start, "");
+ env_name = tokenize(cur, "=");
+ while (env_name != name && len > 0)
+ {
+ env_len = strlen(cur);
+ env_start += env_len + 1;
+ len -= env_len + 1;
+ if (len > 0)
+ {
+ cur = user_string2(env_start, "");
+ env_name = tokenize(cur, "=");
+ }
+ else
+ env_name = "";
+ }
+
+ if (len > 0)
+ env_value = tokenize("", "=");
+ }
+ }
+ return env_value;
+}
diff --git a/testsuite/buildok/context-embedded.stp b/testsuite/buildok/context-embedded.stp
index e4463af..491eebc 100755
--- a/testsuite/buildok/context-embedded.stp
+++ b/testsuite/buildok/context-embedded.stp
@@ -24,4 +24,5 @@ probe begin {
log(cmdline_args(1, 1, "bar"))
log(cmdline_arg(0))
log(cmdline_str())
+ log(env_var("TERM"))
}
--
1.7.0.1