| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011-2014 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | use warnings; | ||
| 4 | use rlib '../../../../..'; | ||
| 5 | |||
| 6 | package Devel::Trepan::CmdProcessor::Command::Info::Files; | ||
| 7 | use Cwd 'abs_path'; | ||
| 8 | |||
| 9 | use Devel::Trepan::CmdProcessor::Command::Subcmd::Core; | ||
| 10 | use Devel::Trepan::DB::LineCache; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | our (@ISA, @SUBCMD_VARS); | ||
| 14 | # Values inherited from parent | ||
| 15 | use vars @Devel::Trepan::CmdProcessor::Command::Subcmd::SUBCMD_VARS; | ||
| 16 | |||
| 17 | our @DEFAULT_FILE_ARGS = qw(size mtime sha1); | ||
| 18 | our $DEFAULT_FILE_ARGS = join(' ', @DEFAULT_FILE_ARGS); | ||
| 19 | |||
| 20 | ## FIXME: do automatically. | ||
| 21 | our $CMD = "info files"; | ||
| 22 | |||
| 23 | unless (@ISA) { | ||
| 24 | eval <<"EOE"; | ||
| 25 | use constant MAX_ARGS => 8; # Need at most this many - undef -> unlimited. | ||
| 26 | EOE | ||
| 27 | } | ||
| 28 | |||
| 29 | @ISA = qw(Devel::Trepan::CmdProcessor::Command::Subcmd); | ||
| 30 | =pod | ||
| 31 | |||
| 32 | =head2 Synopsis: | ||
| 33 | |||
| 34 | =cut | ||
| 35 | our $HELP = <<'HELP'; | ||
| 36 | =pod | ||
| 37 | |||
| 38 | B<info files> [{I<filename>|B<*>} [B<all>|B<ctime>|B<brkpts>|B<mtime>|B<sha1>|B<size>|B<stat>]] | ||
| 39 | |||
| 40 | Show information about the current file. If no filename is given and | ||
| 41 | the program is running, then the current file associated with the | ||
| 42 | current stack entry is used. Giving . has the same effect. | ||
| 43 | |||
| 44 | B<*> gives a list of all files we know about. | ||
| 45 | |||
| 46 | Sub-options which can be shown about a file are: | ||
| 47 | |||
| 48 | =over 2 | ||
| 49 | |||
| 50 | =item * | ||
| 51 | |||
| 52 | B<brkpts> E<mdash> Line numbers where there are statement boundaries. | ||
| 53 | These lines can be used in breakpoint commands. | ||
| 54 | |||
| 55 | =item * | ||
| 56 | |||
| 57 | B<ctime> E<mdash> File creation time | ||
| 58 | |||
| 59 | =item * | ||
| 60 | |||
| 61 | B<time> E<mdash> File modification time | ||
| 62 | |||
| 63 | =item * | ||
| 64 | |||
| 65 | B<sha1> E<mdash> A SHA1 hash of the source text. This may be useful in | ||
| 66 | comparing source code. | ||
| 67 | |||
| 68 | =item * | ||
| 69 | |||
| 70 | B<size> E<mdash> The number of lines in the file. | ||
| 71 | |||
| 72 | =item * | ||
| 73 | |||
| 74 | B<stat> E<mdash> I<stat()> information | ||
| 75 | |||
| 76 | =item * | ||
| 77 | |||
| 78 | B<all> E<mdash> All of the above information. | ||
| 79 | |||
| 80 | =back | ||
| 81 | |||
| 82 | If no sub-options are given, I<size mtime sha1> are assumed. | ||
| 83 | |||
| 84 | =head2 Examples: | ||
| 85 | |||
| 86 | info files # Show "size mtime sha1" information about current file | ||
| 87 | info files . # same as above | ||
| 88 | info files . brkpts # show the number of lines in the current file | ||
| 89 | info files . brkpts size # above plus file size | ||
| 90 | info files * # Give a list of files we know about | ||
| 91 | =cut | ||
| 92 | HELP | ||
| 93 | |||
| 94 | our $SHORT_HELP = 'Show information about the current loaded file(s)'; | ||
| 95 | our $MIN_ABBREV = length('fi'); | ||
| 96 | |||
| 97 | no warnings 'redefine'; | ||
| 98 | sub complete($$) | ||
| 99 | { | ||
| 100 | my ($self, $prefix) = @_; | ||
| 101 | my @completions = ('.', DB::LineCache::file_list()); | ||
| 102 | Devel::Trepan::Complete::complete_token(\@completions, $prefix); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub run($$) | ||
| 106 | { | ||
| 107 | my ($self, $args) = @_; | ||
| 108 | my $proc = $self->{proc}; | ||
| 109 | my @args = @$args; shift @args; shift @args; | ||
| 110 | push(@args, '.') if scalar @args == 0; | ||
| 111 | if ($args[0] eq '*') { | ||
| 112 | $proc->section('Cached files:'); | ||
| 113 | my @primary = DB::LineCache::cached_files(); | ||
| 114 | @primary = sort @primary; | ||
| 115 | $proc->msg($self->{cmd}->columnize_commands(\@primary)); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | my $filename = shift @args; | ||
| 119 | if ($filename eq '.') { | ||
| 120 | my $frame_file = $proc->filename; | ||
| 121 | $filename = map_file($frame_file) || | ||
| 122 | abs_path($frame_file); | ||
| 123 | } | ||
| 124 | @args = @DEFAULT_FILE_ARGS if 0 == scalar @args; | ||
| 125 | |||
| 126 | my $m = $filename; | ||
| 127 | my $canonic_name = $proc->canonic_file($filename); | ||
| 128 | $canonic_name = map_file($canonic_name) || $canonic_name; | ||
| 129 | if (is_cached($canonic_name)) { | ||
| 130 | $m .= " is cached in debugger"; | ||
| 131 | if ($canonic_name ne $filename) { | ||
| 132 | $m .= (" as:\n " . $canonic_name); | ||
| 133 | } | ||
| 134 | $m .= '.'; | ||
| 135 | $proc->msg($m); | ||
| 136 | # } elsif (!(matches = find_scripts(filename)).empty?) { | ||
| 137 | # if (matches.size > 1) { | ||
| 138 | # $self->msg("Multiple files found:"); | ||
| 139 | # matches.sort.each { |match_file| msg "\t%s" % match_file } | ||
| 140 | # return; | ||
| 141 | # } else { | ||
| 142 | # $self->msg('File "%s" just now cached.' % filename); | ||
| 143 | # LineCache::cache(matches[0]); | ||
| 144 | # remap_file(filename, matches[0]); | ||
| 145 | # canonic_name = matches[0]; | ||
| 146 | # } | ||
| 147 | } else { | ||
| 148 | my @matches = (); | ||
| 149 | for my $try (file_list()) { | ||
| 150 | push @matches, $try unless -1 == rindex($try, $filename); | ||
| 151 | } | ||
| 152 | if (scalar(@matches) > 1) { | ||
| 153 | $proc->msg("Multiple files found ending filename string:"); | ||
| 154 | for my $match_file (@matches) { | ||
| 155 | $proc->msg("\t$match_file"); | ||
| 156 | } | ||
| 157 | return | ||
| 158 | } elsif (1 == scalar(@matches)) { | ||
| 159 | $canonic_name = map_file($matches[0]); | ||
| 160 | $m .= " matched debugger cache file:\n\t" . $canonic_name; | ||
| 161 | $proc->msg($m); | ||
| 162 | } else { | ||
| 163 | $proc->msg($m . ' is not cached in debugger.'); | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | my %seen; | ||
| 168 | for my $arg (@args) { | ||
| 169 | my $processed_arg = 0; | ||
| 170 | my $arg = lc($arg); | ||
| 171 | |||
| 172 | if ($arg eq 'all' || $arg eq 'size') { | ||
| 173 | unless ($seen{size}) { | ||
| 174 | my $max_line = | ||
| 175 | Devel::Trepan::DB::LineCache::size($canonic_name); | ||
| 176 | $proc->msg("File has $max_line lines.") if defined $max_line; | ||
| 177 | } | ||
| 178 | $processed_arg = $seen{size} = 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | if ($arg eq 'all' || $arg eq 'sha1') { | ||
| 182 | unless ($seen{sha1}) { | ||
| 183 | my $sha1 = Devel::Trepan::DB::LineCache::sha1($canonic_name); | ||
| 184 | $proc->msg("SHA1: ${sha1}"); | ||
| 185 | } | ||
| 186 | $processed_arg = $seen{sha1} = 1; | ||
| 187 | } | ||
| 188 | |||
| 189 | ## Breakpoints are broken. Something changed to break it and | ||
| 190 | ## I haven't a clue. Furthermore, %{'_<'.$filename} no longer seems | ||
| 191 | ## be set so we can't test for == 1 or == 0 in numeric context and | ||
| 192 | ## get something. | ||
| 193 | if ($arg eq 'all' || $arg eq 'brkpts') { | ||
| 194 | unless ($seen{brkpts}) { | ||
| 195 | $proc->msg("Possible breakpoint line numbers:"); | ||
| 196 | my @lines = trace_line_numbers($canonic_name); | ||
| 197 | my $fmt_lines = $self->{cmd}->columnize_numbers(\@lines); | ||
| 198 | chomp $fmt_lines; | ||
| 199 | $proc->msg($fmt_lines); | ||
| 200 | } | ||
| 201 | $processed_arg = $seen{brkpts} = 1; | ||
| 202 | } | ||
| 203 | |||
| 204 | if ($arg eq 'all' || $arg eq 'ctime') { | ||
| 205 | unless ($seen{ctime}) { | ||
| 206 | my $stat = Devel::Trepan::DB::LineCache::stat($canonic_name); | ||
| 207 | if (defined $stat) { | ||
| 208 | my $ctime = | ||
| 209 | Devel::Trepan::DB::LineCache::stat($canonic_name)->ctime; | ||
| 210 | $ctime = localtime($ctime); | ||
| 211 | $proc->msg("Creation time:\t$ctime"); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | $processed_arg = $seen{ctime} = 1; | ||
| 215 | } | ||
| 216 | |||
| 217 | if ($arg eq 'all' || $arg eq 'mtime') { | ||
| 218 | unless ($seen{mtime}) { | ||
| 219 | my $stat = Devel::Trepan::DB::LineCache::stat($canonic_name); | ||
| 220 | if (defined($stat)) { | ||
| 221 | my $mtime = localtime($stat->mtime); | ||
| 222 | $proc->msg("Modify time:\t$mtime"); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | $processed_arg = $seen{mtime} = 1; | ||
| 226 | } | ||
| 227 | |||
| 228 | # if ($arg eq 'all' || $arg eq 'stat') { | ||
| 229 | # unless ($seen{stat}) { | ||
| 230 | # require Enbugger; Enbugger->stop; | ||
| 231 | # my $stat = Devel::Trepan::DB::LineCache::stat($canonic_name); | ||
| 232 | # my $msg = sprintf "File attributes:\t%s", join(', ', @$stat); | ||
| 233 | # $proc->msg($msg); | ||
| 234 | # } | ||
| 235 | # $processed_arg = $seen{stat} = 1; | ||
| 236 | # } | ||
| 237 | |||
| 238 | unless ($processed_arg) { | ||
| 239 | $proc->errmsg("I don't understand sub-option \"$arg\""); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | unless (caller) { | ||
| 245 | require Devel::Trepan; | ||
| 246 | require Devel::Trepan::DB::LineCache; | ||
| 247 | cache_file(__FILE__); | ||
| 248 | print join(', ', file_list), "\n"; | ||
| 249 | # Demo it. | ||
| 250 | # require_relative '../../mock' | ||
| 251 | # my($dbgr, $parent_cmd) = MockDebugger::setup('show'); | ||
| 252 | # $cmd = __PACKAGE__->new(parent_cmd); | ||
| 253 | # $cmd->run(@$cmd->prefix); | ||
| 254 | } | ||
| 255 | |||
| 256 | # Suppress a "used-once" warning; | ||
| 257 | $HELP || scalar @SUBCMD_VARS; |