How to show Erlang call stack? - erlang

I need to debug some module in foreign system. The module has public function foo() - how can I know place (module and function name)
from which foo() given module was called? I mean stack of calls.
I cannot stop system, all work I can do by reload this module (but with some debug info).
-module(given).
-export(foo/0).
foo() ->
%% here is my debug - and
%% i need here(!) known about unknown_module:unknown_foo!
ok.
---
-module(unknown_module).
..
unknown_foo() ->
given:foo(). %% see above

Here's a simple trick:
Trace = try throw(42) catch 42 -> erlang:get_stacktrace() end,
erlang:display(Trace)

This might work:
where_am_i() ->
try throw(a)
catch throw:a:Stacktrace ->
Stacktrace
end.
Except that it doesn't work for tail calls. For example, given these two functions:
foo() ->
where_am_i().
bar() ->
X = where_am_i(),
{ok, X}.
I get these results:
4> foo:foo().
[{foo,where_am_i,0},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_exprs,6},
{shell,eval_loop,3}]
5> foo:bar().
{ok,[{foo,where_am_i,0},
{foo,bar,0},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_exprs,6},
{shell,eval_loop,3}]}
That is, I can only see bar, since foo's call frame has been left already when where_am_i is called.

io:format("~s~n", [element(2, process_info(self(), backtrace))]).
self() can be replaced by any other pid (rpc:pinfo should even work with remote procs). This helps if you cannot even modify the source or beam.

Here is my code for doing this:
format_stack_entry(S) ->
{Module,Fun,Arity,[{file,File},{line,Line}]}=S,
io_lib:format("{~p,~p,~p,[{file,~p},{line,~p]}",[Module,Fun,Arity,File,Line]).
stacktop([Top|_]) ->
Top.
ancestor(N) ->
{_,Stacktrace}=erlang:process_info(self(),current_stacktrace),
ancestor(N+1,Stacktrace).
ancestor(1,S) ->
format_stack_entry(stacktop(S));
ancestor(N,[_|T]) ->
ancestor(N-1,T).
info(Format) -> io:format(lists:concat([ancestor(2),Format,"\r"])).
info(Format,Args) -> io:format(lists:concat([ancestor(2),Format,"\r"]),Args).
Lists is a custom module in the system. Use your foo module instead.

Related

How can I get Dialyzer to accept a call to a function that intentionally throws?

I have a function that intentionally throws when its first argument is the atom throw.
A simplified version of this code is:
-module(sample).
-export([main/1, throw_or_ok/1]).
main(_Args) ->
throw_or_ok(throw).
throw_or_ok(Action) ->
case Action of
throw -> throw("throwing");
ok -> ok
end.
Dialyzer errors on the call to throw_or_ok:
sample.erl:7: The call sample:throw_or_ok
('throw') will never return since it differs in the 1st argument from the success typing arguments:
('ok')
Adding specs doesn't help, the error message is the same:
-module(sample).
-export([main/1, throw_or_ok/1]).
-spec main(_) -> no_return().
main(_Args) ->
throw_or_ok(throw).
-spec throw_or_ok(throw) -> no_return(); (ok) -> ok.
throw_or_ok(Action) ->
case Action of
throw -> throw("throwing");
ok -> ok
end.
How can I get Dialyzer to accept calls to throw_or_ok/1 that are guaranteed to throw?
Unfortunately there is currently no clean way to mark this as acceptable for Dialyzer via specs.
Perhaps you can use an ignore warning annotation, however.
Looks like if will put throw it will never return, if will put ok the pattern will never match with throw. See topic with similar issue. The logic of main/1 need to change, eg:
main(Args) ->
MaybeOk = case Args of
0 -> throw;
_ -> ok
end,
throw_or_ok(MaybeOk).
OR
main(_Args) ->
throw_or_ok(_Args).

What's the point of meck:validate?

As a newcomer to meck, I've been putting together a test that shows the various features. I cannot, however, understand why a developer might call meck:validate. Here's my example:
-module(meck_demo).
-include_lib("eunit/include/eunit.hrl").
validate_is_of_limited_use_test_() ->
{ foreach, fun setup_mock/0, fun cleanup_mock/1,
[fun validate_does_not_fail_if_a_function_is_not_called/0,
fun validate_does_not_fail_if_a_function_is_called_with_wrong_arity/0,
fun validate_does_not_fail_if_an_undefined_function_is_called/0,
fun validate_does_fail_if_a_function_was_called_with_wrong_argument_types/0,
fun validate_does_fail_if_expectation_throws_an_unexpected_exception/0 ]}.
validate_does_not_fail_if_a_function_is_not_called() ->
meck:expect(womble, name, fun() -> "Wellington" end),
?assert(meck:validate(womble)).
validate_does_not_fail_if_a_function_is_called_with_wrong_arity() ->
meck:expect(womble, name, fun() -> "Madame Cholet" end),
?assertError(undef, womble:name(unexpected_arg)),
?assert(meck:validate(womble)).
validate_does_not_fail_if_an_undefined_function_is_called() ->
?assertError(undef, womble:fly()),
?assert(meck:validate(womble)).
validate_does_fail_if_a_function_was_called_with_wrong_argument_types() ->
meck:expect(womble, jump, fun(Height) when Height < 1 ->
ok
end),
?assertError(function_clause, womble:jump(999)),
?assertNot(meck:validate(womble)).
validate_does_fail_if_expectation_throws_an_unexpected_exception() ->
meck:expect(womble, jump, fun(Height) -> 42 = Height end),
?assertError({badmatch, 999}, womble:jump(999)),
?assertNot(meck:validate(womble)).
setup_mock() ->
meck:new(womble, [non_strict]).
cleanup_mock(_SetupResult) ->
meck:unload(womble).
What am I missing?
-- Updated to reflect the cases that Adam explains can be caught
You managed to hit just about every case not covered by validate (added better documentation in 10c5063).
Validate can detect:
When a function was called with the wrong argument types (function_clause)
When an exception was thrown
When an exception was thrown and expected (via meck:exception/2), which still results in true being return from meck:validate/1
Validate cannot detect:
When you didn't call a function
When you called a function with the wrong number of arguments
If you called an undefined function
The reason it cannot detect these cases is because of how Meck is implemented. Meck replaces the module with a mock and a process that maintains the mock. Everything Meck gets goes through that mock module. Meck does not insert itself at the caller level (i.e. in your module or in your test case), so it cannot know that you failed to call a module. All of the failures in your test case above never reaches the mock module in the first place.

Should I not call gen_server:stop() directly?

In the LYSE book the author handles the termination of the server as follows:
%% Synchronous call
close_shop(Pid) -> gen_server:call(Pid, terminate).
handle_call(terminate, _From, Cats) ->
{stop, normal, ok, Cats}.
terminate(normal, Cats) ->
[io:format("~p was set free.~n",[C#cat.name]) || C <- Cats],
ok.
So it returns a stop value from the handle_call callback.
Here is how I wrote it:
close_shop(Pid) -> gen_server:stop(Pid).
terminate(_Reason, {Cats, Money}) ->
io:format("Made $~w~n", [Money]),
[io:format("~p was set free.~n",[C#cat.name]) || C <- Cats].
Is this not a good practice then to call gen_server:stop() directly?
It is not a bad practice to call gen_server:stop/1,3 directly. It does an almost same thing as the example from LYSE but without calling handle_call/3 from your module. Try and check it out. You can even read the source code to be sure.

spawn function in erlang using function in another module

I have been learning erlang for the past week and am going through Joe Armstrong's Pragmatic erlang book . I was writing some code to spawn processes and have come across a situation
I have a function in module myatom.erl which looks like this
start(anatom,Fun) ->
case whereis(anatom) of
undefined ->
Pid = spawn(Fun),
try register(anatom,Pid) of
true -> true
catch
error:Reason ->
Reason
end;
Other -> {error,already_defined}
end.
There is a function in another module named tloop.erl
loop() ->
receive
{ From , No } -> From ! { self(), No*4};
Other -> void
end.
if I am to use start() to spawn loop in the erlang shell , how can I do it ?
I get the following error when I do
anatom:start(atomname,tloop:loop).
Thanks in advance !
anatom:start(myatom,fun tloop:loop).
* 2: syntax error before: ')
You must write the following
anatom:start(myatom, fun tloop:loop/0).
You have to specify the arity (number of arguments) of the function, as in erlang functions with the same name but different arity are not considered to be the same function.

Declaration of ETS in Erlang

The following code gives me an error: "syntax error before: Some_ets"
-module(tut).
-export([incr/1]).
Some_ets = ets:new(?MODULE, [bag]).
incr(X) ->
X+1.
But I am able to declare the ETS within a function, like:
-module(tut).
-export([incr/1]).
incr(X) ->
Some_ets = ets:new(?MODULE, [bag]),
X+1.
Can't I declare a ETS outside a function?
No - unlike other languages there isn't a concept of static initialization - there's no appropriate time for an Erlang system to execute that piece of code.
Erlang does have the concept of a parameterized module however, and that may be what you're after. Have a look here http://www.lshift.net/blog/2008/05/18/late-binding-with-erlang which is a good write up of that - it would allow you to instantiate an "instance" of your tut module bound to a given ets table and save passing around that handle explicitly in your module function calls.
Or if you are into OTP you could have the handle to the ets table passed around in the state variable:
init(_) ->
Some_ets = ets:new(?MODULE, [bag]),
{ok, Some_ets}.
and then use it in your handle_call methods:
get_ets_handle() ->
gen_server:call(?MODULE, {getETSHandle}, infinity).
handle_call({getETSHandle}, _From, Some_ets) ->
{reply, Some_ets, Some_ets}.
You can't do variable assignments like that in a module. See here.

Resources