anchor.diff:              "c" -> comm:send_local(Pid, {sort_by, cpuusage});
               "m" -> comm:send_local(Pid, {sort_by, memory});
               "t" -> comm:send_local(Pid, {toggle_exclude_self});
  +            "i" -> top_inspect_pid(Pid);
               "g" -> [garbage_collect(X) || X <- processes()];
               _ -> ok
       end,
       case Quit of
  -        quit -> ok;
  +        quit ->
  +            comm:send_local(Pid, {output_off}),
  +            comm:send_local(Pid, {stop_sampling});
           _ -> top_interactive(Pid)
       end.
   
  +top_inspect_pid(Pid) ->
  +    %% Read pid and start inspect mode
  +    comm:send_local(Pid, {output_off}),
  +    comm:send_local(Pid, {stop_sampling}),
  +    InspectPid = pid_prompt(),
  +    comm:send_local(Pid, {enable_sampling, pid}),
  +    comm:send_local(Pid, {sample_pid, InspectPid}),
  +    comm:send_local(Pid, {enable_output, pid}),
  +    comm:send_local(Pid, {output_pid, InspectPid}),
  +    top_inspect_pid_interactive(Pid, InspectPid),
  +    comm:send_local(Pid, {enable_sampling, all}),
  +    comm:send_local(Pid, {sample_all}),
  +    comm:send_local(Pid, {enable_output, all}),
  +    comm:send_local(Pid, {output_all}).
  +
  +pid_prompt() ->
  +    {ok, [InspectPidString]} = io:fread("pid or name > ", "~s"),
  +    try list_to_pid(InspectPidString)
  +    catch _:_ ->
  +            try
  +                InspectPidAtom = list_to_atom(InspectPidString),
  +                case whereis(InspectPidAtom) of
  +                    undefined ->
  +                        case pid_groups:find_all(InspectPidAtom) of
  +                            [Pid] -> Pid;
  +                            Pids ->
  +                                io:format("Which of the ~ps do you mean?~n",
  +                                          [InspectPidAtom]),
  +                                lists:foldl(
  +                                  fun(X, Acc) ->
  +                                          io:format(
  +                                            " ~p: ~p ~p~n",
  +                                            [Acc, X,
  +                                             pid_groups:group_and_name_of(X)]),
  +                                          1 + Acc end, 1, Pids),
  +                                {ok, [Nth]} = io:fread("num > ", "~d"),
  +                                lists:nth(Nth, Pids)
  +                        end;
  +                    Pid -> Pid
  +                end
  +            catch _:_ ->
  +                    io:format("No such pid found. Please retry.~n"),
  +                    pid_prompt()
  +            end
  +    end.
  +
  +top_inspect_pid_interactive(TopPid, Pid) ->
  +    {ok, [Char]} = io:fread(">", "~c"),
  +    Quit =
  +        case Char of
  +            "q" -> quit;
  +            "e" ->
  +                comm:send_local(TopPid, {output_off}),
  +                Expr = io:parse_erl_exprs('expr>'),
  +                comm:send_local(TopPid, {enable_output, pid}),
  +                comm:send_local(TopPid, {output_pid, Pid}),
  +                case element(1, Expr) of
  +                    ok ->
  +                        spawn(fun() -> erl_eval:exprs(element(2, Expr), erl_eval:new_bindings()) end),
  +                          ok;
  +                    _ -> ok
  +                end;
  +            "c" -> comm:send_local(TopPid, {sort_by, cpuusage});
  +            "g" -> [garbage_collect(X) || X <- processes()];
  +            _ -> ok
  +        end,
  +    case Quit of
  +        quit ->
  +            comm:send_local(TopPid, {output_off}),
  +            comm:send_local(TopPid, {stop_sampling}),
  +            ok;
  +        _ -> top_inspect_pid_interactive(TopPid, Pid)
  +    end.
  +
   usage() ->
  +    io:format(
  +      "~n"
  +      " (q)uit, (e)val expr, (g)arb-coll. (i)nspect a pid"
  +      %% ", exclude (t)op itself" %% undocumented feature for top development
  +      "~n sort by: (c)pu usage (m)emory (p)ids (w)aiting messages~n").
  +
  +usage_inspect() ->
       io:format(
         "~n"
         " (q)uit, (e)val expr, (g)arb-coll."
         %% ", exclude (t)op itself" %% undocumented feature for top development
  -      "~n sort by: (c)pu usage (m)emory (p)ids (w)aiting messages~n").
  +      "~n sort by: (c)pu usage~n").
   
   -spec on(comm:message(), state()) -> state().
  -on({mon_on}, State) ->
  -    set_monitoring(State, true);
  -
  -on({output_on}, State) ->
  -    comm:send_local(self(), {output_data}),
  -    set_output(State, true);
  -
  -on({output_off}, State) ->
  -    comm:send_local(self(), {output_data}),
  -    set_output(State, false);
  -
  -on({sort_by, Type}, State) ->
  -    set_sort_by(State, Type);
  -
  -on({toggle_exclude_self}, State) ->
  -    toggle_exclude_self(State);
  -
  -on({start_mon}, State) when element(2, State) ->
  +
  +on({enable_sampling, Val}, State) -> set_sampling(State, Val);
  +on({enable_output, Val}, State)   -> set_output(State, Val);
  +on({sort_by, Type}, State)        -> set_sort_by(State, Type);
  +on({toggle_exclude_self}, State)  -> toggle_exclude_self(State);
  +on({output_off}, State)           -> set_output(State, false);
  +
  +%% gather information on all local pids for periodic statistical output
  +on({sample_all}, State) when all =:= element(2, State) ->
       T = [ catch({X, erlang:process_info(X, [message_queue_len, status])})
             || X <- erlang:processes(), X =/= exclude_self(State) ],
   
  
positive.code:                      Pid = element(1, X),
                       pdb:delete(Pid, TableName),
                       %% plot data is:
                       Status = try element(2, process_info(Pid, status))
                                catch _:_ -> dead_pid
                                end,
                       {vals_pid(X),
                        Status,
                        vals_mqlen(X)/Count,
                        vals_cpu_usage(X, Count),
                        try element(2, process_info(Pid, memory))
                        catch _Level2:_Reason2 -> 0
                        end}
                   end || X <- R1, is_tuple(X), is_pid(element(1, X)) ],
      SortedPlotData = lists:keysort(sort_by(State), PlotData),
      L = length(SortedPlotData),
  
negative.code: -export([top/0]).
  
  -type state() :: { pdb:tableid(),
                     false | all | pid, %% sampling: no, all or a single pid
                     false | all | pid, %% output: no, all, or a single pid
                     erlang_timestamp(), %% last output
                     non_neg_integer(), %% # of measures
                     non_neg_integer(), %% sort by
                     no_exclude | pid()
                   }.
  
  -spec start_link(list()) -> {ok, pid()}.
  start_link(Group) ->
      gen_component:start_link(?MODULE, fun ?MODULE:on/2, [],
                               [{erlang_register, ?MODULE},
                                {pid_groups_join_as, Group, ?MODULE}]).
  
  -spec init([]) -> state().
  init([]) ->
  