《Java程序设计》第一周学习记录(2)

使用JDB调试程序

JDB是JDK自带的基于命令行的调试程序。我们先来man一下吧(说到这里,我之前在翻娄老师的博客的时候看到一篇文章:做中学之提升英语口语很真实,虽然我六级不好不坏589,但是口语只有C+。平时看文档虽然认真看能看懂,但是还是喜欢偷懒去网上翻译。看来要走的路还很多,可能选走更困难一点的路是更正确的选择吧,这个话题以后再说)

jdb(1)                                           Basic Tools                                          jdb(1)

NAME
       jdb - Finds and fixes bugs in Java platform programs.

SYNOPSIS
       jdb [options] [classname]  [arguments]

   BASIC JDB COMMANDS
       The following is a list of the basic jdb commands. The JDB supports other commands that you can list with the -help option.

    help or ?
        The help or ? commands display the list of recognized commands with a brief description.
        不会就help咯
			  
    run    After you start JDB and set breakpoints, you can use the run command to execute the debugged application. The run command is available only when the jdb command starts the debugged application as opposed to attaching to an existing JVM.
        在你启动JDB并且设置断电以后,你可以使用run命令去执行被调试程序。run命令只有在jdb命令启动被调试程序以后才                                                                                可以使用,而不是附加到现存的JVM上

    cont    Continues execution of the debugged application after a breakpoint, exception, or step.
        在断点、exception、step后继续执行被调试程序
	   
    print    Displays Java objects and primitive values. For variables or fields of primitive types, the actual value is printed. For objects, a short description is printed. See the dump command to find out how to get more information about an object.
        显示Java对象和原始值。打印原始类型的变量或者字段。打印对象的简短描述。查看dump命令以了解如何获取更多的对象信息
			  
    dump    For primitive values, the dump command is identical to the print command. For objects, the dump command prints the current value of each field defined in the object. Static and instance fields are included. The dump command supports the same set of expressions as the print command.  
        对于基本类型,dump命令和print命令一样。对于对象,dump命令打印对象里定义的每一个字段的当前值。静态和实例字段包括在内。dump命令支持和print命令一样的表达式

    threads
        List the threads that are currently running. For each thread, its name and current status are printed and an index that can be used in other commands. 
        列出现在正在运行的线程。打印每个线程的名字、当前状态和能在其他命令里使用的索引
              
        In this example, the thread index is 4, the thread is an instance of java.lang.Thread, the thread name  is main, and it is currently running.

        4. (java.lang.Thread)0x1 main      running

        thread Select a thread to be the current thread. Many jdb commands are based on the setting of the current thread. The thread is specified with the thread index described in the threads command.
        选择一个线程作为当前线程。许多jdb命令基于当前线程的设定。线程由在threads命令里给的索引来指定

    where    The where command with no arguments dumps the stack of the current thread. The whereall command dumps the stack of all threads in the current thread group. The wherethreadindex command dumps the stack of the specified thread.
        没有参数的where命令转储当前线程的堆栈。

        If the current thread is suspended either through an event such as a breakpoint or through the suspend command, then local variables and fields can be displayed with the print and dump commands. The up and down commands select which stack frame is the current stack frame.
        如果当前线程被中止,例如断点或者suspend命令,那么本地变量和字段可以被print和dump命令显示。up和down命令选择哪个堆栈帧是当前堆栈帧

   BREAKPOINTS
       Breakpoints can be set in JDB at line numbers or at the first instruction of a method, for example:
       断点可以在JDB中按行号或者方法里的第一个指令设置

       · The command stop at MyClass:22 sets a breakpoint at the first instruction for line 22 of the source
         file containing MyClass.

       · The command stop in java.lang.String.length sets a breakpoint at the beginning of the method
         java.lang.String.length.

       · The command stop in MyClass.<clinit> uses <clinit> to identify the static initialization code for
         MyClass.


接下来就来试试吧。先打开三个标签页方便调试

按理来说现在是可以用Alt+数字来切换标签页,这样会很方便,但是不知道为什么,我的ubuntu在按Alt+1的时候切换不了,而是会出现(arg:1),希望有大神能告诉我怎么办。然后我们用jdb开始调试,用stop in 来设置断点,run运行到断点初停下,用locals来查看变量,step单步执行。要注意stepnext都是下一步,但是step会进入方法体,next不会
输入图片说明
使用list可以查看代码运行到哪里
输入图片说明
stop at在行号处设置断点,clear打印设置的断点
输入图片说明

更新:我知道为什么alt+数字没法切换标签页了,因为我根本就不是打开了三个标签页,而是打开了三个窗口。Ctrl+Alt+T是新建终端窗口,Ctrl+Shift+T是新建标签页!

系统文件被覆盖的挽救

在运行statistics.sh代码的时候,我一开始有点蒙,不知道要把这个脚本放到哪个文件夹下面去,然后做出了一个巨蠢无比的事

输入图片说明

我不知道当时是怎么想的,想把脚本放到/bin目录下,结果不行,我居然还用管理员权限强行执行了命令,结果就是,不仅脚本运行不了,好像系统也出了点问题

输入图片说明

虽然感觉没有什么大影响,但是指不定哪天就出问题了呢。。。。不行,我得修好啊。所以问题出在哪了呢?我首先要搞懂我刚刚干了什么

输入图片说明

/bin/sh是一个链接文件,看看里面是什么

输入图片说明

诶,这不是statistics.sh里的内容吗,也就是说,我强行把系统文件的内容覆盖了?!那要修复的话,只要把文件改回来就行了,好想要一个撤销键啊。。。我一开始想去网上百度一下有没有这个文件的内容直接复制进去算了,但是好像没有。那就只能从别的同学那里拷了。。。。等等!我突然想起来我
之前为了保险起见,用过VMware的快照功能,那我可以先拍下现在的快照,然后回到之前的快照,把完好的sh文件拷到共享文件夹,再回到现在的快照把sh文件改回来

输入图片说明

回到过去,看看sh到底是什么玩意

输入图片说明

。。。。。。。不管了,就是他了,拷到共享文件夹,再回到现在,改回来,看看行不行

输入图片说明

搞定!
以后还是要小心一点,不要玩坏了虚拟机,而且要养成及时备份的习惯。

初学者的一点学习经过,如有错误或可以改进的地方,请不吝赐教!

更新:使用jdb调试时遇到输入类的处理方法

参考资料

原文地址:https://www.cnblogs.com/20175211lyz/p/10466058.html