Astarno
What would be the correct approach to inspect the current environment of a Process? For example, if I would have a simple Process defined as below:
defmodule Stack do
def loop(state, ctr) do
receive do
{_from, :push, value} ->
loop([value | state], ctr + 1)
{from, :pop} ->
[h | t] = state
send(from, {:reply, h})
loop(t, ctr)
end
loop(state)
end
end
Here the environment consists of two variables (parameters of the loop function) at any point during execution. How could I easily get a list of such variables and their associated value as such:
state: value
ctr: value
I’m new to Elixir. Thanks in advance!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lucaong
Hi @Astarno,
In your example, you could add a receive clause to return the state:
Now you can send a message like
{self(), :inspect}and receive the state as a message:Astarno
I’m trying to do this without altering any code inside of the Process. Would you have an approach for that as well?
lucaong
When implementing something similar with a
GenServerone can use:sys.get_state(pid)for debugging, but in this case I don’t see a way without modifying the code. In your example the state is the arguments of the recursive call toloop, so one needs to tap into the function to inspect the state.What’s your use case?
Astarno
I’m researching if Elixir would be fit to develop and experiment with a new visual inspection tool. For this I would require to be able to hook into and inspect a lot of meta information, such as the enivronment of an actor. The idea is that this visual inspection tool should work on existing code, requiring no changes or only very minimal changes to it (like an import).
Unfortunatly Elixir seems to let me down on this front. Other things I’ve looked at such as extending the
receivemacro also seem to not be possible.RudManusachi
In general the better practice would be to use built in OTP abstractions like GenServer, as @lucaong mentioned, rather than plain loop with
receive do, and we can use:sys.get_state,:sys.replace_state,:observerand other tools to inspect running system in realtime without changing the code of the project.Astarno
Would using GenServer have any disadvantages over simple Processes? Would this also allow me to inspect stuff like the mailbox, the arrival of messages, etc without adding code?
RudManusachi
I can’t think of real disadvantages. Surely, GenServer and other OTP constructs add some overhead to bare-process as they handle more stuff for us but it’s for our own good =)
please take a look at this answer and the thread overall
Process.info/2could help to get the info about mailbox. Though, one thing to note, is GenServer reads messages from mailbox upon arrival and matches to one of the callbacks. So if the process is not blocked you won’t see the messages in the mailbox by checking them viaProcess.info/2. It’s also possible to trace the process for example with:dbg.p/2see flag
:rSo it depends on what you are trying to achieve and how you build the system.