frida-gum icon indicating copy to clipboard operation
frida-gum copied to clipboard

[Enhancement] Add name field to ThreadDetails

Open alvarofe opened this issue 5 years ago • 3 comments

Does it make sense to add another field to ThreadDetails to expose the name of the thread? Might be useful when wanting to only stalk specific threads by their names, leaving alone those for the binder, etc.

I added the below lines to do some stuff and they were useful to track specific threads. I can send a PR if you give the OK

diff --git a/bindings/gumjs/gumdukprocess.c b/bindings/gumjs/gumdukprocess.c
index 9700c88e..52d7c1b7 100644
--- a/bindings/gumjs/gumdukprocess.c
+++ b/bindings/gumjs/gumdukprocess.c
@@ -214,6 +214,13 @@ gum_emit_thread (const GumThreadDetails * details,
       GUM_CPU_CONTEXT_READONLY, scope->core);
   duk_put_prop_string (ctx, -2, "context");
 
+  if (details->name)
+    duk_push_string (ctx, (details->name));
+  else
+    duk_push_string (ctx, "null");
+
+  duk_put_prop_string (ctx, -2, "name");
+
   if (_gum_duk_scope_call_sync (scope, 1))
   {
     if (duk_is_string (ctx, -1))
diff --git a/bindings/gumjs/gumv8process.cpp b/bindings/gumjs/gumv8process.cpp
index bd92f871..dfc27ffe 100644
--- a/bindings/gumjs/gumv8process.cpp
+++ b/bindings/gumjs/gumv8process.cpp
@@ -183,6 +183,17 @@ gum_emit_thread (const GumThreadDetails * details,
       _gum_v8_cpu_context_new_immutable (&details->cpu_context, core);
   _gum_v8_object_set (thread, "context", cpu_context, core);
 
+  if (details->name)
+  {
+    _gum_v8_object_set (thread, "name", _gum_v8_string_new_ascii (isolate,
+          details->name), core);
+  }
+  else
+  {
+    _gum_v8_object_set (thread, "name", _gum_v8_string_new_ascii (isolate,
+          "null"), core);
+  }
+
   auto proceed = mc->OnMatch (thread);
 
   _gum_v8_cpu_context_free_later (
diff --git a/gum/backend-linux/gumprocess-linux.c b/gum/backend-linux/gumprocess-linux.c
index 7a31a109..9d2d930c 100644
--- a/gum/backend-linux/gumprocess-linux.c
+++ b/gum/backend-linux/gumprocess-linux.c
@@ -577,6 +577,7 @@ _gum_process_enumerate_threads (GumFoundThreadFunc func,
   GDir * dir;
   const gchar * name;
   gboolean carry_on = TRUE;
+  gchar * thread_name_path;
 
   dir = g_dir_open ("/proc/self/task", 0, NULL);
   g_assert (dir != NULL);
@@ -588,11 +589,18 @@ _gum_process_enumerate_threads (GumFoundThreadFunc func,
     details.id = atoi (name);
     if (gum_thread_read_state (details.id, &details.state))
     {
+      thread_name_path = g_strdup_printf ("/proc/self/task/%lu/comm", details.id);
+      if (!g_file_get_contents (thread_name_path, (gchar **) &details.name, NULL,  NULL))
+        details.name = NULL;
+      else
+        details.name = g_strchomp (details.name);
+
       if (gum_process_modify_thread (details.id, gum_store_cpu_context,
             &details.cpu_context))
       {
         carry_on = func (&details, user_data);
       }
+      g_free (thread_name_path);
     }
   }
 
diff --git a/gum/gumprocess.h b/gum/gumprocess.h
index 8cfa6592..d003949d 100644
--- a/gum/gumprocess.h
+++ b/gum/gumprocess.h
@@ -51,6 +51,7 @@ struct _GumThreadDetails
   GumThreadId id;
   GumThreadState state;
   GumCpuContext cpu_context;
+  gchar *name;
 };
 
 struct _GumModuleDetails

alvarofe avatar Sep 24 '19 13:09 alvarofe

It would be nice to add this, but I don't like adding features only implemented on one OS, so ideally we should support the main three OS families that we currently support (Linux, macOS, Windows).

oleavr avatar Sep 25 '19 09:09 oleavr

I can take care of linux and macOS, unfortunately about Windows I don't know much about its APIs and internals so it would take more time.

alvarofe avatar Sep 25 '19 14:09 alvarofe

If you do Linux and macOS I can probably take a stab at Windows when we get to that point.

oleavr avatar Sep 26 '19 21:09 oleavr