add: additional information to ptrace command

This commit adds more information to the ptrace command to allow better integration with external features.
This commit is contained in:
ThePedroo
2024-08-10 13:43:14 -03:00
parent 88f1b7fdb9
commit 75a004f9d5
6 changed files with 139 additions and 21 deletions

View File

@@ -165,6 +165,60 @@ namespace zygiskd {
info->pid = socket_utils::read_u32(fd);
info->modules = (struct zygote_modules *)malloc(sizeof(struct zygote_modules));
if (info->modules == NULL) {
info->modules->modules_count = 0;
close(fd);
return;
}
info->modules->modules_count = socket_utils::read_usize(fd);
if (info->modules->modules_count == 0) {
info->modules->modules = NULL;
close(fd);
return;
}
info->modules->modules = (char **)malloc(sizeof(char *) * info->modules->modules_count);
if (info->modules->modules == NULL) {
free(info->modules);
info->modules = NULL;
info->modules->modules_count = 0;
close(fd);
return;
}
for (size_t i = 0; i < info->modules->modules_count; i++) {
/* INFO by ThePedroo: Ugly solution to read with std::string existance (temporary) */
std::string name = socket_utils::read_string(fd);
char module_path[PATH_MAX];
snprintf(module_path, sizeof(module_path), "/data/adb/modules/%s/module.prop", name.c_str());
FILE *module_prop = fopen(module_path, "r");
if (module_prop == NULL) {
info->modules->modules[i] = strdup(name.c_str());
} else {
char line[1024];
while (fgets(line, sizeof(line), module_prop) != NULL) {
if (strncmp(line, "name=", 5) == 0) {
info->modules->modules[i] = strndup(line + 5, strlen(line) - 6);
break;
}
}
fclose(module_prop);
}
}
close(fd);
} else info->running = false;
}

View File

@@ -42,6 +42,11 @@ private:
Fd fd_ = -1;
};
struct zygote_modules {
char **modules;
size_t modules_count;
};
enum zygote_root_impl {
ZYGOTE_ROOT_IMPL_NONE,
ZYGOTE_ROOT_IMPL_APATCH,
@@ -50,6 +55,7 @@ enum zygote_root_impl {
};
struct zygote_info {
struct zygote_modules *modules;
enum zygote_root_impl root_impl;
pid_t pid;
bool running;

View File

@@ -82,7 +82,52 @@ int main(int argc, char **argv) {
}
}
printf("Is the daemon running: %s\n", info.running ? "yes" : "no");
#ifdef __LP64__
printf("Daemon64 running: %d\n", status64.daemon_running);
printf("Zygote64 injected: %s\n", status64.zygote_injected ? "yes" : "no");
#else
printf("Daemon32 running: %s\n", status32.daemon_running ? "yes" : "no");
printf("Zygote32 injected: %s\n", status32.zygote_injected ? "yes" : "no");
#endif
switch (tracing_state) {
case TRACING: {
printf("Tracing state: TRACING\n");
break;
}
case STOPPING: {
printf("Tracing state: STOPPING\n");
printf("Stop reason: %s\n", monitor_stop_reason);
break;
}
case STOPPED: {
printf("Tracing state: STOPPED\n");
printf("Stop reason: %s\n", monitor_stop_reason);
break;
}
case EXITING: {
printf("Tracing state: EXITING\n");
break;
}
}
if (info.modules->modules_count != 0) {
printf("Modules: %zu\n", info.modules->modules_count);
for (size_t i = 0; i < info.modules->modules_count; i++) {
printf(" - %s\n", info.modules->modules[i]);
free(info.modules->modules[i]);
}
free(info.modules->modules);
} else {
printf("Modules: N/A\n");
}
return 0;
} else {

View File

@@ -22,14 +22,7 @@
static void updateStatus();
enum TracingState {
TRACING = 1,
STOPPING,
STOPPED,
EXITING
};
static char monitor_stop_reason[32];
char monitor_stop_reason[32];
constexpr char SOCKET_NAME[] = "init_monitor";
@@ -110,20 +103,11 @@ struct EventLoop {
}
};
static TracingState tracing_state = TRACING;
TracingState tracing_state = TRACING;
static char prop_path[PATH_MAX];
struct Status {
bool supported = false;
bool zygote_injected = false;
bool daemon_running = false;
pid_t daemon_pid = -1;
char *daemon_info;
char *daemon_error_info;
};
static Status status64;
static Status status32;
Status status64;
Status status32;
struct SocketHandler : public EventHandler {
int sock_fd_;

View File

@@ -3,6 +3,29 @@
#include <stdbool.h>
extern char monitor_stop_reason[32];
enum TracingState {
TRACING = 1,
STOPPING,
STOPPED,
EXITING
};
extern TracingState tracing_state;
struct Status {
bool supported = false;
bool zygote_injected = false;
bool daemon_running = false;
pid_t daemon_pid = -1;
char *daemon_info;
char *daemon_error_info;
};
extern Status status64;
extern Status status32;
void init_monitor();
bool trace_zygote(int pid);

View File

@@ -283,6 +283,12 @@ fn handle_daemon_action(
let pid = unsafe { libc::getpid() };
stream.write_u32(pid as u32)?;
stream.write_usize(context.modules.len())?;
for module in context.modules.iter() {
stream.write_string(&module.name)?;
}
}
DaemonSocketAction::ReadModules => {
stream.write_usize(context.modules.len())?;