Acquiring Source Code for PAs
TODO
- Read the whole text carefully.
- Try the commands of Linux mentioned.
- Try to use commands about
git
. - Get the main project and initialize with your own infomation.
- Upload the project to private on-line coding repositories.
Now it's time to get ready for the trip to the simulated world of computer and this section will lead you to the entrance of it.
Getting Source Code for PAs
Go back to the home directory by
cd ~
Usually, all works unrelated to system should be performed under the home directory. Other directories under the root of file system (/
) are related to system. Therefore, do NOT finish your PAs and Labs under these directories by sudo
.
{% panel style="danger", title="不要使用 root 账户做实验!!!" %}
从现在开始, 所有与系统相关的配置工作已经全部完成, 你已经没有使用root账户的必要. 继续使用root账户进行实验, 会改变实验相关文件的权限属性, 可能会导致开发跟踪系统无法正常工作; 更严重的, 你的误操作可能会无意中损坏系统文件, 导致虚拟机无法启动! 往届有若干学长因此而影响了实验进度, 甚至由于损坏了实验相关的文件而影响了分数. 请大家引以为鉴, 不要贪图方便, 否则后果自负!
如果你仍然不理解为什么要这样做, 你可以阅读这个页面: Why is it bad to login as root? 正确的做法是: 永远使用你的普通账号做那些安分守己的事情(例如写代码), 当你需要进行一些需要root权限才能进行的操作时, 使用sudo
.
{% endpanel %}
Now acquire source code for PA by the following command:
git clone https://gitee.com/nuaa-pa-2021/ics-pa.git ics2021
A directory called ics2021
will be created. This is the project directory for PAs. Details will be explained in PA1.
Issue the following commands to perform git
configuration:
git config --global user.name "161220000-Zhang San" # your student ID and name
git config --global user.email "zhangsan@foo.com" # your email
git config --global core.editor vim # your favorite editor
git config --global color.ui true
You should configure git
with your student ID, name, and email. Before continuing, please read this git
tutorial to learn some basics of git
.
Enter the project directory ics2021
, then run
git branch -m master
bash init.sh
to add some environment variables into the bash configuration file ~/.bashrc
. These variables are defined by absolute path to support the compilation of the subprojects. Therefore, DO NOT move your project to another directory once the initialization finishes, else these variables will become invalid. Particularly, if you use shell other than bash
, please set these variables in the corresponding configuration file manually.
Git Usage
There is a official documentation for Git: https://git-scm.com/book/en/v2 . You can learn it whenever you have problems using git.
We will 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.
You can list all branches by
git branch
You will see there is only one branch called "master" now.
* master
To create a new branch, use git checkout
command:
git checkout -b pa0
This command will create a branch called pa0
, and check out to it. Now list all branches again, and you will see we are now at branch pa0
:
master
* pa0
From now on, all modifications of files in the project will be recorded in the branch pa0
.
{% panel style="success", title="have a try!" %}
Now Modify the STU_ID
variable in nemu/Makefile.git
:
STU_ID=161220000 # your student ID
Run
git status
to see those files modified from the last commit:
On branch pa0
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: nemu/Makefile.git
no changes added to commit (use "git add" and/or "git commit -a")
Run
git diff
to list modifications from the last commit:
diff --git a/nemu/Makefile.git b/nemu/Makefile.git
index c9b1708..b7b2e02 100644
--- a/nemu/Makefile.git
+++ b/nemu/Makefile.git
@@ -1,4 +1,4 @@
-STU_ID = 161220000
+STU_ID = 161221234
# DO NOT modify the following code!!!
You should see the STU_ID
is modified. Now add the changes to commit by git add
, and issue git commit
:
git add .
git commit
The git commit
command will call the text editor. Type modified my STU_ID
in the first line, and keep the remaining contents unchanged. Save and exit the editor, and this finishes a commit. Now you should see a log labeled with your student ID and name by
git log
{% endpanel %}
Now switch back to the master
branch by
git checkout master
Open nemu/Makefile.git
, and you will find that STU_ID
is still unchanged! By issuing git log
, you will find that the commit log you just created has disappeared!
Don't worry! This is a feature of branches in git
. Modifications in different branches are isolated, which means modifying files in one branch will not affect other branches. Switch back to pa0
branch by
git checkout pa0
You will find that everything comes back! If you want to make changes in pa0
merged to master
, you can do that with git merge
command.
{% panel style="danger", title="为每个阶段的 PA 建立一个分支" %}
The workflow above shows how you will use branch in PAs:
- before starting a new PA, new a branch
pa?
and check out to it - coding in the branch
pa?
(this will introduce lot of modifications)
不要忘记在每个阶段开始时建立一个新分支 pa?
,不要把 pa?
合并到 master
,也不要把新阶段的 pa?
合并到上个阶段的分支 pa?-1
,否则检查脚本爬取 git log 时会错误地记录你完成每个功能点的时间,导致你被判定为抄袭等情况!!!
{% endpanel %}
Compiling and Running NEMU
Now enter nemu/
directory, and compile the project by make
:
make
If nothing goes wrong, NEMU will be compiled successfully.
{% panel style="success", title="What happened?" %}
You should know how a program is generated in the 程序设计基础 course. But do you have any idea about what happened when a bunch of information is output to the screen during make
is executed?
{% endpanel %}
To perform a fresh compilation, type
make clean
to remove the old compilation result, then make
again.
To run NEMU, type
make run
However, you will see an error message:
nemu: nemu/src/cpu/reg.c:21: reg_test: Assertion `(cpu.gpr[check_reg_index(i)]._16) == (sample[i] & 0xffff)' failed.
This message tells you that the program has triggered an assertion fail at line 21 of the file nemu/src/cpu/reg.c
. If you do not know what is assertion, blame the 程序设计基础 course. If you go to see the line 21 of nemu/src/cpu/reg.c
, you will discover the failure is in a test function. This failure is expected, because you have not implemented the register structure correctly. Just ignore it now, and you will fix it in PA1.
To debug NEMU with gdb, type
make gdb
Development Tracing
Once the compilation succeeds, the change of source code will be traced by git
. Type
git log
If you see something like
commit 4072d39e5b6c6b6837077f2d673cb0b5014e6ef9
Author: tracer-ics2017 <tracer@njuics.org>
Date: Sun Jul 26 14:30:31 2017 +0800
> compile NEMU
161220000
user
Linux debian 3.16.0-4-686-pae #1 SMP Debian 3.16.7-3 i686 GNU/Linux
14:30:31 up 3:44, 2 users, load average: 0.28, 0.09, 0.07
3860572d5cc66412bf85332837c381c5c8c1009f
this means the change is traced successfully.
If you see the following message while executing make, this means the tracing fails.
fatal: Unable to create '/home/user/ics2020/.git/index.lock': File exists.
If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.
Try to clean the compilation result and compile again:
make clean
make
If the error message above always appears, please contact us as soon as possible.
{% panel style="danger", title="开发跟踪" %}
我们使用 git
对你的实验过程进行跟踪, 不合理的跟踪记录会影响你的成绩. 往届有学长"完成"了某部分实验内容, 但我们找不到相应的 git log, 最终该部分内容被视为没有完成. git log 是独立完成实验的最有力证据, 完成了实验内容却缺少合理的 git log, 不仅会损失大量分数, 还会给抄袭判定提供最有力的证据. 因此, 请你注意以下事项:
- 请你不定期查看自己的 git log, 检查是否与自己的开发过程相符.
- 提交往届代码将被视为没有提交.
- 不要把你的代码上传到公开的地方.
- 总是在工程目录下进行开发, 不要在其它地方进行开发, 然后一次性将代码复制到工程目录下, 这样
git
将不能正确记录你的开发过程. - 不要修改
Makefile
中与开发跟踪相关的内容. - 不要删除我们要求创建的分支, 否则会影响我们的脚本运行, 从而影响你的成绩
- 不要清除 git log
- 不要尝试用脚本刷 git log,要发现它们还是很容易的,只要是正常实验的 git log 都不会被误判
偶然的跟踪失败不会影响你的成绩. 如果上文中的错误信息总是出现, 请尽快联系我们. 由于检查工作量庞大,一切检查均使用脚本进行,不符合讲义要求将出现错误,若脚本筛选出异常的 git log,将进行人工二次筛查,请务必重视自己的代码!
{% endpanel %}
Local Commit
Although the development tracing system will trace the change of your code after every successful compilation, the trace record is not suitable for your development. This is because the code is still buggy at most of the time. Also, it is not easy for you to identify those bug-free traces. Therefore, you should trace your bug-free code manually.
When you want to commit the change, type
git add .
git commit --allow-empty
The --allow-empty
option is necessary, because usually the change is already committed by development tracing system. Without this option, git
will reject no-change commits. If the commit succeeds, you can see a log labeled with your student ID and name by
git log
To filter out the commit logs corresponding to your manual commit, use --author
option with git log
. For details of how to use this option, RTFM.
Submission
Finally, you should submit your project to the submission website.
ics2021
├── init.sh
├── Makefile
├── nanos-lite
├── navy-apps
├── nemu
└── nexus-am
Then go back to the project directory, issue
make submit
This command does 3 things:
- Cleanup unnecessary files for submission
- Cleanup unnecessary files in git
- Create an archive containing the source code and your report. The archive is located in the father directory of the project directory, and it is named by your student ID set in Makefile.
If nothing goes wrong, transfer the archive to your host. Open the archive to double check whether everything is fine. And you can manually submit this archive to the submission website.
Uploading Codes to Repositories
It's dangerous to save the only copy of your codes in the VM for the possibility of loss the code so you are asked to upload your project onto on-line code repositories. Make sure you have set the accessibility of your project as PRIVATE
so that nobody except you can get the access to your codes to protect them. Giving your codes to others is not wise because we don't know who treated so both of you will be judged treated.
{% panel style="danger", title="上传代码到在线代码仓库并设置私有" %}
不要仅仅把你的代码只在虚拟机里存一份,这很危险。每年都有学长学姐因后期虚拟机突然故障而丢失了宝贵的代码,面对成绩只能流下了悔恨的泪水。鉴于此,我们要求本学期所有同学都必须在在线代码平台私有托管自己的项目。国内可用的可以免费建立私有托管仓库的平台有如下几家:
- GitHub (https://github.com/) (GitHub 已经允许免费用户建立私有仓库)
- Coding.net (https://coding.net/)
- 码云 (https://gitee.com/)
请你自行任意选择一个平台并注册,将代码上传托管,并设置为私有项目。
{% endpanel %}
{% panel style="danger", title="务必保管好自己的代码" %}
后台使用了代码查重工具,每次提交机器查重 100%,并抽取 20% 进行人工查重。查重本着宁愿放过也不会误杀的原则进行。如果你把代码发给他人(或因没有设定私有项目保管不善,甚至被他人以某种方式窃取)因此被判定抄袭,无论处于何种主观或客观的原因,我们不会判断是谁抄袭谁,因此将进行各打五十大板的处理,因此请务必保管好自己的代码。
{% endpanel %}
To push your codes to a new remote address, you should create a new remote point named myrepo
instead of using origin
because we will often tell you to pull
new updates from the remote point origin
.
We presume that you have created a repository named ics2021
on Coding.net
and you get a address like
https:/jinhang1997/ics2021.git
and this address is the target you should push your project onto. Execute commands below
git remote add myrepo https://git.coding.net/jinhang1997/ics2021.git
git push -u myrepo master
and everything in your project on branch master
will be pushed onto the git server. The argument -u
will set myrepo
as your default remote point. Note that you cannot push changes to the origin
branch but we will tell you to pull something new form origin
to your working branch when we developed some new features or fixed some bugs.
To push other branches onto the server is the same pattern of operation like:
git push myrepo pa0
You should usually push your correct branch of project onto the server to keep your code safe.
When we tell you that you should update your project on branch paX
, what you should do is just
git pull origin paX
and the changes will be written to the paX branch then a window of vim will be allocated to ask you write something about this merge and what you should do is type :wq
to save and exit it.
RTFSC and Enjoy
If you are new to GNU/Linux and finish this tutorial by yourself, congratulations! You have learn a lot! The most important, you have learn searching the Internet and RTFM for using new tools and trouble-shooting. With these skills, you can solve lots of troubles by yourself during PAs, as well as in the future.
In PA1, the first thing you will do is to RTFSC. If you have troubles during reading the source code, go to RTFM:
- If you can not find the definition of a function, it is probably a library function. Read
man
for more information about that function. - If you can not understand the code related to hardware details, refer to the i386 manual.
By the way, you will use C language for programming in all PAs. Here is an excellent tutorial about C language. It contains not only C language (such as how to use printf()
and scanf()
), but also other elements in a computer system (data structure, computer architecture, assembly language, linking, operating system, network...). It covers most parts of this course. You are strongly recommended to read this tutorial.
Finally, enjoy the journey of PAs, and you will find hardware is not mysterious, so does the computer system! But remember:
- The machine is always right.
- Every line of untested code is always wrong.
- RTFM.
{% panel style="success", title="How will you do?" %}
Von Neumann architecture (冯·诺依曼体系) consists of these parts below:
- A processing unit that contains an arithmetic logic unit and processor registers
- A control unit that contains an instruction register and program counter
- Memory that stores data and instructions
- External mass storage
- Input and output mechanisms
If you were asked to make a simulated computer which can run a helloworld
program by yourself, how will you do that? This question has no correct answer and you can imagine it as you like. You can answer it in Chinese or English.
{% endpanel %}
{% panel style="success", title="Reminder" %}
This ends PA0. And there is no 章末必答题 in PA0.
{% endpanel %}
That's everything in PA0.7.