(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){(function (){
"use strict";

var e, t = this && this.__classPrivateFieldGet || function(e, t, s, n) {
  if ("a" === s && !n) throw new TypeError("Private accessor was defined without a getter");
  if ("function" == typeof t ? e !== t || !n : !t.has(e)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  return "m" === s ? n : "a" === s ? n.call(e) : n ? n.value : t.get(e);
}, s = this && this.__classPrivateFieldSet || function(e, t, s, n, r) {
  if ("m" === n) throw new TypeError("Private method is not writable");
  if ("a" === n && !r) throw new TypeError("Private accessor was defined without a setter");
  if ("function" == typeof t ? e !== t || !r : !t.has(e)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  return "a" === n ? r.call(e, s) : r ? r.value = s : t.set(e, s), s;
};

const n = 1e3;

class r {
  constructor() {
    this.handlers = new Map, this.nativeTargets = new Set, this.stagedPlanRequest = null, 
    this.stackDepth = new Map, this.traceState = {}, this.nextId = 1, this.started = Date.now(), 
    this.pendingEvents = [], this.flushTimer = null, this.cachedModuleResolver = null, 
    this.cachedObjcResolver = null, this.cachedSwiftResolver = null, this.onTraceError = ({id: e, name: t, message: s}) => {
      send({
        type: "agent:warning",
        id: e,
        message: `Skipping "${t}": ${s}`
      });
    }, this.flush = () => {
      if (null !== this.flushTimer && (clearTimeout(this.flushTimer), this.flushTimer = null), 
      0 === this.pendingEvents.length) return;
      const e = this.pendingEvents;
      this.pendingEvents = [], send({
        type: "events:add",
        events: e
      });
    };
  }
  init(e, t, s, n) {
    const r = global;
    r.stage = e, r.parameters = t, r.state = this.traceState, r.defineHandler = e => e;
    for (const e of s) try {
      (0, eval)(e.source);
    } catch (t) {
      throw new Error(`unable to load ${e.filename}: ${t.stack}`);
    }
    return this.start(n).catch((e => {
      send({
        type: "agent:error",
        message: e.message
      });
    })), {
      id: Process.id,
      platform: Process.platform,
      arch: Process.arch,
      pointer_size: Process.pointerSize,
      page_size: Process.pageSize,
      main_module: Process.mainModule
    };
  }
  dispose() {
    this.flush();
  }
  updateHandlerCode(e, t, s) {
    const n = this.handlers.get(e);
    if (void 0 === n) throw new Error("invalid target ID");
    if (3 === n.length) {
      const r = this.parseFunctionHandler(s, e, t, this.onTraceError);
      n[0] = r[0], n[1] = r[1];
    } else {
      const r = this.parseInstructionHandler(s, e, t, this.onTraceError);
      n[0] = r[0];
    }
  }
  updateHandlerConfig(e, t) {
    const s = this.handlers.get(e);
    if (void 0 === s) throw new Error("invalid target ID");
    s[2] = t;
  }
  async stageTargets(e) {
    const t = await this.createPlan(e);
    this.stagedPlanRequest = t, await t.ready;
    const {plan: s} = t, n = [];
    let r = 1;
    for (const [e, t, a] of s.native.values()) n.push([ r, t, a ]), r++;
    r = -1;
    for (const e of s.java) for (const [t, s] of e.classes.entries()) for (const e of s.methods.values()) n.push([ r, t, e ]), 
    r--;
    return n;
  }
  async commitTargets(e) {
    const t = this.stagedPlanRequest;
    this.stagedPlanRequest = null;
    let {plan: s} = t;
    null !== e && (s = this.cropStagedPlan(s, e));
    const n = [], r = e => {
      n.push(e);
    }, a = await this.traceNativeTargets(s.native, r);
    let o = [];
    return 0 !== s.java.length && (o = await new Promise(((e, t) => {
      Java.perform((() => {
        this.traceJavaTargets(s.java, r).then(e, t);
      }));
    }))), {
      ids: [ ...a, ...o ],
      errors: n
    };
  }
  readMemory(e, t) {
    try {
      return ptr(e).readVolatile(t);
    } catch (e) {
      return null;
    }
  }
  resolveAddresses(e) {
    let t = null;
    return e.map(ptr).map(DebugSymbol.fromAddress).map((e => {
      if (null === e.name) {
        null === t && (t = new ModuleMap);
        const s = t.find(e.address);
        if (null !== s) return `${s.name}!${e.address.sub(s.base)}`;
      }
      return e;
    })).map((e => e.toString()));
  }
  cropStagedPlan(e, t) {
    let s;
    if (t < 0) {
      s = -1;
      for (const n of e.java) for (const [e, r] of n.classes.entries()) for (const [a, o] of r.methods.entries()) {
        if (s === t) {
          const t = {
            methods: new Map([ [ a, o ] ])
          }, s = {
            loader: n.loader,
            classes: new Map([ [ e, t ] ])
          }, r = new b;
          return r.java.push(s), r;
        }
        s--;
      }
    } else {
      s = 1;
      for (const [n, r] of e.native.entries()) {
        if (s === t) {
          const e = new b;
          return e.native.set(n, r), e;
        }
        s++;
      }
    }
    throw new Error("invalid staged item ID");
  }
  async start(e) {
    const t = await this.createPlan(e, (async e => {
      await this.traceJavaTargets(e.java, this.onTraceError);
    }));
    await this.traceNativeTargets(t.plan.native, this.onTraceError), send({
      type: "agent:initialized"
    }), t.ready.then((() => {
      send({
        type: "agent:started",
        count: this.handlers.size
      });
    }));
  }
  async createPlan(e, t = async () => {}) {
    const s = new b, n = [];
    for (const [t, r, a] of e) switch (r) {
     case "module":
      "include" === t ? this.includeModule(a, s) : this.excludeModule(a, s);
      break;

     case "function":
      "include" === t ? this.includeFunction(a, s) : this.excludeFunction(a, s);
      break;

     case "relative-function":
      "include" === t && this.includeRelativeFunction(a, s);
      break;

     case "absolute-instruction":
      "include" === t && this.includeAbsoluteInstruction(ptr(a), s);
      break;

     case "imports":
      "include" === t && this.includeImports(a, s);
      break;

     case "objc-method":
      "include" === t ? this.includeObjCMethod(a, s) : this.excludeObjCMethod(a, s);
      break;

     case "swift-func":
      "include" === t ? this.includeSwiftFunc(a, s) : this.excludeSwiftFunc(a, s);
      break;

     case "java-method":
      n.push([ t, a ]);
      break;

     case "debug-symbol":
      "include" === t && this.includeDebugSymbol(a, s);
    }
    for (const e of s.native.keys()) this.nativeTargets.has(e) && s.native.delete(e);
    let r, a = !0;
    if (n.length > 0) {
      if (!Java.available) throw new Error("Java runtime is not available");
      r = new Promise(((e, r) => {
        Java.perform((async () => {
          a = !1;
          try {
            for (const [e, t] of n) "include" === e ? this.includeJavaMethod(t, s) : this.excludeJavaMethod(t, s);
            await t(s), e();
          } catch (e) {
            r(e);
          }
        }));
      }));
    } else r = Promise.resolve();
    return a || await r, {
      plan: s,
      ready: r
    };
  }
  async traceNativeTargets(e, t) {
    const s = new Map, n = new Map, r = new Map, a = new Map;
    for (const [t, [o, i, c]] of e.entries()) {
      let e;
      switch (o) {
       case "insn":
        e = s;
        break;

       case "c":
        e = n;
        break;

       case "objc":
        e = r;
        break;

       case "swift":
        e = a;
      }
      let l = e.get(i);
      void 0 === l && (l = [], e.set(i, l)), l.push([ c, ptr(t) ]);
    }
    const [o, i, c] = await Promise.all([ this.traceNativeEntries("insn", s, t), this.traceNativeEntries("c", n, t), this.traceNativeEntries("objc", r, t), this.traceNativeEntries("swift", a, t) ]);
    return [ ...o, ...i, ...c ];
  }
  async traceNativeEntries(e, t, s) {
    if (0 === t.size) return [];
    const n = this.nextId, r = [], o = {
      type: "handlers:get",
      flavor: e,
      baseId: n,
      scopes: r
    };
    for (const [e, s] of t.entries()) r.push({
      name: e,
      members: s.map((e => e[0])),
      addresses: s.map((e => e[1].toString()))
    }), this.nextId += s.length;
    const {scripts: i} = await a(o), c = [];
    let l = 0;
    const d = "insn" === e;
    for (const e of t.values()) for (const [t, r] of e) {
      const e = n + l, a = "string" == typeof t ? t : t[1], o = d ? this.parseInstructionHandler(i[l], e, a, s) : this.parseFunctionHandler(i[l], e, a, s);
      this.handlers.set(e, o), this.nativeTargets.add(r.toString());
      try {
        Interceptor.attach(r, d ? this.makeNativeInstructionListener(e, o) : this.makeNativeFunctionListener(e, o));
      } catch (t) {
        s({
          id: e,
          name: a,
          message: t.message
        });
      }
      c.push(e), l++;
    }
    return c;
  }
  async traceJavaTargets(e, t) {
    const s = this.nextId, n = [], r = {
      type: "handlers:get",
      flavor: "java",
      baseId: s,
      scopes: n
    };
    for (const t of e) for (const [e, {methods: s}] of t.classes.entries()) {
      const t = e.split("."), r = t[t.length - 1], a = Array.from(s.keys()).map((e => [ e, `${r}.${e}` ]));
      n.push({
        name: e,
        members: a
      }), this.nextId += a.length;
    }
    const {scripts: o} = await a(r);
    return new Promise((n => {
      Java.perform((() => {
        const r = [];
        let a = 0;
        for (const n of e) {
          const e = Java.ClassFactory.get(n.loader);
          for (const [i, {methods: c}] of n.classes.entries()) {
            const n = e.use(i);
            for (const [e, i] of c.entries()) {
              const c = s + a, l = this.parseFunctionHandler(o[a], c, i, t);
              this.handlers.set(c, l);
              const d = n[e];
              for (const e of d.overloads) e.implementation = this.makeJavaMethodWrapper(c, e, l);
              r.push(c), a++;
            }
          }
        }
        n(r);
      }));
    }));
  }
  makeNativeFunctionListener(e, t) {
    const s = this;
    return {
      onEnter(n) {
        const [r, a, o] = t;
        s.invokeNativeHandler(e, r, o, this, n, ">");
      },
      onLeave(n) {
        const [r, a, o] = t;
        s.invokeNativeHandler(e, a, o, this, n, "<");
      }
    };
  }
  makeNativeInstructionListener(e, t) {
    const s = this;
    return function(n) {
      const [r, a] = t;
      s.invokeNativeHandler(e, r, a, this, n, "|");
    };
  }
  makeJavaMethodWrapper(e, t, s) {
    const n = this;
    return function(...r) {
      return n.handleJavaInvocation(e, t, s, this, r);
    };
  }
  handleJavaInvocation(e, t, s, n, r) {
    const [a, o, i] = s;
    this.invokeJavaHandler(e, a, i, n, r, ">");
    const c = t.apply(n, r), l = this.invokeJavaHandler(e, o, i, n, c, "<");
    return void 0 !== l ? l : c;
  }
  invokeNativeHandler(e, t, s, n, r, a) {
    const o = n.threadId, i = this.updateDepth(o, a);
    if (s.muted) return;
    const c = Date.now() - this.started, l = n.returnAddress.toString(), d = s.capture_backtraces ? Thread.backtrace(n.context).map((e => e.toString())) : null;
    t.call(n, ((...t) => {
      this.emit([ e, c, o, i, l, d, t.join(" ") ]);
    }), r, this.traceState);
  }
  invokeJavaHandler(e, t, s, n, r, a) {
    const o = Process.getCurrentThreadId(), i = this.updateDepth(o, a);
    if (s.muted) return;
    const c = Date.now() - this.started, l = (...t) => {
      this.emit([ e, c, o, i, null, null, t.join(" ") ]);
    };
    try {
      return t.call(n, l, r, this.traceState);
    } catch (e) {
      if (void 0 !== e.$h) throw e;
      Script.nextTick((() => {
        throw e;
      }));
    }
  }
  updateDepth(e, t) {
    const s = this.stackDepth;
    let n = s.get(e) ?? 0;
    return ">" === t ? s.set(e, n + 1) : "<" === t && (n--, 0 !== n ? s.set(e, n) : s.delete(e)), 
    n;
  }
  parseFunctionHandler(e, t, s, n) {
    try {
      const t = this.parseHandlerScript(s, e);
      return [ t.onEnter ?? w, t.onLeave ?? w, o() ];
    } catch (e) {
      return n({
        id: t,
        name: s,
        message: e.message
      }), [ w, w, o() ];
    }
  }
  parseInstructionHandler(e, t, s, n) {
    try {
      return [ this.parseHandlerScript(s, e), o() ];
    } catch (e) {
      return n({
        id: t,
        name: s,
        message: e.message
      }), [ w, o() ];
    }
  }
  parseHandlerScript(e, t) {
    const s = `/handlers/${e}.js`;
    return Script.evaluate(s, t);
  }
  includeModule(e, t) {
    const {native: s} = t;
    for (const t of this.getModuleResolver().enumerateMatches(`exports:${e}!*`)) s.set(t.address.toString(), c(t));
  }
  excludeModule(e, t) {
    const {native: s} = t;
    for (const t of this.getModuleResolver().enumerateMatches(`exports:${e}!*`)) s.delete(t.address.toString());
  }
  includeFunction(e, t) {
    const s = h(e), {native: n} = t;
    for (const e of this.getModuleResolver().enumerateMatches(`exports:${s.module}!${s.function}`)) n.set(e.address.toString(), c(e));
  }
  excludeFunction(e, t) {
    const s = h(e), {native: n} = t;
    for (const e of this.getModuleResolver().enumerateMatches(`exports:${s.module}!${s.function}`)) n.delete(e.address.toString());
  }
  includeRelativeFunction(e, t) {
    const s = f(e), n = Module.getBaseAddress(s.module).add(s.offset);
    t.native.set(n.toString(), [ "c", s.module, `sub_${s.offset.toString(16)}` ]);
  }
  includeAbsoluteInstruction(e, t) {
    const s = t.modules.find(e);
    null !== s ? t.native.set(e.toString(), [ "insn", s.path, `insn_${e.sub(s.base).toString(16)}` ]) : t.native.set(e.toString(), [ "insn", "", `insn_${e.toString(16)}` ]);
  }
  includeImports(e, t) {
    let s;
    if (null === e) {
      const e = Process.enumerateModules()[0].path;
      s = this.getModuleResolver().enumerateMatches(`imports:${e}!*`);
    } else s = this.getModuleResolver().enumerateMatches(`imports:${e}!*`);
    const {native: n} = t;
    for (const e of s) n.set(e.address.toString(), c(e));
  }
  includeObjCMethod(e, t) {
    const {native: s} = t;
    for (const t of this.getObjcResolver().enumerateMatches(e)) s.set(t.address.toString(), l(t));
  }
  excludeObjCMethod(e, t) {
    const {native: s} = t;
    for (const t of this.getObjcResolver().enumerateMatches(e)) s.delete(t.address.toString());
  }
  includeSwiftFunc(e, t) {
    const {native: s} = t;
    for (const t of this.getSwiftResolver().enumerateMatches(`functions:${e}`)) s.set(t.address.toString(), d(t));
  }
  excludeSwiftFunc(e, t) {
    const {native: s} = t;
    for (const t of this.getSwiftResolver().enumerateMatches(`functions:${e}`)) s.delete(t.address.toString());
  }
  includeJavaMethod(e, t) {
    const s = t.java, n = Java.enumerateMethods(e);
    for (const e of n) {
      const {loader: t} = e, n = g(s, (e => {
        const {loader: s} = e;
        return null !== s && null !== t ? s.equals(t) : s === t;
      }));
      if (void 0 === n) {
        s.push(m(e));
        continue;
      }
      const {classes: r} = n;
      for (const t of e.classes) {
        const {name: e} = t, s = r.get(e);
        if (void 0 === s) {
          r.set(e, p(t));
          continue;
        }
        const {methods: n} = s;
        for (const e of t.methods) {
          const t = v(e), s = n.get(t);
          void 0 === s ? n.set(t, e) : n.set(t, e.length > s.length ? e : s);
        }
      }
    }
  }
  excludeJavaMethod(e, t) {
    const s = t.java, n = Java.enumerateMethods(e);
    for (const e of n) {
      const {loader: t} = e, n = g(s, (e => {
        const {loader: s} = e;
        return null !== s && null !== t ? s.equals(t) : s === t;
      }));
      if (void 0 === n) continue;
      const {classes: r} = n;
      for (const t of e.classes) {
        const {name: e} = t, s = r.get(e);
        if (void 0 === s) continue;
        const {methods: n} = s;
        for (const e of t.methods) {
          const t = v(e);
          n.delete(t);
        }
      }
    }
  }
  includeDebugSymbol(e, t) {
    const {native: s} = t;
    for (const t of DebugSymbol.findFunctionsMatching(e)) s.set(t.toString(), u(t));
  }
  emit(e) {
    this.pendingEvents.push(e), null === this.flushTimer && (this.flushTimer = setTimeout(this.flush, 50));
  }
  getModuleResolver() {
    let e = this.cachedModuleResolver;
    return null === e && (e = new ApiResolver("module"), this.cachedModuleResolver = e), 
    e;
  }
  getObjcResolver() {
    let e = this.cachedObjcResolver;
    if (null === e) {
      try {
        e = new ApiResolver("objc");
      } catch (e) {
        throw new Error("Objective-C runtime is not available");
      }
      this.cachedObjcResolver = e;
    }
    return e;
  }
  getSwiftResolver() {
    let e = this.cachedSwiftResolver;
    if (null === e) {
      try {
        e = new ApiResolver("swift");
      } catch (e) {
        throw new Error("Swift runtime is not available");
      }
      this.cachedSwiftResolver = e;
    }
    return e;
  }
}

async function a(e) {
  const t = [], {type: s, flavor: r, baseId: a} = e, o = e.scopes.slice().map((({name: e, members: t, addresses: s}) => ({
    name: e,
    members: t.slice(),
    addresses: s?.slice()
  })));
  let c = a;
  do {
    const e = [], a = {
      type: s,
      flavor: r,
      baseId: c,
      scopes: e
    };
    let l = 0;
    for (const {name: t, members: s, addresses: r} of o) {
      const a = Math.min(s.length, n - l);
      if (0 === a) break;
      e.push({
        name: t,
        members: s.splice(0, a),
        addresses: r?.splice(0, a)
      }), l += a;
    }
    for (;0 !== o.length && 0 === o[0].members.length; ) o.splice(0, 1);
    send(a);
    const d = await i(`reply:${c}`);
    t.push(...d.scripts), c += l;
  } while (0 !== o.length);
  return {
    scripts: t
  };
}

function o() {
  return {
    muted: !1,
    capture_backtraces: !1
  };
}

function i(e) {
  return new Promise((t => {
    recv(e, (e => {
      t(e);
    }));
  }));
}

function c(e) {
  const [t, s] = e.name.split("!").slice(-2);
  return [ "c", t, s ];
}

function l(e) {
  const {name: t} = e, [s, n] = t.substr(2, t.length - 3).split(" ", 2);
  return [ "objc", s, [ n, t ] ];
}

function d(e) {
  const {name: t} = e, [s, n] = t.split("!", 2);
  return [ "swift", s, n ];
}

function u(e) {
  const t = DebugSymbol.fromAddress(e);
  return [ "c", t.moduleName ?? "", t.name ];
}

function h(e) {
  const t = e.split("!", 2);
  let s, n;
  return 1 === t.length ? (s = "*", n = t[0]) : (s = "" === t[0] ? "*" : t[0], n = "" === t[1] ? "*" : t[1]), 
  {
    module: s,
    function: n
  };
}

function f(e) {
  const t = e.split("!", 2);
  return {
    module: t[0],
    offset: parseInt(t[1], 16)
  };
}

function m(e) {
  return {
    loader: e.loader,
    classes: new Map(e.classes.map((e => [ e.name, p(e) ])))
  };
}

function p(e) {
  return {
    methods: new Map(e.methods.map((e => [ v(e), e ])))
  };
}

function v(e) {
  const t = e.indexOf("(");
  return -1 === t ? e : e.substr(0, t);
}

function g(e, t) {
  for (const s of e) if (t(s)) return s;
}

function w() {}

class b {
  constructor() {
    this.native = new Map, this.java = [], e.set(this, null);
  }
  get modules() {
    let n = t(this, e, "f");
    return null === n && (n = new ModuleMap, s(this, e, n, "f")), n;
  }
}

e = new WeakMap;

const M = new r;

rpc.exports = {
  init: M.init.bind(M),
  dispose: M.dispose.bind(M),
  updateHandlerCode: M.updateHandlerCode.bind(M),
  updateHandlerConfig: M.updateHandlerConfig.bind(M),
  stageTargets: M.stageTargets.bind(M),
  commitTargets: M.commitTargets.bind(M),
  readMemory: M.readMemory.bind(M),
  resolveAddresses: M.resolveAddresses.bind(M)
};

}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})

},{}]},{},[1])
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 
