10 #include <sys/utsname.h>
18 static void env_set(const char *var, const char *val);
19 EAPI int prefix_determine(char *argv0);
22 env_set(const char *var, const char *val)
30 size_t size = strlen(var) + 1 + strlen(val) + 1;
33 snprintf(buf, size, "%s=%s", var, val);
34 if (getenv(var)) putenv(buf);
35 else putenv(strdup(buf));
43 if (getenv(var)) putenv(var);
48 /* local subsystem globals */
49 static Eina_Prefix *pfx = NULL;
51 /* externally accessible functions */
53 prefix_determine(char *argv0)
55 pfx = eina_prefix_new(argv0, prefix_determine,
56 "E", "enlightenment", "AUTHORS",
57 PACKAGE_BIN_DIR, PACKAGE_LIB_DIR,
58 PACKAGE_DATA_DIR, LOCALE_DIR);
68 char buf[4096], tbuf[256 * 1024];
70 int l, fd, children = 0, cret;
72 home = getenv("HOME");
73 if (home) snprintf(buf, sizeof(buf), "%s/.e-precache", home);
74 else snprintf(buf, sizeof(buf), "/tmp/.e-precache");
79 // while (fgets(buf, sizeof(buf), f));
81 while (fgets(buf, sizeof(buf), f))
84 if (l > 0) buf[l - 1] = 0;
87 if (buf[0] == 's') stat(buf + 2, &st);
88 else if (buf[0] == 'o')
90 fd = open(buf + 2, O_RDONLY);
93 while (read(fd, tbuf, 256 * 1024) > 0);
97 else if (buf[0] == 'd')
99 fd = open(buf + 2, O_RDONLY);
102 while (read(fd, tbuf, 256 * 1024) > 0);
125 find_valgrind(char *path, size_t path_len)
127 const char *env = getenv("PATH");
131 const char *p = strchr(env, ':');
134 if (p) p_len = p - env;
135 else p_len = strlen(env);
136 if (p_len <= 0) goto next;
137 else if (p_len + sizeof("/valgrind") >= path_len) goto next;
138 memcpy(path, env, p_len);
139 memcpy(path + p_len, "/valgrind", sizeof("/valgrind"));
140 if (access(path, X_OK | R_OK) == 0) return 1;
150 /* maximum number of arguments added above */
151 #define VALGRIND_MAX_ARGS 10
152 /* bitmask with all supported bits set */
153 #define VALGRIND_MODE_ALL 15
156 valgrind_append(char **dst, int valgrind_gdbserver, int valgrind_mode, int valgrind_tool, char *valgrind_path, const char *valgrind_log)
162 dst[i++] = valgrind_path;
163 switch (valgrind_tool)
165 case 1: dst[i++] = "--tool=massif"; break;
166 case 2: dst[i++] = "--tool=callgrind"; break;
170 if (valgrind_gdbserver) dst[i++] = "--db-attach=yes";
171 if (!valgrind_mode) return 0;
172 dst[i++] = valgrind_path;
173 dst[i++] = "--track-origins=yes";
174 dst[i++] = "--malloc-fill=13"; /* invalid pointer, make it crash */
177 static char logparam[PATH_MAX + sizeof("--log-file=")];
179 snprintf(logparam, sizeof(logparam), "--log-file=%s", valgrind_log);
182 if (valgrind_mode & 2) dst[i++] = "--trace-children=yes";
183 if (valgrind_mode & 4)
185 dst[i++] = "--leak-check=full";
186 dst[i++] = "--leak-resolution=high";
187 dst[i++] = "--track-fds=yes";
189 if (valgrind_mode & 8) dst[i++] = "--show-reachable=yes";
194 copy_args(char **dst, char **src, size_t count)
196 for (; count > 0; count--, dst++, src++) *dst = *src;
200 _env_path_prepend(const char *env, const char *path)
203 int len = 0, len2 = 0;
206 if (p) len = strlen(p);
208 if (p2) len2 = strlen(p2);
211 // path already there at the start. dont prepend. :)
212 if ((!strcmp(p, p2)) ||
214 (!strncmp(p, p2, len2)) &&
218 s = malloc(len + 1 + len2 + 1);
234 _env_path_append(const char *env, const char *path)
237 int len = 0, len2 = 0;
243 if (p2) len2 = strlen(p2);
246 // path already there at the end. dont append. :)
247 if ((!strcmp(p, p2)) ||
249 (!strcmp((p + len - len2), p2)) &&
250 (p[len - len2 - 1] == ':')))
253 s = malloc(len + 1 + len2 + 1);
269 main(int argc, char **argv)
271 int i, do_precache = 0, valgrind_mode = 0;
272 int valgrind_tool = 0;
273 int valgrind_gdbserver = 0;
274 char buf[16384], **args, *p;
275 char valgrind_path[PATH_MAX] = "";
276 const char *valgrind_log = NULL;
277 Eina_Bool really_know = EINA_FALSE;
280 prefix_determine(argv[0]);
282 env_set("E_START", argv[0]);
284 for (i = 1; i < argc; i++)
286 if (!strcmp(argv[i], "-no-precache")) do_precache = 0;
287 else if (!strcmp(argv[i], "-valgrind-gdb")) valgrind_gdbserver = 1;
288 else if (!strncmp(argv[i], "-valgrind", sizeof("-valgrind") - 1))
290 const char *val = argv[i] + sizeof("-valgrind") - 1;
292 if (*val == '\0') valgrind_mode = 1;
293 else if (*val == '-')
296 if (!strncmp(val, "log-file=", sizeof("log-file=") - 1))
298 valgrind_log = val + sizeof("log-file=") - 1;
299 if (*valgrind_log == '\0') valgrind_log = NULL;
302 else if (*val == '=')
305 if (!strcmp(val, "all")) valgrind_mode = VALGRIND_MODE_ALL;
306 else valgrind_mode = atoi(val);
309 printf("Unknown valgrind option: %s\n", argv[i]);
311 else if (!strcmp(argv[i], "-massif")) valgrind_tool = 1;
312 else if (!strcmp(argv[i], "-callgrind")) valgrind_tool = 2;
313 else if ((!strcmp(argv[i], "-h")) ||
314 (!strcmp(argv[i], "-help")) ||
315 (!strcmp(argv[i], "--help")))
321 "\t\tDisable pre-caching of files\n"
322 "\t-valgrind[=MODE]\n"
323 "\t\tRun enlightenment from inside valgrind, mode is OR of:\n"
324 "\t\t 1 = plain valgrind to catch crashes (default)\n"
325 "\t\t 2 = trace children (thumbnailer, efm slaves, ...)\n"
326 "\t\t 4 = check leak\n"
327 "\t\t 8 = show reachable after processes finish.\n"
328 "\t\t all = all of above\n"
330 "\t\tRun enlightenment from inside massif valgrind tool.\n"
332 "\t\tRun enlightenment from inside callgrind valgrind tool.\n"
333 "\t-valgrind-log-file=<FILENAME>\n"
334 "\t\tSave valgrind log to file, see valgrind's --log-file for details.\n"
337 "\tenlightenment %s\n"
338 "for more options.\n",
342 else if (!strcmp(argv[i], "-i-really-know-what-i-am-doing-and-accept-full-responsibility-for-it"))
343 really_know = EINA_TRUE;
348 _env_path_append("PATH", eina_prefix_bin_get(pfx));
349 _env_path_append("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
353 _env_path_prepend("PATH", eina_prefix_bin_get(pfx));
354 _env_path_prepend("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
357 if (valgrind_mode || valgrind_tool)
359 if (!find_valgrind(valgrind_path, sizeof(valgrind_path)))
361 printf("E - valgrind required but no binary found! Ignoring request.\n");
366 printf("E - PID=%i, do_precache=%i, valgrind=%d", getpid(), do_precache, valgrind_mode);
369 printf(" valgrind-command='%s'", valgrind_path);
370 if (valgrind_log) printf(" valgrind-log-file='%s'", valgrind_log);
378 /* sanity checks - if precache might fail - check here first */
379 lib = dlopen("libeina.so", RTLD_GLOBAL | RTLD_LAZY);
380 if (!lib) dlopen("libeina.so.1", RTLD_GLOBAL | RTLD_LAZY);
382 func = dlsym(lib, "eina_init");
383 if (!func) goto done;
385 lib = dlopen("libecore.so", RTLD_GLOBAL | RTLD_LAZY);
386 if (!lib) dlopen("libecore.so.1", RTLD_GLOBAL | RTLD_LAZY);
388 func = dlsym(lib, "ecore_init");
389 if (!func) goto done;
391 lib = dlopen("libecore_file.so", RTLD_GLOBAL | RTLD_LAZY);
392 if (!lib) dlopen("libecore_file.so.1", RTLD_GLOBAL | RTLD_LAZY);
394 func = dlsym(lib, "ecore_file_init");
395 if (!func) goto done;
397 lib = dlopen("libecore_x.so", RTLD_GLOBAL | RTLD_LAZY);
398 if (!lib) dlopen("libecore_x.so.1", RTLD_GLOBAL | RTLD_LAZY);
400 func = dlsym(lib, "ecore_x_init");
401 if (!func) goto done;
403 lib = dlopen("libevas.so", RTLD_GLOBAL | RTLD_LAZY);
404 if (!lib) dlopen("libevas.so.1", RTLD_GLOBAL | RTLD_LAZY);
406 func = dlsym(lib, "evas_init");
407 if (!func) goto done;
409 lib = dlopen("libedje.so", RTLD_GLOBAL | RTLD_LAZY);
410 if (!lib) dlopen("libedje.so.1", RTLD_GLOBAL | RTLD_LAZY);
412 func = dlsym(lib, "edje_init");
413 if (!func) goto done;
415 lib = dlopen("libeet.so", RTLD_GLOBAL | RTLD_LAZY);
416 if (!lib) dlopen("libeet.so.0", RTLD_GLOBAL | RTLD_LAZY);
418 func = dlsym(lib, "eet_init");
419 if (!func) goto done;
421 /* precache SHOULD work */
422 snprintf(buf, sizeof(buf), "%s/enlightenment/preload/e_precache.so",
423 eina_prefix_lib_get(pfx));
424 env_set("LD_PRELOAD", buf);
425 printf("E - PRECACHE GOING NOW...\n");
431 /* mtrack memory tracker support */
437 /* if you have ~/.e-mtrack, then the tracker will be enabled
438 * using the content of this file as the path to the mtrack.so
439 * shared object that is the mtrack preload */
440 snprintf(buf, sizeof(buf), "%s/.e-mtrack", p);
444 if (fgets(buf, sizeof(buf), f))
446 int len = strlen(buf);
447 if ((len > 1) && (buf[len - 1] == '\n'))
452 env_set("LD_PRELOAD", buf);
453 env_set("MTRACK", "track");
454 env_set("E_START_MTRACK", "track");
455 snprintf(buf, sizeof(buf), "%s/.e-mtrack.log", p);
456 env_set("MTRACK_TRACE_FILE", buf);
462 /* try dbus-launch */
463 snprintf(buf, sizeof(buf), "%s/enlightenment", eina_prefix_bin_get(pfx));
465 args = alloca((argc + 2 + VALGRIND_MAX_ARGS) * sizeof(char *));
466 if ((!getenv("DBUS_SESSION_BUS_ADDRESS")) &&
467 (!getenv("DBUS_LAUNCHD_SESSION_BUS_SOCKET")))
469 args[0] = "dbus-launch";
470 args[1] = "--exit-with-session";
472 i = 2 + valgrind_append(args + 2, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
474 copy_args(args + i, argv + 1, argc - 1);
475 args[i + argc - 1] = NULL;
476 execvp("dbus-launch", args);
479 /* dbus-launch failed - run e direct */
480 i = valgrind_append(args, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
482 copy_args(args + i, argv + 1, argc - 1);
483 args[i + argc - 1] = NULL;
484 execv(args[0], args);
486 printf("FAILED TO RUN:\n");
487 printf(" %s\n", buf);