Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MarkBaggett/ccf8a441f788f6f631f9b5f0e5fa3de9 to your computer and use it in GitHub Desktop.
Save MarkBaggett/ccf8a441f788f6f631f9b5f0e5fa3de9 to your computer and use it in GitHub Desktop.

Revisions

  1. MarkBaggett created this gist May 28, 2022.
    13 changes: 13 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    def get_local_envvars_pid(process_id):
    gdb_script = "set variable $envs = (char **) environ\nset $i = 0\nwhile ($envs[$i] != 0)\nprint $envs[$i++]\nend\nquit\n"
    pathlib.Path("/tmp/getenv.gdb").write_text(gdb_script)
    gdb_command = f"gdb -batch -x /tmp/getenv.gdb -p {process_id}"
    ph = subprocess.Popen(gdb_command.split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out,err = ph.communicate()
    log.debug(f"gdb environment variable output {out} errors {err}")
    try:
    found_items = re.findall(r'\$\d+\s+=\s+[0-9a-fx]+\s+"(\S+)=(.+)"\n', out.decode())
    except:
    log.exception("Unable to decode environment varaibles.")
    return None
    return dict(found_items)