Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created July 23, 2012 02:16
Show Gist options
  • Select an option

  • Save dcolish/3161691 to your computer and use it in GitHub Desktop.

Select an option

Save dcolish/3161691 to your computer and use it in GitHub Desktop.
forward port some of apple's gdb tricks
Index: gdb/environ.h
===================================================================
RCS file: /cvs/src/src/gdb/environ.h,v
retrieving revision 1.12
diff -u -r1.12 environ.h
--- gdb/environ.h 4 Jan 2012 08:17:01 -0000 1.12
+++ gdb/environ.h 23 Jul 2012 02:16:58 -0000
@@ -47,4 +47,8 @@
extern char **environ_vector (struct gdb_environ *);
+#ifdef __APPLE__
+extern void smuggle_dyld_settings (struct gdb_environ *);
+#endif
+
#endif /* defined (ENVIRON_H) */
Index: gdb/environ.c
===================================================================
RCS file: /cvs/src/src/gdb/environ.c,v
retrieving revision 1.24
diff -u -r1.24 environ.c
--- gdb/environ.c 4 Jan 2012 08:17:01 -0000 1.24
+++ gdb/environ.c 23 Jul 2012 02:16:58 -0000
@@ -87,6 +87,64 @@
}
}
+#ifdef __APPLE__
+/* APPLE LOCAL: gdb has to run setgid on MacOS X and
+ dyld truncates the DYLD_* environment variables on set[ug]id
+ binaries so their environment isn't mucked with by an evil user.
+
+ To work around this, the /usr/bin/gdb shell script copies DYLD_*
+ into GDB_DYLD_*, and in this function we look for DYLD_* env
+ vars that have been truncated and a matching GDB_DYLD_* env var.
+ If this exists, we copy the GDB_DYLD_* value into DYLD_* so that
+ it will take effect when the inferior process is run. */
+
+struct dyld_smuggle_pairs {
+ const char *real_name;
+ const char *smuggled_name;
+};
+
+void
+smuggle_dyld_settings (struct gdb_environ *e)
+{
+ /* The list of DYLD_* names was gleaned from dyld's src/dyld.cpp. */
+
+ struct dyld_smuggle_pairs env_names[] = {
+ {"DYLD_FRAMEWORK_PATH", "GDB_DYLD_FRAMEWORK_PATH"},
+ {"DYLD_FALLBACK_FRAMEWORK_PATH", "GDB_DYLD_FALLBACK_FRAMEWORK_PATH"},
+ {"DYLD_LIBRARY_PATH", "GDB_DYLD_LIBRARY_PATH"},
+ {"DYLD_FALLBACK_LIBRARY_PATH", "GDB_DYLD_FALLBACK_LIBRARY_PATH"},
+ {"DYLD_ROOT_PATH", "GDB_DYLD_ROOT_PATH"},
+ {"DYLD_PATHS_ROOT", "GDB_DYLD_PATHS_ROOT"},
+ {"DYLD_IMAGE_SUFFIX", "GDB_DYLD_IMAGE_SUFFIX"},
+ {"DYLD_INSERT_LIBRARIES", "GDB_DYLD_INSERT_LIBRARIES"},
+ { NULL, NULL } };
+ int i;
+
+ for (i = 0; env_names[i].real_name != NULL; i++)
+ {
+ const char *real_val = get_in_environ (e, env_names[i].real_name);
+ const char *smuggled_val = get_in_environ (e, env_names[i].smuggled_name);
+
+ if (real_val == NULL && smuggled_val == NULL)
+ continue;
+
+ if (smuggled_val == NULL)
+ continue;
+
+ /* Is the value of the DYLD_* env var truncated to ""? */
+ if (real_val != NULL && real_val[0] != '\0')
+ continue;
+
+ /* real_val has a value and it looks legitimate - don't overwrite it
+ with the smuggled version. */
+ if (real_val != NULL)
+ continue;
+
+ set_in_environ (e, env_names[i].real_name, smuggled_val);
+ }
+}
+#endif
+
/* Return the vector of environment E.
This is used to get something to pass to execve. */
Index: gdb/inferior.c
===================================================================
RCS file: /cvs/src/src/gdb/inferior.c,v
retrieving revision 1.36
diff -u -r1.36 inferior.c
--- gdb/inferior.c 12 May 2012 06:35:08 -0000 1.36
+++ gdb/inferior.c 23 Jul 2012 02:16:58 -0000
@@ -136,6 +136,10 @@
inf->environment = make_environ ();
init_environ (inf->environment);
+#ifdef __APPLE__
+ smuggle_dyld_settings(inf->environment);
+#endif
+
inferior_alloc_data (inf);
observer_notify_inferior_added (inf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment