-
Notifications
You must be signed in to change notification settings - Fork 33
Awesome Match Specs
Péter Gömöri edited this page Nov 2, 2017
·
2 revisions
A community-edited collection of match-spec examples for tracing. Did you find a match-spec that you use often or find tricky? Please add it below so that we can all learn from each other.
The snippets below use XProf-flavoured match-spec fun syntax (either Erlang or Elixir).
- Only capture
ets:lookup
calls on tabledata
ets:lookup(data, _)
- Capture connecting to either TCP port 80 or 443
gen_tcp:connect(_, Port, _, _) when Port =:= 80; Port =:= 443
- Only capture the important field of a possibly big tuple
ets:insert(_, Data) -> message(element(3, Data)).
- And just for the sake of example connecting to either port
expressed with multiple clauses. (As the match-spec body is
only evaluated for its side-effects or action-function calls and
the actual return value is ignored to achieve the default
behaviour any dummy term can be put there like
ok
ortrue
)
gen_tcp:connect(_, 80, _, _) -> true; (_, 443, _, _) -> true end.
- Capture
Registry
lookups onMyApp.Registry
Registry.lookup(MyApp.Registry, _)
- Capture dispatching to
"topic1"
or"topic2"
Registry.dispatch(MyApp.Registry, topic, _) when topic in ["topic1", "topic2"]
- Instead of a possibly long list only capture the length of the list
Enum.fetch(list, index) when is_list(list) -> message([length(list), index])
- Again just for the example dispatching to multiple topics expressed with multiple clauses.
Registry.dispatch(MyApp.Registry, "topic1", _) -> nil; (MyApp.Registry, "topic2", _) -> nil
Capture lists:foldl/3
calls, but only when it is not called by the sync
application.
(Sync recompiles and reloads your Erlang code on-the-fly, and calls lists:foldl/3
periodically).
Note that the caller()
action-function can only be used in the match-spec body, but there we cannot use pattern matching or if/case statements, so andalso
to the rescue.
lists:foldl(_,_,_) -> element(1,caller()) =:= sync_scanner andalso message(false)