| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | App::Phoebe::HeapDump - debugging Phoebe memory leaks | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Perhaps you find yourself in a desperate situation: your server is leaking | ||
| 24 | memory and you don't know where. This extension provides a way to use | ||
| 25 | L<Devel::MAT::Dumper> by allowing users identified with a known fingerprint of | ||
| 26 | their client certificate to initiate a dump. | ||
| 27 | |||
| 28 | You must set the fingerprints in your F<config> file. | ||
| 29 | |||
| 30 | package App::Phoebe; | ||
| 31 | our @known_fingerprints = qw( | ||
| 32 | sha256$fce75346ccbcf0da647e887271c3d3666ef8c7b181f2a3b22e976ddc8fa38401); | ||
| 33 | use App::Phoebe::HeapDump; | ||
| 34 | |||
| 35 | Once have restarted the server, L<gemini://localhost/do/heap-dump> will write a | ||
| 36 | heap dump to its wiki data directory. See L<Devel::MAT::UserGuide> for more. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | package App::Phoebe::HeapDump; | ||
| 41 | use App::Phoebe qw(@extensions $server $log @known_fingerprints | ||
| 42 | port host_regex space_regex success result); | ||
| 43 | use Modern::Perl; | ||
| 44 | use Devel::MAT::Dumper; | ||
| 45 | |||
| 46 | our @known_fingerprints; | ||
| 47 | |||
| 48 | # order is important: we must be able to reset the stats for tests | ||
| 49 | push(@extensions, \&heap_dump); | ||
| 50 | |||
| 51 | sub heap_dump { | ||
| 52 | my $stream = shift; | ||
| 53 | my $url = shift; | ||
| 54 | my $hosts = host_regex(); | ||
| 55 | my $port = port($stream); | ||
| 56 | if ($url =~ m!^gemini://(?:$hosts)(?::$port)?/do/heap-dump$!) { | ||
| 57 | with_heap_dump_fingerprint($stream, sub { | ||
| 58 | success($stream); | ||
| 59 | my $dir = $server->{wiki_dir}; | ||
| 60 | Devel::MAT::Dumper::dump("$dir/phoebe.pmat"); | ||
| 61 | $stream->write("# Heap Dump Saved\n"); | ||
| 62 | $stream->write("On the server, examine $dir/phoebe.pmat") }); | ||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub with_heap_dump_fingerprint { | ||
| 69 | my $stream = shift; | ||
| 70 | my $fun = shift; | ||
| 71 | my $fingerprint = $stream->handle->get_fingerprint(); | ||
| 72 | if ($fingerprint and grep { $_ eq $fingerprint} @known_fingerprints) { | ||
| 73 | $fun->(); | ||
| 74 | } elsif ($fingerprint) { | ||
| 75 | $log->info("Unknown client certificate $fingerprint"); | ||
| 76 | result($stream, "61", "Your client certificate is not authorised for heap dumping"); | ||
| 77 | } else { | ||
| 78 | $log->info("Requested client certificate"); | ||
| 79 | result($stream, "60", "You need an authorised client certificate to heap dump"); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; |