Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Matching on maps not supported? #2

Closed
nietaki opened this issue Nov 7, 2017 · 9 comments
Closed

[question] Matching on maps not supported? #2

nietaki opened this issue Nov 7, 2017 · 9 comments

Comments

@nietaki
Copy link

nietaki commented Nov 7, 2017

When playing around with redbug (which I loved right out of the gate) I realized it doesn't support matching maps in the trace pattern arguments:

10> redbug:start("erlang:term_to_binary([])").
{38,1}
redbug done, timeout - 0
11> [A, B, C] = "#\{\}".
"#{}"
12> redbug:start("erlang:term_to_binary(#\{\})").
{bad_type,{map,[]}}
13> redbug:start("erlang:term_to_binary(X) when X == 1").
{38,1}
14> redbug:stop().
stopped
redbug done, local_done - 0
15> redbug:start("erlang:term_to_binary(X) when #\{\} = X").
{function_clause,{parse_guards,"erlang:term_to_binary(X) when #{} = X"},
                 [{redbug_msc,arg,
                              [{match,1,{map,1,[]},{var,1,'X'}}],
                              (... more errors cut)

After looking at the source code, I think it's intentional.

While I'm not questioning the decision, I'm curious to the motivation. Is there any reason in particular why matching maps isn't supported in the trace pattern?

Thanks in advance and sorry to bother you with this GitHub issue.

@massemanet
Copy link
Owner

sorry for the late reply.

AFAICT, the BEAM tracing does not support matching on maps.

Possibly that has changed in erlang 20?

@nietaki
Copy link
Author

nietaki commented Jan 9, 2018

I couldn't find any information on it, so today I tried to repro it to see if it's supported. It seems like it's possible, at least on Erlang 20.

wat.erl:

-module(wat).
-export([start/1]).

start(X) -> X.
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.0  (abort with ^G)
1> c(wat).
{ok,wat}
2> l(wat).
{module,wat}
3> erlang:trace(all, true, [call]).
29
4> erlang:trace_pattern({wat, start, '_'}, [{[#{foo => bar}], [], []}], [local]).
1
5> spawn(fun() -> wat:start(#{baz => ban}) end).
<0.70.0>
6> spawn(fun() -> wat:start(#{foo => bar}) end).
<0.72.0>
7> spawn(fun() -> wat:start(foo) end).
<0.74.0>
8> flush().
Shell got {trace,<0.72.0>,call,{wat,start,[#{foo => bar}]}}
ok

I'll try earlier versions of Erlang to see where it's possible.

I'd be happy to write a PR to make matching on maps possible in redbug, but considering my understanding on the low level tracing is shaky at best and I don't write Erlang day to day, you might be able to do it much quicker and with better code quality 😄

@nietaki
Copy link
Author

nietaki commented Jan 9, 2018

The example I posted works in in 19.3 and 18.3, I had problems compiling 17.x so I couldn't check further than that.

@massemanet
Copy link
Owner

I'll do it. I've been waiting for them to support maps (or maybe they did all along and I just missed it).
thanks for the help!

@nietaki
Copy link
Author

nietaki commented Jan 10, 2018

Brilliant, thank you!

@massemanet
Copy link
Owner

(Possibly) fixed in 0bc3ffa

@nietaki
Copy link
Author

nietaki commented Feb 4, 2018

I tried 935ae87 and it works like a charm, with var binding, good errors for invalid trace patterns and everything.

From CI it looks like it doesn't work with Erlang 16 anymore, but it should be ok with semver if you release it under 1.2.x.

Thank you for doing this!

@massemanet
Copy link
Owner

I'll tag it with 1.2.0
thx for testing.

@massemanet
Copy link
Owner

fixed in 1.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants