南⼤计算机系统基础实验PA0笔记
技能学习
最近开始学习南京⼤学计算机系统实验,刚刚完成PA0——开发环境配置。从安装 GNU/Linux 系统,熟悉Linux常⽤命令,学习Makefile,GDB,Git,TMux⼯具的的使⽤,编译并运⾏NEMU(全系统模拟器)。
PA0实验中遇到印象⾮常深刻的问题是:
1. 在Installing Tools环节,系统崩溃⾃动重启⽆法开机,安装的是双系统,ubuntu 20.04 LTS。第⼀次系统崩溃不知道是什么问题,
宫锁珠帘 百合
以为⾃⼰指令操作不当导致。只好重新安装系统(仍然是⽤之前做好的Ubuntu20.04LTS U盘),安装输⼊法、⼯具然后⼜是Installing Tools环节,⽆法关机(右上⾓的Poweroff按钮消失)后使⽤终端poweroff关机,此时已经凌晨2点以为解决了。早上起来⽆法开机(⼼态有点崩),在怀疑是系统与软件兼容问题?不过不是LTS版本吗?带着疑惑试了第三次重装,果然是⼀样的结果。
后重新下载ubuntu 21.04,后来没有故障。由于⾃⼰对linux的不熟悉导致了浪费了很多时间,不过也熟悉了不少。
2. 在Compiling and Running NEMU环节,在执⾏make menuconfig命令后出现报错,官⽅给的⽅案是⾃⼰解决。⼀开始看着报错摸不
着头脑,分析报错信息,查看nemu⽂件构成,功能等,半个⼩时过去了没解决(就开始作弊了,百度搜索…不过还好也没搜到解决⽅案),只能接着想后来在报错信息发现了端倪和官⽅的提⽰(缺少的⼯具装上),根据信息装上了两个⼯具后成功解决这个报错。虽然是个⼩问题,但⽆不反应普通⾼校学校的同学对学术欺骗的态度,和⾯对问题的独⽴解决能⼒差和不耐⼼,值得⾃⼰反思。
这是实验的开端,官⽅预计解决时间是10h,我花了23h,可见与南⼤学⼦的差距。此次实验也是对⾃⼰能⼒的调整与突破:尝试全英⽂学习,使⽤英⽂doc,全英⽂版ubuntu,全英⽂查阅问题
尽可能做到学术诚信,不抄袭,不参考他⼈代码,独⽴解决
PA0
First Exploration with GNU/Linux
GUI and CLI
if you stick to GUI, you can only do what it can; but in CLI(Command Line Interface), it can do what you want. Difference Between “su” and “su -” Commands
Linux User Environment: Linux’s systems are multi-user environments. Whenever Linux operating system creates a new shell session(a new terminal) it started preparing an environment for itself. This environment basically holds the Environment variable(Environment depends on shell type, Bash is generally used by most of the Linux distribution).
For example, pwd command is an environmental variable.
su command is an abbreviation(缩写) for “substitute user” because it is used for switching to another user during a normal login session, but it is often mistaken as an abbreviation for “super user” as mainly su command is used for getting “super user” privileges.
Su Command
$whoami
ryan #show current user
$pwd
/home/ryan #show cuurent directory
$su root #not create a new User Environment
#whoami
root
#pwd
/home/ryan #the same
Su - Command
$whoami
ryan #show current user
$pwd
/home/ryan #show current directory
何洁男友
$su - root #Create a new User Environment
#whoami
root
#pwd
/root
su command does not create a new User Environment (in the simple term they pretend to be the target user) but su -
**creates a totally new **User Environment(in the simple term they are actually the target user).
Sudo Command
赵本山妻子马丽娟sudo command executes instructions as a system administrator, that is to say, the instructions executed via sudo are as if they were executed by root himself.
More Exploration
Man Command
man an interface to the system reference manuals.
Input man ls, “LS(1)” is displayed in the upper left corner, “LS” means manual’s name and “(1)” means this in the first chapter.
Makefile
Makefile is a program-building tool that runs on Unix, Linux, and their flavors. It aids in simplifying building program executables that may need various modules. To determine how the modules need to be compiled or recompiled, make takes the help of user-defined makefiles. Makefile guides the make utility while compiling and linking program modules.
Compiling the source code files can be tiring, especially when you have to include several source files and type the compiling command every time you need to compile. Makefiles are the solution to simplify this task.
Makefiles are speical format files that help build and manage the projects automatically.
The make command allows you to manage large programs or groups of programs. As you begin to write large programs, you notice that re-compiling large programs take a longer time than re-compiling short programs. Moreover, you notice that you usually only work on a small section of the program, and much of reaming program is unchanged.
Difference between g++ & gcc
g++gcc
g++ is used to compile C++ is used to compile C program
g++ can compile any .c or .cpp files but they will be treated as
C++ can compile any .c or .cpp files but they will be treated as C and
C++ respectively.
g++ fileName.cpp -o binary gcc fileName.c -o binary
Using g++ to link the object files, file automatically links in the
std C++ libraries.
gcc does not do this.
Defining Rules in Makefile
target [] : []
[]
In the above code, the arguments in brackets are optional and ellipsis means one or more.Here, note that the tab to preface each command is required.
Make How to Execute
Find “Makefile” or “makefile” files in the current directory.
If you find it, make will search the first target of the makefile, such as the main file(executable file).
If the main file is not found or the .o files that the main file depends on are newer than it, execute the command to generate the main file.
If the .o files are not found, make will find the dependent files of the .o files and execute the command to generate .o files(It likes the process of the stack).
If the .o files and .h files are both found, make will generate .o files then use .o files to achieve the final task - - -generate the main file.
Special Macros
$@The file name of the target.
$<The name of the first dependency.
$*The part of filename which matched a suffix rule.
$?The names of all the dependencies newer than the target separated by spaces.
$^The names of all the dependencies separated by spaces, but with duplicate names removed.
GDB
GDB, short for GNU Debugger, is the most popular debugger for UNIX systems to debug C and C++
programs. A debugger is a program that runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise.
GDB Command
Getting Started: Starting and Stopping
gcc -g main.c - Compiles main.c with the debugging option (-g). You still get an a.out, but it contains debugging
小g娜爆吴亦凡尺寸information that lets you use variables and function names inside GDB.
云梦泽gdb a.out - Opens GDB with file a.out, but does not run the program.
r - Runs the program until a breakpoint or error
q - Quit GDB
l - List 10 lines of source code for current line.
Stepping through Code
next - Runs the program until next line, then pauses. If the current line is a function, it executes the entire function , the pauses. next is good for walking through your code quickly.
step - Runs the next instruction, not line.If the current instruction is setting a variable, it is the same as next. If it’s a function, it will jump into the function, execute the first statement, then pause. step is good for diving into the detail of your code.
finish - Finishes executing the current function, then pause (also called step out).
Breakpoints or Watchpoints
print x - Prints current value of variable x.
display x - Constantly displays the value of variable x, which is shown after every step of pause.
Getting Source Code for PAs
Git usage
Use the branch feature of git to manage the process of development. A branch is an ordered list of commits , where a commit refers to some modifications in the project.
git branch - List all branches.
** git checkout -b newbranch** - Create a new branch and check out to it.
git status - See those files modified from the last commit.
聊天记录删了怎么恢复git diff - List modifications from the last commit.
git add . - Add the changes to commit.
git commit - Finish a commit.
git checkout branch - Check out to branch.
git log - The changes is traced.
Compiling and Running NEMU
NEMU (NJU Emulator) is a simple but complete full-system emulator designed for teaching purposes. Currently, it supports x86, mips32, riscv32, and riscv64. To build programs run above NEMU, refer to the .