Author: jfrederic.clere(a)jboss.com
Date: 2007-10-17 07:45:00 -0400 (Wed, 17 Oct 2007)
New Revision: 1116
Modified:
trunk/sight/native/os/solaris/module.c
Log:
fork() to collect own process information.
Modified: trunk/sight/native/os/solaris/module.c
===================================================================
--- trunk/sight/native/os/solaris/module.c 2007-10-16 16:55:48 UTC (rev 1115)
+++ trunk/sight/native/os/solaris/module.c 2007-10-17 11:45:00 UTC (rev 1116)
@@ -96,8 +96,8 @@
struct module {
char *name;
- int base;
- int size;
+ long base;
+ long size;
struct module *next;
};
@@ -140,6 +140,7 @@
current->next = apr_palloc(module->pool, sizeof(struct module));
current = current->next;
}
+ module->current = current;
current->next = NULL;
current->name = apr_pstrdup(module->pool, car);
current->base = map->pr_vaddr;
@@ -147,7 +148,48 @@
module->nummodules++;
return 0;
}
+/* Use a child process to read maps */
+static int proc_map_child(void *ptr, const prmap_t *map, const char *car)
+{
+ FILE *file = (FILE *)ptr;
+ fprintf(file, "%s\n", car);
+ fprintf(file, "%u\n", map->pr_vaddr);
+ fprintf(file, "%u\n", map->pr_size);
+ return 0;
+}
+static void proc_child(int fd, int pid)
+{
+ struct ps_prochandle *ph;
+ int ret;
+ FILE *file = fdopen(fd, "w");
+ ph = Pgrab(pid, 0, &ret);
+ if (ph == NULL)
+ return;
+ Pobject_iter(ph, proc_map_child, file);
+ Prelease(ph, 0);
+ return;
+}
+/* read information for the son process */
+static void Read_map_f(int fd, module_enum_t *module)
+{
+ FILE *file = fdopen(fd, "r");
+ char car[128];
+ char buf[128];
+ int addr, pr_size;
+ prmap_t map;
+ for (;;) {
+ if (fscanf(file, "%s", car)<0)
+ break;
+ fscanf(file, "%s", buf);
+ map.pr_vaddr = atoi(buf);
+ fscanf(file, "%s", buf);
+ map.pr_size = atoi(buf);
+ proc_map_f((void *)module, &map, car);
+ }
+}
+#define G_SELF 9
+
SIGHT_EXPORT_DECLARE(jobjectArray, Module, enum0)(SIGHT_STDARGS,
jint pid)
{
@@ -160,6 +202,7 @@
module_enum_t *module;
struct module *current;
jobjectArray mods = NULL;
+ int filedes[2];
UNREFERENCED_O;
if (pid >= 0)
@@ -171,16 +214,31 @@
}
ph = Pgrab(mpid, 0, &ret);
if (ph == NULL) {
- throwOSException(_E, Pgrab_error(ret));
- apr_pool_destroy(pool);
- return NULL;
+ if (ret != G_SELF) {
+ throwOSException(_E, Pgrab_error(ret));
+ apr_pool_destroy(pool);
+ return NULL;
+ } else {
+ /* fork and use a pipe to read self info */
+ pipe(filedes);
+ if (fork() == 0) {
+ close(filedes[0]);
+ proc_child(filedes[1], pid);
+ exit(0);
+ } else {
+ close(filedes[1]);
+ }
+ }
+ } else {
+ filedes[0] = -1;
}
/* Process all entries */
module = apr_palloc(pool, sizeof(module_enum_t));
if (module == NULL) {
throwAprMemoryException(_E, THROW_FMARK, apr_get_os_error());
- Prelease(ph, 0);
+ if (filedes[0] == -1)
+ Prelease(ph, 0);
apr_pool_destroy(pool);
return NULL;
}
@@ -188,23 +246,30 @@
module->nummodules = 0;
module->first_module = NULL;
module->current = NULL;
- Pobject_iter(ph, proc_map_f, module);
- if (ret<0) {
- throwOSException(_E, Pgrab_error(ret));
- apr_pool_destroy(pool);
- return NULL;
+ if (filedes[0] == -1) {
+ Pobject_iter(ph, proc_map_f, module);
+ if (ret<0) {
+ throwOSException(_E, Pgrab_error(ret));
+ Prelease(ph, 0);
+ apr_pool_destroy(pool);
+ return NULL;
+ }
+ /* Release the proc file system */
+ Prelease(ph, 0);
+ } else {
+ /* use pipe to read the information */
+ Read_map_f(filedes[0], module);
+ close(filedes[0]);
}
- /* Release the proc file system */
- Prelease(ph, 0);
-
/* Fill the java objects */
mods = (*_E)->NewObjectArray(_E, module->nummodules, _clazzn.a, NULL);
if (!mods || (*_E)->ExceptionCheck(_E)) {
apr_pool_destroy(pool);
return NULL;
}
- current = module->current;
+ current = module->first_module;
+ printf("%d enum0 %d\n", current, module->nummodules);
for (j = 0; j < module->nummodules; j++) {
jobject m = new_module_class(_E, _O, pid, j);
if (!m || (*_E)->ExceptionCheck(_E)) {
@@ -214,9 +279,10 @@
SET_IFIELD_S(0000, m, current->name);
SET_IFIELD_S(0001, m, current->name); /* should be basename */
SET_IFIELD_J(0002, m, current->base);
- SET_IFIELD_J(0002, m, current->size);
+ SET_IFIELD_J(0003, m, current->size);
(*_E)->SetObjectArrayElement(_E, mods, j, m);
(*_E)->DeleteLocalRef(_E, m);
+ current = current->next;
}
apr_pool_destroy(pool);