title: Frida - Function to dump/inspect object
body: <p>I'm trying to write a function using Frida that takes a Java object instance as an argument and returns a JS object with all the values and types of the Java object fields, possibly recursively.</p>
  
  <p>For instance, if we have something like:</p>
  
  <pre><code>public class Person {
    private String fullName;
    private int age;
    private List hobbies,
  
    public Person(String fullName, int age, List hobbies) {
      ...
    }
  
    public void hello() {
      ...
    }
  }
  </code></pre>
  
  <p>I'd like to be able (in a Frida JS script) to run something like</p>
  
  <pre><code>Java.use("Person").hello.implementation = function() {
    console.log(JSON.stringify(dumpObject(this, 2)));  // dump recursively 2 levels deep
    this.hello();
  }
  </code></pre>
  
  <p>and see something like:</p>
  
  <pre><code>{
    "instance":"0x1234",
    "type":"Person",
    "fields' [
      {
       "name":"fullName",
       "declaredType":"java.lang.String",
       "actualType":"java.lang.String",
       "value":"John Smith",
      },
      {
       "name":"age",
       "declaredType":"int",
       "actualType":"int",
       "value":25,
      },
      {
       "name":"hobbies",
       "declaredType":"java.util.List",
       "actualType":"java.util.ArrayList",
       "value":{
          "instance":"0x4567",
          "type":"java.util.ArrayList",
          "fields": [
             ... all the fields of this ArrayList instance, 2 levels deep
          ],
       },
      },
    ]
  }
  </code></pre>
  
  <p>I tried several different approaches, but I keep having lots of errors and problems. Before I dig deeper, is anyone aware of some existing implementation that would save me quite a bit of time? I Googled extensively, but could only find scripts that dump class methods and field names, nothing that intelligently looks at values, including corner cases requiring _name access, static members, recursive dumping, etc...</p>
  
  <p>Thanks!</p>