agent still works in remote mode and executable environment may not
allow to save any data at all (e.g. in k8s container), so be robust.
Signed-off-by: Alex Lyashko <alex.lyashko@gmail.com>
If gopsdir contains a trailing slash, the resulting portfile path will
contain a trailing double slash which might lead to problems in some
circumstances. Moreover iut could also be problematic on platforms where
other path separators are used (e.g. Windows).
Fix this by using filepath.Join to construct the path.
Introduce Option.SocketReuseAddrAndPort which, if set, will lead to the
SO_REUSEPORT socket option being set on the listening socket on
Unix-like OSes. This also sets SO_REUSEADDR which is already the default
in net.Listen (see net.setDefaultSockopts).
Setting these options increases the chance to re-bind() to the same
address and port upon agent restart if Options.Addr is set.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
In case the agent is exiting due to a SIGTERM or SIGQUIT it is leaking
the port files in the gops config directory, as e.g. reported in
cilium/cilium#11455
Make sure the socket is cleaned up and port files in the gops config
directory are removed upon these signals by also relaying these signals
to the signal channel in gracefulShutdown. In case of SIGTERM, which is
the "normal" termination signal, exit the process with exit status 0.
As part of agent.Close, Close is called on the listener. This has
Accept return a "use of closed network connection" error, which is
expected. This error is logged but it would be nice if it wasn't.
Unfortunately, there's not a great way to check for this
error (https://golang.org/issues/4373) so use a best-effort
strings.Contains check.
The warnings that agent prints to os.Stderr did not previously include
newlines causing program output to get jumbled up when an error occurs.
We see this most often when calling `Close()`, which causes the listener
to print `gops: accept tcp 127.0.0.1:9999: use of closed network
connection` which breaks whatever is logged next.
* Introducing support to debug.SetGCPercent.
Fixes#8
On my I needed to augment a little bit the communication
protocol to add support to optional parameters.
Using pprof.Lookup("goroutine") instead of runtime.Stack directly
because the former already takes care of expanding the buffer used with
runtime.Stack until it's big enough for the stack.
If the application the agent is being installed on also need to run some
shutdown code during an Interrupt signal, it's important to have the
ability to prevent gops agent from calling os.Exit(). This also adds the
requirement of exposing a method to manually cleanup garbage left by the
gops agent.
To allow custom agent flags now and in the future an Agent type was
created with a public boolean field HandleSignals.