aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pman/src/pman_relay.erl
blob: 289765492f71dd233ae0e6b9f1de1bb026b1c526 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
%%----------------------------------------------------------------------
%% Purpose : Interface function to  relay calls (esp. trace calls) 
%%           to processes on other  nodes. Some of the calls
%%           are conditionally relayed.
%%----------------------------------------------------------------------

-module(pman_relay).

%%-compile(export_all).
-export([start/1,
	 ok_to_trace/1,
	 trac/3]).


-include("assert.hrl").

%% --------------------------------------------------------------
%%                  DISTRIBUTION
%% --------------------------------------------------------------
%% (???) Process dictionary alert!!!
%%
%% Since we are not allowed to do erlang:trace/3 on remote
%% processe we create a help process at the remote node to
%% do the job for us 
%% ---------------------------------------------------------------

start(P) when is_pid(P), node(P)/=node() ->
    
    %% Remote supervision, relaying necessary
    
    put(relay, spawn_link(node(P), pman_relay_server, init, [self()]));


start(_) ->
    
    %% Local supervision, no relaying
    
    ignore.




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ok_to_trace/1 - Tests wheter we can actually start tracing 
%%    a process.
%%
%% Arguments:
%%   Pid		Pid of the process to trace (on local or remote node)
%%
%% Returns
%%   true		If it is OK to trace the process
%%   false		If the process is already traced, or some other
%%			condition prevents it from being traced.

ok_to_trace(Pid) when node(Pid) == node()->

    %% Local trace, no relaying

    case catch erlang:trace(Pid, false, [send]) of
	1 ->
	    true;
	_Otherwise ->
	    false
    end;
ok_to_trace(Pid) ->

    %% Remote trace, relaying necessary

    PidRelay =  get(relay),
    PidRelay ! {ok_to_trace, self(), Pid},
    receive
	{ok_to_trace, PidRelay} ->
	    true;
	{not_ok_to_trace, PidRelay} ->
	    false;
	_Otherwise ->
	    ?ALWAYS_ASSERT("Unexpected message from relay process")
    after
	5000 ->
	    false
    end.


    
	     
    

%% ---------------------------------------------------------------
%% Possibly send a request to do tracing to a remote node.
%% ---------------------------------------------------------------

trac(Pid, How, Flag) when node(Pid) == node() ->

    %% Local trace, no relaying necessary


    case catch erlang:trace(Pid, How, Flag) of
	1 -> ok;
	_ -> pman_win:format("** Illegal trace request ** \n", [])
    end;
	
trac(Pid, How, Flag) ->



    %% Remote trace, relaying necessary

    get(relay) ! {self(), erlang, trace, [Pid, How, Flag]}.