Пример #1
0
  @BIF
  public static EInteger trace(EProc self_proc, EObject arg0, EObject arg1, EObject arg2) {
    EInternalPID pid = arg0.testInternalPID();
    EAtom all = arg0.testAtom();
    EObject how = arg1.testBoolean();
    ESeq flags = arg2.testSeq();

    if ((pid == null && all != am_all) || how == null || flags == null) {
      throw ERT.badarg(arg0, arg1, arg2);
    }

    EInternalPID self = self_proc.self_handle();

    if (all == am_all) {
      ESeq allprocs = EProc.processes();
      int result = allprocs.length();
      global_trace_flags.update(how == ERT.TRUE, flags, self);
      while (!allprocs.isNil()) {
        EProc proc = allprocs.head().testInternalPID().task();
        allprocs = allprocs.tail();

        if (proc.trace_flags != null) {
          proc.trace_flags.update(how == ERT.TRUE, flags, self);
        }
      }

      return ERT.box(result);

    } else {
      EProc proc = pid.task();

      if (!proc.is_alive_dirtyread()) {
        return ESmall.ZERO;
      }

      if (proc.trace_flags == null) {
        proc.trace_flags = global_trace_flags.clone();
      }

      proc.trace_flags.update(how == ERT.TRUE, flags, self);

      return ESmall.ONE;
    }
  }
Пример #2
0
  public static void print_all_stack_traces() {
    System.err.println("== Trace ==");

    Mailbox<EObject> mbox = new Mailbox<>(EProc.process_count());

    ESeq all = EProc.processes();
    for (EObject o : all) {
      EInternalPID pid = (EInternalPID) o;
      pid.task().printStackTrace(mbox);
    }

    while (true) {
      EObject o = mbox.getb(1000);
      if (o == null) break;

      ETuple2 tup = ETuple2.cast(o);
      EHandle handle = tup.elm(1).testHandle();
      ESeq stack = tup.elm(2).testSeq();

      System.err.println("\n == " + handle + " : " + handle.name);
      for (EObject elm : stack) {

        ETuple4 tup4 = ETuple4.cast(elm);

        if (tup4 != null) {
          ESeq args = tup4.elem3.testSeq();
          ESmall arity = tup4.elem3.testSmall();
          if (arity == null && args != null) {
            arity = ERT.box(args.length());
          }

          StringBuffer file_line = new StringBuffer();

          ESeq info = tup4.elem4.testSeq();
          if (info != null) {
            String file = "?";
            int line = -1;
            for (EObject inf : info) {
              ETuple2 t;
              if ((t = ETuple2.cast(inf)) != null) {
                ESmall n;
                EString f;
                if (t.elem1 == ErlangException.am_line && ((n = t.elem2.testSmall()) != null)) {
                  line = n.value;
                } else if (t.elem1 == ErlangException.am_file
                    && ((f = t.elem2.testString()) != null)) {
                  file = f.stringValue();
                }
              }
            }
            if (line != -1) {
              file_line.append('(').append(file).append(':').append(line).append(')');
            }
          }

          String module = tup4.elem1.toString();
          String mfa;
          System.err.print(mfa = make_width(20, module) + ":" + tup4.elem2 + "/" + arity);
          System.err.println(" " + make_width(65 - mfa.length(), file_line.toString()));
        } else {
          System.err.println(elm);
        }
      }
    }
  }