<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Shawn (Wanxiang) Zhong</title>
<link>https://shawnzhong.com/blog/</link>
<atom:link href="https://shawnzhong.com/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Shawn (Wanxiang) Zhong&#39;s personal website</description>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Wed, 16 Oct 2019 03:30:13 GMT</lastBuildDate>
<item>
  <title>Remote SSH to UW-Madison CS Lab with VSCode</title>
  <link>https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/</link>
  <description><![CDATA[ 




<p>This tutorial teaches you how to remote ssh to the CS Lab at University of Wisconsin-Madison using Visual Studio Code.</p>
<section id="preparation" class="level1">
<h1>Preparation</h1>
<p>1. Download Visual Studio Code from <a href="https://code.visualstudio.com">code.visualstudio.com</a> and install it. 2. Install plugin “Remote - SSH” in the Extensions view</p>
<p><img src="https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/img_5da6c8d2090b3.png" class="img-fluid"></p>
<p>3. Open settings. Check the box for&nbsp;“Remote.SSH: Lockfiles In Tmp” and “Remote.SSH: Show Login Terminal” File locking does not work when the home directory is mounted from an AFS. Setting the path of lockfiles to <code>/tmp</code> solves the problem. For more detail, see <a href="https://github.com/microsoft/vscode-remote-release/issues/540" class="uri">https://github.com/microsoft/vscode-remote-release/issues/540</a> For the second setting, VS Code should automatically prompt you to enter the password, but I had some trouble seeing the prompt before. I suggest enabling this so the password is entered in the terminal like normal ssh.</p>
<p><img src="https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/img_5da6ca38b19af.png" class="img-fluid"></p>
</section>
<section id="ssh-configuration" class="level1">
<h1>SSH Configuration</h1>
<p>1. In the Remote Explorer view, click the Configure icon at the right of “SSH TARGETS” <img src="https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/img_5dcb9c48daf17.png" class="img-fluid"> 2. Select the first SSH configuration file to edit 3. Add the CS Lab host to the file using the SSH config file format</p>
<p><img src="https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/img_5dcb9caf75f93.png" class="img-fluid"></p>
<p>For example,</p>
<pre><code>Host CSLab
 &nbsp;&nbsp;&nbsp;HostName best-linux.cs.wisc.edu
 &nbsp;&nbsp;&nbsp;User szhong</code></pre>
<p>If you want to connect to a machine that is only accessible inside the department network, you can use ProxyCommand. Take mininet as an example:</p>
<pre><code>Host Mininet
 &nbsp;&nbsp; HostName mininet-xx.cs.wisc.edu
&nbsp;&nbsp;&nbsp; User mininet
 &nbsp;&nbsp; ProxyCommand ssh -W %h:%p CSLab</code></pre>
<p>Optionally, If you are on macOS and Linux and you want to reduce how often you have to enter a password, you can add the following entry to the config file</p>
<pre><code>Host *
 &nbsp;&nbsp;&nbsp;ControlMaster&nbsp;auto
    ControlPersist  600
 &nbsp;&nbsp;&nbsp;ControlPath  ~/.ssh/%r@%h-%p
 &nbsp;&nbsp; Compression  yes</code></pre>
<p>It reuses the ssh connection when you connect to the machine next time (within the timeout), so you don’t need to enter the password or do anything.</p>
</section>
<section id="remote-ssh" class="level1">
<h1>Remote SSH</h1>
<p>You can now remote SSH to the CS Lab by clicking the newly-added entry under “SSH TARGETS” A new VSCode windows will pop up with the bottom right corner showing “SSH: CSLab”</p>
<p><img src="https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/img_5da6d30f37af3.png" class="img-fluid"></p>
<p>Open a directory and happy coding :D</p>
<p><img src="https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/img_5da6d382c42b2.png" class="img-fluid"></p>
</section>
<section id="recommended-plugins" class="level1">
<h1>Recommended Plugins</h1>
<ul>
<li>Live share is pretty helpful for pair-programming &amp; group projects. I have a tutorial about this at <a href="https://shawnzhong.com/2019/02/28/pair-programming-using-vscode-live-share/" class="uri">https://shawnzhong.com/2019/02/28/pair-programming-using-vscode-live-share/</a></li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://visualstudio.microsoft.com/wp-content/uploads/2018/11/v2-Edit-Comp_FINAL-optimized840.gif" class="img-fluid figure-img"></p>
<figcaption>Animated gif of 2 people highlighting editing code in real-time together.</figcaption>
</figure>
</div>
<ul>
<li>For xv6 projects, you can install the “Native Debug” plugin for debugging. Along with the live share plugin, you can have collaborative debugging! See previous tutorial at <a href="https://shawnzhong.com/2019/03/25/co-debug-xv6-on-windows-using-vscode/" class="uri">https://shawnzhong.com/2019/03/25/co-debug-xv6-on-windows-using-vscode/</a></li>
</ul>
<p><img src="https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/img_5c989b8a3b251.png" class="img-fluid"></p>
<ul>
<li>If you are developing C or C++, the <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools">official C/C++ plugin</a> is very useful for auto-completion, linting, etc.</li>
<li>For Java developer, I suggest using <a href="https://marketplace.visualstudio.com/items?itemName=redhat.java">Language Support for Java(TM) by Red Hat</a>. Note that all the plugins are running at the remote machine and this plugin is hungry at the memory. If the target machine has less than 1G of memory (say mininet VM), <a href="https://marketplace.visualstudio.com/items?itemName=georgewfraser.vscode-javac">Java Language Support by George Fraser</a> is preferred as it has less memory footprint.</li>
<li>The <a href="https://marketplace.visualstudio.com/items?itemName=mutantdino.resourcemonitor">Resource Monitor</a> plugin displays CPU frequency, usage, memory consumption, and battery percentage remaining in the status bar. Pretty helpful for remote machines with low resources.</li>
</ul>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/remote-ssh-to-cs-lab-with-vscode/</guid>
  <pubDate>Wed, 16 Oct 2019 03:30:13 GMT</pubDate>
</item>
<item>
  <title>Remote Desktop Access to UW-Madison CS Lab</title>
  <link>https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/</link>
  <description><![CDATA[ 




<p>This tutorial teaches you how to access the remote desktop of CS Lab computers, so you don’t have to be physically at the CS Lab to use desktop environments.</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca97cc3e82ef.png" class="img-fluid"></p>
<section id="preparation" class="level1">
<h1>Preparation</h1>
<p>For security reasons, the remote desktop service (port 3389) is not accessible from the public internet. You can either connect to the CS Department VPN or use <code>ssh</code> port forwarding. To connect to the VPN, you first need to download and install <strong>AnyConnect</strong> (not GlobalProtect) from <a href="https://www.net.wisc.edu/vpn/clients/old/?C=M;O=D">here</a>. Enter <code>dept-ra-cssc.vpn.wisc.edu</code> for the server address, and choose the group to be <code>COMP_SCI_SPLIT</code>.</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca983e375873.png" class="img-fluid"></p>
<p>Alternatively, you can use ssh port forwarding to get access to the remote desktop service. Run the following line in your command line and enter your password.</p>
<pre><code>ssh -fNT -L 3389:localhost:3389 &lt;cs-login&gt;@&lt;server&gt;</code></pre>
<p>Here, <code>-f</code> stands for background, <code>-N</code> means no remote command, <code>-T</code> disables pseudo-terminal allocation and <code>-L 3389:localhost:3389</code> forwards the 3389 port at the remote server to the local computer. You can choose <code>&lt;server&gt;</code> from the following list. Avoid using best-linux.cs.wisc.edu, since we don’t want to connect to a different server each time.</p>
<ul>
<li>rockhopper-01.cs.wisc.edu through rockhopper-09.cs.wisc.edu</li>
<li>royal-01.cs.wisc.edu through royal-30.cs.wisc.edu</li>
<li>snares-01.cs.wisc.edu through anares-10.cs.wisc.edu</li>
<li>emperor-01.cs.wisc.edu through emperor-07.cs.wisc.edu</li>
</ul>
</section>
<section id="connect-to-remote-desktop" class="level1">
<h1>Connect to Remote Desktop</h1>
<p>For Windows users, press Ctrl + R to open <code>Run</code> and run the program <code>mstsc</code>. (Or navigate to Start Menu -&gt; All Programs -&gt; Accessories -&gt; Remote Desktop Connection). This program should be preinstalled on all Windows machines. Set the remote computer to be one of the CS Lab computers above (or localhost if you are using SSH port forwarding), and enter your user name.</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca989ab19238.png" class="img-fluid"></p>
<p>For Mac users, download Microsoft Remote Desktop from <a href="https://itunes.apple.com/us/app/microsoft-remote-desktop-10/id1295203466?mt=12">here</a>, and follow the configuration for Windows.</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca98c45da7a1.png" class="img-fluid"></p>
<p>Since Microsoft Remote Desktop is also supported on Android and iOS, you can connect from your mobile devices! Here is an example at iPad with touch screen support:</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca98e69b0485.png" class="img-fluid"></p>
</section>
<section id="how-is-it-useful" class="level1">
<h1>How is it useful?</h1>
<p>There are a lot of programs pre-installed on CS Lab computers, so you don’t need to configure the environment on your local computer. Examples include:</p>
<ul>
<li>Scientific computing: Matlab, Octave, Maple</li>
<li>Data analysis: R, Jupyter Notebook, Python (with pandas, numpy, matplotlib)</li>
<li>IDE &amp; editor: VSCode, Eclipse, Atom, Sublime, Vim, EMACS</li>
<li>Other: VirtualBox, TeXstudio, Slack, LibreOffice</li>
</ul>
<p>You can start a VSCode Live Share from a CS Lab computer, and edit the code from your local computer, or share the link with your teammate for collaboration. I wrote a tutorial about <a href="https://shawnzhong.com/2019/02/28/pair-programming-using-vscode-live-share/">setting up Live Share</a>&nbsp;and <a href="https://shawnzhong.com/2019/03/25/co-debug-xv6-on-windows-using-vscode/">xv6 collaborative debug</a>. Feel free to check them out.</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca997b1c5b5e.png" class="img-fluid"></p>
<p>For debuging xv6 using VSCode, you need to install the Native Debug plugin, and your&nbsp;<code>launch.json</code> should look something like this:</p>
<pre><code>{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "gdb",
            "request": "attach",
            "name": "debug",
            "executable": "./kernel/kernel",
            "target": ":25784",               // replace this with your debug port
            "remote": true,
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText"
        }
    ]
}</code></pre>
<p>You can also install CLion on CS Lab computers, and work on the projects via remote desktop.</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca999df46f06.png" class="img-fluid"></p>
<p>Note that your home folder (e.g., <code>/u/s/z/szhong</code>) is mounded from a network location, so it will be relatively slow to run programs from there. One workaround is to install programs under <code>/nobackup</code>, which is stored on the local disc of the computer you connected to.&nbsp;To install CLion, you can download JetBrains ToolBox from <a href="https://www.jetbrains.com/toolbox/app/">here</a>, and set the installation folder to <code>/nobackup/JetBrains</code></p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca99ce1a6a4a.png" class="img-fluid"></p>
</section>
<section id="one-more-thing" class="level1">
<h1>One More Thing</h1>
<p>If you set the server address to be <code>rd.cs.wisc.edu</code>, you can connect to a Windows Remote Desktop.</p>
<p><img src="https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/img_5ca9a137d036c.png" class="img-fluid"></p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<p><a href="https://csl.cs.wisc.edu/services/remote-access/department-vpn" class="uri">https://csl.cs.wisc.edu/services/remote-access/department-vpn</a> <a href="https://csl.cs.wisc.edu/services/remote-access/windows-remote-desktop-howto" class="uri">https://csl.cs.wisc.edu/services/remote-access/windows-remote-desktop-howto</a></p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/remote-desktop-access-to-cs-lab/</guid>
  <pubDate>Sun, 07 Apr 2019 01:47:11 GMT</pubDate>
</item>
<item>
  <title>Detect Bugs using Google Sanitizers</title>
  <link>https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/</link>
  <description><![CDATA[ 




<p><a href="https://github.com/google/sanitizers">Google Sanitizers</a> are a set of dynamic code analysis tools to detect common bugs in your code, including</p>
<ul>
<li>Thread Sanitizer: detect data race, thread leak, deadlock</li>
<li>Address Sanitizer: detect buffer overflow, dangling pointer dereference</li>
<li>Leak Sanitizer: part of Address Sanitizer, detect memory leak</li>
<li>Undefined Behavior Sanitizer: detect integer overflow, float-number overflow</li>
<li>Memory Sanitizer: detect&nbsp;of uninitialized memory reads</li>
</ul>
<section id="preparation" class="level1">
<h1>Preparation</h1>
<p>For Windows users, install gcc with <a href="http://www.mingw.org/">MinGW</a>, or install <a href="https://clang.llvm.org/get_started.html">Clang</a> For Mac users, install Clang using `xcode-select –install` For Linux users, make sure you have gcc installed. Open CLion and make sure that the run button is clickable with toolchains configured correctly.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5c9bde33f1297.png" class="img-fluid"></p>
</section>
<section id="run-program-with-sanitizer" class="level1">
<h1>Run Program with Sanitizer</h1>
<p>To run a program with sanitizer, we need a special flag <code>-fsanitize</code> to the compiler.&nbsp;Common options include: <code>-fsanitize=address</code>, <code>-fsanitize=thread</code>, <code>-fsanitize=memory</code>, <code>-fsanitize=undefined</code>, <code>-fsanitize=leak</code>. A full list of options can be found <a href="https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html">here</a>. Note that it is not possible to combine more than one of the <code>-fsanitize=address</code>, <code>-fsanitize=thread</code>, and <code>-fsanitize=memory</code> checkers in the same program, so you may need to toggle the options multiple times for a comprehensive checking. For testing, let’s add the following line to the CMakeLists.txt file:</p>
<pre><code>set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,undefined -g")</code></pre>
<p>When you run the code, you should be able to see a sanitizer tab next to the console.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca3947dbabdc.png" class="img-fluid"></p>
</section>
<section id="thread-sanitizer-example" class="level1">
<h1>Thread Sanitizer Example</h1>
<p>Here is a poorly-written multithreading program:</p>
<pre><code>int counter = 0;
pthread_mutex_t lock;

void *inc() {
  pthread_mutex_lock(&amp;lock); // lock not initiazlied
  counter++; // thread contention
  pthread_mutex_unlock(&amp;lock);
  return NULL;
}

void thread_bugs() {
  pthread_t tid;
  for (int i = 0; i &lt; 2; ++i)
    pthread_create(&amp;tid, NULL, inc, NULL);
  printf("%d", counter); // print the result before join
  pthread_join(tid, NULL); // the first thread is not joined
}</code></pre>
<p>Add the following line to the CMakeLists.txt to enable the Thread Sanitizer</p>
<pre><code>set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -g")</code></pre>
<p>When the program is executing, the sanitizer will generate a report for thread-related bugs. Be aware that your program might run significantly slower with sanitizers enabled. The sanitizer noticed that two threads are reading/writing to the same memory location at the line <code>counter++;</code>, since we the locked is used before initialized.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca476afa1c7e.png" class="img-fluid"></p>
<p>There is also a data race between counter++ and the print statement since the main thread did not wait for one of the child threads.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca4781c6713b.png" class="img-fluid"></p>
<p>Finally, there is a thread leak by the same reason above.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca4784b16ded.png" class="img-fluid"></p>
</section>
<section id="address-sanitizer-example" class="level1">
<h1>Address Sanitizer Example</h1>
<p>To enable the Address Sanitizer, you need to add the following line to the CMakeLists.txt</p>
<pre><code>set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -g")</code></pre>
<p>It helps you detect heap overflow, which may happen when you incorrectly calculated the size.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca479f2e227d.png" class="img-fluid"></p>
<p>Here is an example of overflowing a stack-allocated array</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca47a42af7e5.png" class="img-fluid"></p>
<p>The Address Sanitizer also checks for using freed pointers. Note that it shows you where is memory is allocated and freed.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca47aa94de33.png" class="img-fluid"></p>
<p>Here is a silly example of freeing the same memory twice, but it will be less noticeable when different pointers are pointing to the same heap location.</p>
<p><img src="https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/img_5ca47b44a5a03.png" class="img-fluid"></p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<p><a href="https://clang.llvm.org/docs/UsersManual.html" class="uri">https://clang.llvm.org/docs/UsersManual.html</a> <a href="https://www.jetbrains.com/help/clion/google-sanitizers.html" class="uri">https://www.jetbrains.com/help/clion/google-sanitizers.html</a></p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/detect-bugs-using-google-sanitizers/</guid>
  <pubDate>Wed, 03 Apr 2019 04:28:01 GMT</pubDate>
</item>
<item>
  <title>Analyze Execution Time with CLion</title>
  <link>https://shawnzhong.com/blog/analyze-execution-time-with-clion/</link>
  <description><![CDATA[ 




<p>CLion has CPU profiler integrated so that you can analyze the execution time for each function and use the information for code optimization. From the screenshot below, you can see that most of the execution time (94.68%) is spent on <code>MR_Emit</code>, so I can try to spend my time on that function instead of the reduce part, which only takes 4.82% of the execution time.</p>
<p><img src="https://shawnzhong.com/blog/analyze-execution-time-with-clion/img_5c9bd6cfa360b.png" class="img-fluid"></p>
<p>Note: CPU profiler won’t work on Windows. The computer at CS Lab supports profiler (<a href="https://perf.wiki.kernel.org/index.php/Main_Page">Perf</a>), but we don’t have the permission to run that for some reason.</p>
<section id="preparation" class="level1">
<h1>Preparation</h1>
<p>For Mac users, run the following command to install Command Line Tools, which includes llvm (similar to gcc) and lldb (similar to gdb).</p>
<pre><code>xcode-select --install</code></pre>
<p>For Linux users, make sure you have gcc and perf installed. They should be pre-installed for Ubuntu. If not, you should be able to figure it out :) Open CLion and make sure that the run button is clickable with toolchains configured correctly.</p>
<p><img src="https://shawnzhong.com/blog/analyze-execution-time-with-clion/img_5c9bde33f1297.png" class="img-fluid"></p>
</section>
<section id="run-cpu-profiler" class="level1">
<h1>Run CPU Profiler</h1>
<p>Run the profiler by clicking the run button next to your main function and choose Profile(, or click the button next to stop at the toolbar)</p>
<p><img src="https://shawnzhong.com/blog/analyze-execution-time-with-clion/img_5c9bdeb664a6e.png" class="img-fluid"></p>
<p>The program will run in profile mode, and a report will be generated by the tool. You can open the report by clicking the Profiler panel on the bottom left corner. In the Flame Chart tab, you can see a visual representation of the execution time. You can start from the bottom and move up, following the code flow from parent to child functions.</p>
<p><img src="https://shawnzhong.com/blog/analyze-execution-time-with-clion/img_5c9bdfc265766.png" class="img-fluid"></p>
<p>In the Call Tree tab, you can see a tree of function calls with the corresponding execution time.</p>
<p><img src="https://shawnzhong.com/blog/analyze-execution-time-with-clion/img_5c9be0960b59b.png" class="img-fluid"></p>
<p>In the Methods List tab, a list of methods is presented ordered by decreasing execution time.</p>
<p><img src="https://shawnzhong.com/blog/analyze-execution-time-with-clion/img_5c9be0db8eb3f.png" class="img-fluid"></p>
</section>
<section id="reference" class="level1">
<h1>Reference</h1>
<p><a href="https://www.jetbrains.com/help/clion/cpu-profiler.html" class="uri">https://www.jetbrains.com/help/clion/cpu-profiler.html</a> <a href="http://www.brendangregg.com/perf.html" class="uri">http://www.brendangregg.com/perf.html</a> <a href="https://perf.wiki.kernel.org/index.php/Main_Page" class="uri">https://perf.wiki.kernel.org/index.php/Main_Page</a></p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/analyze-execution-time-with-clion/</guid>
  <pubDate>Wed, 27 Mar 2019 15:46:17 GMT</pubDate>
</item>
<item>
  <title>Co-debug xv6 on Windows using VSCode</title>
  <link>https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/</link>
  <description><![CDATA[ 




<p>This tutorial teaches you how to <strong>set up collaborative debugging</strong> for xv6 on Windows using VSCode. If you haven’t installed WSL and/or toolchains for compiling xv6, please refer to my previous tutorial: <a href="https://shawnzhong.com/2019/02/26/compile-xv6-locally-on-windows/">Compile xv6 Locally on Windows</a>. Also, make sure that you have <code>gdb</code> installed on Windows (not just in WSL). <img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c98a1aa594ab.png" class="img-fluid"> Note: This tutorial is specifically focused on Windows users. Some configuration might not work on other operating systems.</p>
<section id="configure-build-task" class="level1">
<h1>Configure Build Task</h1>
<p>To simplify the workflow, we can set up a <a href="https://code.visualstudio.com/docs/editor/tasks">task</a> in VSCode for building xv6. First, open the folder for xv6 in VSCode and open “<strong>Terminal &gt; Configure Tasks…</strong>” in the menu.</p>
<p><img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c98938d01954.png" class="img-fluid"></p>
<p>On the popup window, choose “<strong>Create tasks.json file from template</strong>” and copy the following content to <code>tasks.json</code>. (Equivalently, you can directly create <code>tasks.json</code> under <code>./vscode</code> folder and past the lines below)</p>
<pre><code>{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile xv6 and run in debug mode",
            "command": "bash",
            "args": [
                "-c",
                "make &amp;&amp; make qemu-nox-gdb"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "new",
                "showReuseMessage": true,
                "clear": true
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}</code></pre>
<p>This file creates a default build task that compiles xv6 and runs it in debug mode.</p>
<p>To test whether the task is configured correctly, <strong>press Ctrl+Shift+B</strong> (or open “Terminal &gt; Run Build Task…” in the menu). You should be able to see that the command <code>make &amp;&amp; make qemu-nox-gdb</code> is executed in the bash of Linux subsystem.</p>
<p><img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c98975079799.png" class="img-fluid"></p>
</section>
<section id="add-debug-configuration" class="level1">
<h1>Add Debug Configuration</h1>
<p>To enable remote debugging, you need to install a plugin called <strong>Native Debug</strong>. You can install it in the Extensions tab on the left (or press Ctrl+P and run <code>ext install webfreak.debug</code>)</p>
<p><img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c9898d585d24.png" class="img-fluid"></p>
<p>After Native Debug is installed, open “<strong>Debug &gt; Open Configurations</strong>” in the menu. and past the following lines to <code>launch.json</code>. (Or equivalently, create <code>launch.json</code> under <code>./vscode</code> folder and past the lines below)</p>
<pre><code>{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "gdb",
            "request": "attach",
            "name": "debug xv6",
            "executable": "kernel/kernel",
            "target": ":26000",
            "remote": true,
            "cwd": "${workspaceRoot}",
        }
    ]
}</code></pre>
<p>To test whether debugging is configured correctly, add a breakpoint at <code>kernel/proc.c:266</code> by clicking the space left to the line number and <strong>press F5</strong> for debugging (or open “Debug &gt; Start Debugging” in the menu). You should be able to see the program stops at the specified line with variables and call stack shown at the left.</p>
<p><img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c989b8a3b251.png" class="img-fluid"></p>
<p>Congratulations! You just set up the debugger for xv6! Be familiar with the first 4 icons in the floating toolbar. They are Continue/Resume, Step Over, Step Into, and Step Out respectively.</p>
<p><img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c989c7116a8a.png" class="img-fluid"></p>
<p>You can also watch a variable in the WATCH panel, such as <code>ptable-&gt;proc[0]-&gt;name</code>. To set a conditional breakpoint, open “Debug &gt; New Breakpoint &gt; Conditional Breakpoint” in the menu.</p>
<p><img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c989d7f16f42.png" class="img-fluid"></p>
</section>
<section id="collaborative-debugging" class="level1">
<h1>Collaborative Debugging</h1>
<p>If you haven’t heard about VSCode Live Share or haven’t installed it, you can check my previous tutorial: <a href="https://shawnzhong.com/2019/02/28/pair-programming-using-vscode-live-share/">Pair Programming using VSCode</a>. Click the live share button on the bottom of VSCode, and share the invitation like to your partner. Your partner can join the debug session and control the debugging process by clicking the button on the toolbar. Note that the screenshot below is on macOS. The variables, call stack, and breakpoints are all synced to this computer.</p>
<p><img src="https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/img_5c98a1aa594ab.png" class="img-fluid"></p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<p><a href="https://docs.microsoft.com/en-us/visualstudio/liveshare/use/vscode" class="uri">https://docs.microsoft.com/en-us/visualstudio/liveshare/use/vscode</a> <a href="https://code.visualstudio.com/docs/editor/debugging" class="uri">https://code.visualstudio.com/docs/editor/debugging</a> <a href="https://code.visualstudio.com/docs/editor/tasks" class="uri">https://code.visualstudio.com/docs/editor/tasks</a></p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/co-debug-xv6-on-windows-using-vscode/</guid>
  <pubDate>Mon, 25 Mar 2019 03:45:40 GMT</pubDate>
</item>
<item>
  <title>Pair Programming using VSCode</title>
  <link>https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/</link>
  <description><![CDATA[ 




<p>As some of you may already know, we will be allowed to work in pair going forward from P3. I found VSCode Live Share pretty useful for group programming (especially face-to-face paring programming). Roughly speaking, VSCode Live Share is Google Docs for Coding. It supports</p>
<ul>
<li>Live editing</li>
<li>Focus and Follow</li>
<li>Audio calls</li>
<li>Shared terminal</li>
<li>Shared servers (not covered in this tutorial)</li>
<li>Group debugging (not covered in this tutorial)</li>
</ul>
<p>In this tutorial, I will walk you through how to set up VS Code Live Share for code collaboration. <img src="https://visualstudio.microsoft.com/wp-content/uploads/2018/11/v2-Edit-Comp_FINAL-optimized840.gif" class="img-fluid" alt="Animated gif of 2 people highlighting editing code in real-time together."></p>
<section id="preparation" class="level1">
<h1>Preparation</h1>
<p>1. Download Visual Studio Code from <a href="https://code.visualstudio.com/">code.visualstudio.com</a> and install it. (You can also use Visual Studio if you already installed it, but this tutorial will be focused on VSCode.) 2. Open a folder that you want to work on. I will use xv6 as an example.</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c7774b37589c.png" class="img-fluid"></p>
<p>3. Open the extension tab on the left, search for VS Live Share Extension Pack, and click install. Restart the editor if prompted.</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c77738aa321f.png" class="img-fluid"></p>
</section>
<section id="start-a-live-share-session" class="level1">
<h1>Start a Live Share Session</h1>
<p>1. Click on Live Share at the bottom left. An invitation link will be copied to the clipboard.</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c777586afc7e.png" class="img-fluid"></p>
<p>2. Send the link to your partner. He/she will be able to open the link in VS Code and join the session (possibly required to login via Microsoft account or GitHub).</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c777962e1ffa.png" class="img-fluid"></p>
<p>3. Once your partner joins the live share, you should be able to see his/her cursor in the editor.</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c777ad04b9f4.png" class="img-fluid"></p>
</section>
<section id="some-useful-tricks-and-tips" class="level1">
<h1>Some Useful Tricks and Tips</h1>
<p>By default, your partner will follow your cursor when you are moving. You can disable it in the Live Share tab (see screenshot below).</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c777bae92e35.png" class="img-fluid"></p>
<p>During VSCode Live Share, you can open an integrated terminal by pressing Control + `, and the terminal will be shared across the session. It’s read-only by default, but you can change the permission in the Live Share tab as well.</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c777eaa5954e.png" class="img-fluid"></p>
<p>I didn’t use this feature so often, but you can actually start an audio call in VSCode.</p>
<p><img src="https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/img_5c77833110d80.png" class="img-fluid"></p>
</section>
<section id="reference" class="level1">
<h1>Reference</h1>
<p><a href="https://code.visualstudio.com/blogs/2017/11/15/live-share" class="uri">https://code.visualstudio.com/blogs/2017/11/15/live-share</a> <a href="https://moduscreate.com/blog/remote-pair-programming-made-easy-with-vscode-live-share/" class="uri">https://moduscreate.com/blog/remote-pair-programming-made-easy-with-vscode-live-share/</a> <a href="https://visualstudio.microsoft.com/services/live-share/" class="uri">https://visualstudio.microsoft.com/services/live-share/</a></p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/pair-programming-using-vscode-live-share/</guid>
  <pubDate>Thu, 28 Feb 2019 00:25:38 GMT</pubDate>
</item>
<item>
  <title>Compile xv6 locally on Windows</title>
  <link>https://shawnzhong.com/blog/compile-xv6-locally-on-windows/</link>
  <description><![CDATA[ 




<p>In this tutorial, you will learn how to compile and run xv6 locally on Windows 10 using WSL (Windows Subsystem for Linux) <img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c75079abd37f-1-1.png" class="img-fluid"></p>
<section id="enable-wsl-windows-subsystem-for-linux" class="level1">
<h1>Enable WSL (Windows Subsystem for Linux)</h1>
<p>1. Search for “Turn Windows features on or off” in the start menu and open it. 2. Select “Windows Subsystem for Linux” 3. Restart your computer when prompted</p>
<p><img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c74f3eb8caa0.png" class="img-fluid"></p>
</section>
<section id="install-linux-distribution" class="level1">
<h1>Install Linux Distribution</h1>
<p>1. Open this link <a href="https://aka.ms/wslstore" class="uri">https://aka.ms/wslstore</a> on Windows. You will be redirected to an installation page in Microsoft Store. (If this link doesn’t work, you can manually open Microsoft Store and search for Ubuntu.) 2. Download Ubuntu. This usually takes about 5-10 minutes depending on your Internet speed.</p>
<p><img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c74f5a9103b2.png" class="img-fluid"></p>
<p>3. Once Ubuntu is downloaded, you should be able to see it in the Start Menu. Launch the program. It will take some time to set up the system for the first time. 4. After that, you will be prompted to create a new user for the subsystem. This does not need to match your Windows username or CS account. Once the account is created, you should be able to run the familiar Linux commands inside the subsystem.</p>
<p><img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c7503cdc91cc.png" class="img-fluid"></p>
<p>Note that the working directory is <code>/home/&lt;username&gt;</code>. If you want to access the disk on your Windows machine, you can cd to <code>/mnt/&lt;drive letter&gt;/&lt;path&gt;</code>.</p>
</section>
<section id="install-toolchains-and-qemu" class="level1">
<h1>Install Toolchains and QEMU</h1>
<p>Run the following command in bash. (Note: You can right-click the terminal window to paste the command.)</p>
<pre><code>sudo apt-get update
sudo apt-get install git nasm build-essential qemu gdb</code></pre>
<p>You will be prompted to enter yes to confirm installation. The installation process takes about 30 minutes.</p>
<p><img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c7505c9a4b8e.png" class="img-fluid"></p>
<p>After apt-get is done, you should be able to run commands like <code>gcc</code>, <code>gdb</code>, <code>qemu</code>, etc.</p>
<p><img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c75079abd37f.png" class="img-fluid"></p>
</section>
<section id="compile-xv6" class="level1">
<h1>Compile xv6</h1>
<p>Change the working directory to your Windows machine and copy xv6 from CS Lab</p>
<pre><code>cd /mnt/c/Users/ShawnZhong/Downloads
scp -r szhong@best-linux.cs.wisc.edu:/p/course/cs537-shivaram/xv6-sp19 .</code></pre>
<p><img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c750abc8d57e.png" class="img-fluid"></p>
<p>You can then use the following command to build and run xv6 locally on Windows.</p>
<pre><code>make &amp;&amp; make qemu-nox QEMU=/usr/bin/qemu-system-i386</code></pre>
<p><img src="https://shawnzhong.com/blog/compile-xv6-locally-on-windows/img_5c750b692df13.png" class="img-fluid"></p>
<p>Congratulations! You have xv6 running locally on your Windows machine. Since everything is on your local computer, you don’t need to sync the files to the server anymore! It also makes the debug experience much better since the debugging port is open on your local computer (i.e., no more port forwarding).</p>
</section>
<section id="reference" class="level1">
<h1>Reference</h1>
<p><a href="https://gcallah.github.io/OperatingSystems/xv6Install.html" class="uri">https://gcallah.github.io/OperatingSystems/xv6Install.html</a> <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10" class="uri">https://docs.microsoft.com/en-us/windows/wsl/install-win10</a></p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/compile-xv6-locally-on-windows/</guid>
  <pubDate>Tue, 26 Feb 2019 03:19:59 GMT</pubDate>
</item>
<item>
  <title>xv6 Remote Debug using CLion</title>
  <link>https://shawnzhong.com/blog/xv6-remote-debug-using-clion/</link>
  <description><![CDATA[ 




<p>This tutorial will teach you how to debug xv6 remotely using CLion. In the screenshot below, I have:</p>
<ul>
<li><strong>xv6 running remotely</strong> on a&nbsp;CS Lab machine</li>
<li>gdb connected to xv6 with <strong>frames</strong> and <strong>variables</strong> showed</li>
<li><strong>code edited on the local machine</strong> and automatically synced</li>
<li>files on CS Lab machine listed</li>
<li><strong>documentation</strong> (and <strong>declared location</strong>) shown for the selected function</li>
</ul>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68d8d85a963.png" class="img-fluid"></p>
<p>If you haven’t installed CLion or haven’t set up file synchronization, you can read my previous tutorial:&nbsp;<a href="https://shawnzhong.com/2019/02/10/tutorial-setting-up-coding-environment-for-cs-537/">Setting up Coding Environment for CS 537</a>. This tutorial is specifically focused on debugging&nbsp;xv6. Since xv6 is running in QEMU (instead of a host machine), we have to manually connect gdb to the gdb server, which complicates the setup process. I have another tutorial about debugging general&nbsp;C programs here:&nbsp;<a href="https://shawnzhong.com/2019/02/10/tutorial-remote-debug-and-execution-using-clion/">Remote Debug and Execution&nbsp;using CLion</a>. Prof.&nbsp;Shivaram has a great tutorial about using gdb to debug xv6 during the discussion section. You can find the recorded video on <a href="https://piazza.com/class/jql5yyu0x8a12k?cid=323">this piazza page</a>. I highly recommend to watch the video first,&nbsp;but this tutorial&nbsp;<strong>doesn’t require any prior knowledge about gdb</strong>.</p>
<section id="preparation" class="level1">
<h1>1. Preparation</h1>
<p>Note: You can skip this part if you are compiling xv6 on your local computer or you are already in the CS Lab. 1. SSH to a CS Lab machine. Please <strong>use a specific server</strong> in the following list instead of best-linux.cs.wisc.edu. (best-linux.cs.wisc.edu is the Linux machine in the CS labs that currently has the fewest number of other users logged in to it. We don’t want to use this because it might change over time.)</p>
<ul>
<li>rockhopper-01.cs.wisc.edu through rockhopper-09.cs.wisc.edu</li>
<li>royal-01.cs.wisc.edu through royal-30.cs.wisc.edu</li>
<li>snares-01.cs.wisc.edu through anares-10.cs.wisc.edu</li>
<li>emperor-01.cs.wisc.edu through emperor-07.cs.wisc.edu</li>
</ul>
<p>2. <strong>Copy</strong> xv6 to your working directory on a CS Lab machine,&nbsp;<strong>build</strong> the source, and <strong>run</strong> xv6 in debug mode.</p>
<pre><code>cp -r /p/course/cs537-shivaram/xv6-sp19 .
make
make qemu-nox-gdb</code></pre>
<p>3. <strong>Record the port number</strong> for gdb server. I always have 25784, but it might be different for you. (What qemu-nox-gdb does is that it runs a <a href="http://man7.org/linux/man-pages/man1/gdbserver.1.html">gdb server</a> at localhost:25784 so that gdb can later connect to this server to&nbsp;debug the code.)</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68e5e0e169f.png" class="img-fluid"></p>
<p>4. Open CLion and <strong>sync the folder to your local machine</strong>. Please refer to <a href="https://shawnzhong.com/2019/02/10/tutorial-setting-up-coding-environment-for-cs-537/">this tutorial</a> if you don’t know how to do this.</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68e4a255807.png" class="img-fluid"></p>
<p>5. Open <strong>another</strong> terminal window, and use the following command to forward the port (recorded in step 2) on the remote server to your local machine. (This is called <a href="https://www.ssh.com/ssh/tunneling/example">SSH tunneling or SSH port forwarding</a> so that we can access the gdb server running&nbsp;on the CS Lab machine via localhost:&lt;port&gt; on your local machine) Note: Please <strong>leave the terminal window open at the background in the following steps.</strong></p>
<pre><code>ssh -L &lt;port&gt;:localhost:&lt;port&gt; &lt;username&gt;@&lt;server&gt;.cs.wisc.edu</code></pre>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68ec9d795b0.png" class="img-fluid"></p>
</section>
<section id="configure-clion" class="level1">
<h1>2. Configure CLion</h1>
<p>1. Click “<strong>Edit configuration</strong>” on the top-right 2. Add new configuration of type&nbsp;“<strong>GDB Remote Debug</strong>” 3. Set ’<strong>target remote’ args</strong> to be localhost:&lt;port&gt; 4. Choose the <strong>symbol file</strong> to be &lt;local_folder&gt;/kernel/kernel. (If you cannot find this file, build xv6 again and download the built folder to your local machine) 5. Set the <strong>system root</strong> to be&nbsp;&lt;local_folder&gt; 6. Add <strong>path mappings</strong> between &lt;local_folder&gt; and &lt;remote_folder&gt;</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68ebd5abe29.png" class="img-fluid"></p>
</section>
<section id="run-gdb-remote-debug" class="level1">
<h1>3. Run GDB Remote Debug</h1>
<p>For testing, let’s create a breakpoint at kernel/proc.c:261 by clicking the space next to the line number (same as “b kernel/proc.c:261” in gdb). Click the debug button on the top-right corner. You should be able to see a debug tab open on the bottom left, which shows you the frames (same as “info frame” in gdb) and variables (same as “info variables” and “print &lt;var_name&gt;” in gdb). If the debugger doesn’t stop at the breakpoint, please go to section 5: Troubleshooting.</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68ee7a573d3.png" class="img-fluid"></p>
<p>Here are some useful commands that you can use during debugging</p>
<ul>
<li>Step over: execute next line of code. Will not enter functions. (same as “n” in gdb)</li>
<li>Step into: step to next line of code. Will step into a function.&nbsp;(same as “s” in gdb)</li>
<li>Step out: run until current stack frame finishes (same as “finish” in gdb)</li>
<li>Continue:&nbsp;continue running your program (same as “c” in gdb)</li>
<li>Pause:&nbsp;pause the execution of a running program (same as Ctrl+C in gdb)</li>
</ul>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68f0e59b3ed-e1550381325764.png" class="img-fluid"></p>
<p>To create a&nbsp;conditional breakpoint, right click on a normal breakpoint to add the condition.</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68f2fa6c5ce.png" class="img-fluid"></p>
<p>You can also directly type commands into gdb by switching to the gdb tab (when the program is paused)</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68f3855cb58.png" class="img-fluid"></p>
</section>
<section id="some-useful-tips" class="level1">
<h1>4. Some Useful Tips</h1>
<p>For better code analysis and navigation, you can mark your xv6 folder as Project Sources and Headers, and add the line “include_directories(&lt;relative_path_to_include_folder&gt;)” to CMakeLists.txt</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c68f4b3f1a38.png" class="img-fluid"></p>
<p>Once the previous step is done, you can press F1 on a function/variable to see the documentation. There is also an option for showing quick documentation on mouse move at “Editor - General - Other”.</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c6a02fb6caa9.png" class="img-fluid"></p>
</section>
<section id="troubleshooting" class="level1">
<h1>5. Troubleshooting</h1>
<p>First, make sure that the debugger is connected to the remote server. In the debug tab, you should see something like “Debugger connected to localhost:25784”. If the message doesn’t show up, you need to check whether</p>
<ul>
<li>xv6 is running in debug mode (<code>make qemu-nox-gdb</code>)</li>
<li>SSH tunnel is connected to the same server as xv6 and forwarding the right port number (<code>ssh -L &lt;port&gt;:localhost:&lt;port&gt; &lt;username&gt;@&lt;server&gt;</code>)</li>
</ul>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c7641c8790cc.png" class="img-fluid"></p>
<p>If the debugger is successfully connected to xv6, but it still doesn’t stop at the breakpoints, click the pause button at the left. If the debugger highlighted some code in the xv6 source folder, but the line number is wired, <strong>downloading the compiled xv6 from CS Lab server again</strong> should resolve this problem. This is because the symbol table on your local computer is not up to date after you modified the code and recompiled it on the remote server. If you got an xv6 (disassembly) window with commented assembly code, switch to the GDB tab and enter <code>info sources</code>. Make sure that the files can be found on your local computer. If you moved the xv6 source folder after compilation, try <code>make clean</code> and compile again.</p>
<p><img src="https://shawnzhong.com/blog/xv6-remote-debug-using-clion/img_5c7646378a158.png" class="img-fluid"></p>
<p>If the debugger is still not working, feel free to contact me by email :)</p>
</section>
<section id="reference" class="level1">
<h1>6. Reference</h1>
<p><a href="http://man7.org/linux/man-pages/man1/gdbserver.1.html" class="uri">http://man7.org/linux/man-pages/man1/gdbserver.1.html</a> <a href="https://sourceware.org/gdb/onlinedocs/gdb/Server.html" class="uri">https://sourceware.org/gdb/onlinedocs/gdb/Server.html</a> <a href="https://www.jetbrains.com/help/clion/debugging-code.html" class="uri">https://www.jetbrains.com/help/clion/debugging-code.html</a> <a href="https://www.jetbrains.com/clion/features/run-and-debug.html" class="uri">https://www.jetbrains.com/clion/features/run-and-debug.html</a></p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/xv6-remote-debug-using-clion/</guid>
  <pubDate>Sat, 16 Feb 2019 23:40:38 GMT</pubDate>
</item>
<item>
  <title>Remotely Debug C Program using CLion</title>
  <link>https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/</link>
  <description><![CDATA[ 




<p>I believed that most C programmers have encountered&nbsp;segmentation fault before. In this case, we have to write a lot of print statements or use the command-line tool gdb to find where is wrong. Either way, it’s time-consuming&nbsp;to debug your code only using those basic tools. This tutorial walks you through how to debug on CLion locally while having your code compile and run on remote CS Lab machines. <img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c60a55bafa55.png" class="img-fluid"> Note: If you haven’t installed CLion before, you can read my previous&nbsp;tutorial <a href="https://shawnzhong.com/2019/02/10/tutorial-setting-up-coding-environment-for-cs-537/">here</a></p>
<section id="configure-remote-toolchain" class="level1">
<h1>1. Configure Remote Toolchain</h1>
<p>A toolchain is a set of development tools including compiler, linker, debugger, etc.&nbsp;We want to use set up the remote toolchain for our project so that we can compile and run codes remotely. 1. Open setting and navigate to <strong>Build, Execution, Deployment &gt; Toolchains</strong> 2. Click&nbsp;<strong>add</strong>&nbsp;at the bottom 3. Select <strong>Remote Host</strong> instead of System at the right of <strong>Name</strong> 4. Click&nbsp;<strong>open</strong>&nbsp;in the&nbsp;<strong>Credentials</strong>&nbsp;line 5. Fill in the information for the remote host</p>
<p><strong>Use a specific server</strong> for the host (e.g.&nbsp;royal-10.cs.wisc.edu) instead of best-linux.cs.wisc.edu, since we don’t want the project to be run on a different machine each time.</p>
<p>Here is a list of Linux server you can use:</p>
<ul>
<li>rockhopper-01.cs.wisc.edu through rockhopper-09.cs.wisc.edu</li>
<li>royal-01.cs.wisc.edu through royal-30.cs.wisc.edu</li>
<li>snares-01.cs.wisc.edu through anares-10.cs.wisc.edu</li>
<li>emperor-01.cs.wisc.edu through emperor-07.cs.wisc.edu</li>
</ul>
<p>6. If you have configured toolchain before, you need to drag the remote host to the top of the list to set it as the default toolchain.</p>
<p><img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c609ee9d8873.png" class="img-fluid"></p>
<p>Note 1: After you clicked OK, It may take&nbsp;some time for CLion to retrieve&nbsp;some information from the remote server. You can check the process by clicking the status information at the bottom of CLion. <strong>Please wait for that until you continue.</strong> Note 2: If cmake&nbsp;reports “<strong>CMake 3.13 or higher is required. You are running version 3.10.2</strong>”, you need to change the first line of CMakeLists.txt to “cmake_minimum_required(VERSION 3.10.2)”</p>
<p><img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c60a246264f5.png" class="img-fluid"></p>
<p>Now you should be able to compile and run your code remotely on CS Lab machine by clicking the run button at the top right corner of CLion.</p>
<p><img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c60a37e31af5.png" class="img-fluid"></p>
</section>
<section id="remote-debug-on-cs-lab-machine" class="level1">
<h1>2. Remote Debug on CS Lab Machine</h1>
<p>Now you can click on the line number to add a breakpoint and click the debug button to debug your code. If you are encountering segmentation fault, the debugger will stop at the line where segfault&nbsp;happens.</p>
<p><img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c60a55bafa55.png" class="img-fluid"></p>
</section>
<section id="some-useful-tips" class="level1">
<h1>3.&nbsp;Some Useful Tips</h1>
<p>1. Make good use of the following&nbsp;three buttons on the debug tab.</p>
<ul>
<li>Step over: go to the next line</li>
<li>Step into: look into the function call</li>
<li>Step out: get out of this function</li>
</ul>
<p><img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c60a7d1aadc4.png" class="img-fluid"></p>
<p>2. You can add new watches to the variable list to check for a specific variable <img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c60a6181044d.png" class="img-fluid"> 3. Press F1 on a system function to show the documentation. You can also dock the documentation popup as a permanent&nbsp;tab on the right.</p>
<p><img src="https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/img_5c60a82f5ca8d.png" class="img-fluid"></p>
</section>
<section id="references" class="level1">
<h1>4. References</h1>
<ul>
<li><a href="https://www.jetbrains.com/help/clion/debugging-code.html" class="uri">https://www.jetbrains.com/help/clion/debugging-code.html</a></li>
<li><a href="https://www.jetbrains.com/help/clion/viewing-inline-documentation.html" class="uri">https://www.jetbrains.com/help/clion/viewing-inline-documentation.html</a></li>
</ul>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/tutorial-remote-debug-and-execution-using-clion/</guid>
  <pubDate>Sun, 10 Feb 2019 16:40:08 GMT</pubDate>
</item>
<item>
  <title>Setting up CLion for CS 537</title>
  <link>https://shawnzhong.com/blog/tutorial-setting-up-coding-environment-for-cs-537/</link>
  <description><![CDATA[ 




<p>As the projects get larger and more complicated, I think it is important to have a good coding environment for this course. I’d like to share with you how I use CLion as the main IDE for&nbsp;C programming. I recommend CLion for the following reasons:</p>
<ul>
<li><strong>Sync changed files</strong>&nbsp;to the remote server. You don’t need to manually copy and paste the modified code to the CS lab.</li>
<li>Powerful&nbsp;<strong>code analysis</strong>&nbsp;and&nbsp;<strong>correction suggestion</strong>. You can eliminate most of the compilation errors&nbsp;while you are editing the file.</li>
<li>Built-in&nbsp;<strong><code>ssh</code>&nbsp;and&nbsp;<code>scp</code>&nbsp;support</strong>&nbsp;with a handy graphical interface. For Windows users, you don’t need to install PuTTY/WinSCP/WSL to connect to the CS lab.</li>
<li>Quick&nbsp;<strong>search</strong>&nbsp;and&nbsp;<strong>navigation</strong>. It’s much easier to search files in the project folder (compared to&nbsp;<code>grep</code>) and navigate between the&nbsp;declaration and definition among files (compared to vim or&nbsp;<code>emacs</code>).</li>
</ul>
<p>Please feel free to post any questions or comments down below&nbsp;:) <img src="https://piazza.com/redirect/s3?bucket=uploads&amp;prefix=attach%2Fjql5yyu0x8a12k%2Fj7808xo55wp4ef%2Fjro0j3dvkmnq%2FWechatIMG4913.png" class="img-fluid"></p>
<section id="installation" class="level1">
<h1>Installation</h1>
<p>You can download CLion&nbsp;from this link:&nbsp;<a href="https://www.jetbrains.com/clion/">https://www.jetbrains.com/clion/</a>.&nbsp;It is commercial software, but you can get the student license for free here:&nbsp;<a href="https://www.jetbrains.com/student/">https://www.jetbrains.com/student/</a>. The installer&nbsp;will ask you to configure toolchains for CLion. If you have not installed any toolchains &nbsp;(gcc/llvm/MinGW/Cygwin) before, you can just skip this part, since we are probably not going to compile or run the code on our local machine. <img src="https://piazza.com/redirect/s3?bucket=uploads&amp;prefix=attach%2Fjql5yyu0x8a12k%2Fj7808xo55wp4ef%2Fjrnzkf0mr0gu%2FWechatIMG4909.png" class="img-fluid"> You can follow the default configuration to finish the installation. BTW, If you are a big fan of&nbsp;<code>vim</code>, you can enable the IdeaVim plugin on the last screen.</p>
</section>
<section id="connect-to-the-remote-server" class="level1">
<h1>Connect to the Remote Server</h1>
<p>After you created your first project,&nbsp;go to “Tools - Deployment - Configuration”. Click the “+” button and select “SFTP” as the type. Under the connection tab, fill in the “Host”, “User name”, “Password” field, check the box for “Save password” (if you want to), and click “Autodetect” for “Root path”. <img src="https://piazza.com/redirect/s3?bucket=uploads&amp;prefix=attach%2Fjql5yyu0x8a12k%2Fj7808xo55wp4ef%2Fjrnzwvf7opyj%2FWechatIMG4910.png" class="img-fluid"> Under the mappings tab, choose your deployment path on the server (I put all files under&nbsp;<code>/private/CS537</code>. You can choose whatever path you want, but don’t put it under&nbsp;<code>/public</code>) <img src="https://piazza.com/redirect/s3?bucket=uploads&amp;prefix=attach%2Fjql5yyu0x8a12k%2Fj7808xo55wp4ef%2Fjro01qvvmvh3%2FWechatIMG4911.png" class="img-fluid"> Now the remote server is configured. You can click “Tools - Start SSH session” to connect to the CS lab via&nbsp;<code>ssh</code> <img src="https://piazza.com/redirect/s3?bucket=uploads&amp;prefix=attach%2Fjql5yyu0x8a12k%2Fj7808xo55wp4ef%2Fjro0j3dvkmnq%2FWechatIMG4913.png" class="img-fluid"></p>
</section>
<section id="configure-file-synchronization" class="level1">
<h1>Configure File Synchronization</h1>
<p>If you select your project folder on the left&nbsp;and go to “Tools - Deployment”, you can see the following menu. <img src="https://piazza.com/redirect/s3?bucket=uploads&amp;prefix=attach%2Fjql5yyu0x8a12k%2Fj7808xo55wp4ef%2Fjro0dakozsai%2FWechatIMG4912.png" class="img-fluid"> You&nbsp;can enable the “Automatic Upload” option so that local changes will be uploaded to the server automatically. You can also click “Browse Remote Host” to check the files on the CS lab. For setting up the xv6 files, I suggest to copy them from&nbsp;<code>/p/course/cs537-</code>shivaram<code>/xv6-sp19</code>&nbsp;to your private remote folder (e.g.&nbsp;<code>/u/s/z/</code>szhong<code>/private/CS537/p1b</code>), and then click “Download from CS Lab”. (Note that the deployment menu&nbsp;is also available if you right-click a certain folder or file so that you can manually upload/download a single file/folder)</p>
</section>
<section id="some-useful-tips" class="level1">
<h1>Some Useful Tips</h1>
<ul>
<li>Press Control (on Windows) or Command (on Mac) to jump between definition and declaration. (It’s pretty useful when navigating files in xv6)</li>
<li>Press Control + Shift + F to search among all project files</li>
<li>Double press shift to search for all file names/classes/actions…</li>
<li>Press Alt + Shift + L (on Windows) or Command + Shift + L (on Mac) to reformat code, or you can use the plugin called “Save Actions” to perform reformat on save</li>
<li>You can check the editing history under the menu “VSC - Local History - Show History”.</li>
</ul>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/tutorial-setting-up-coding-environment-for-cs-537/</guid>
  <pubDate>Sun, 10 Feb 2019 01:04:11 GMT</pubDate>
</item>
<item>
  <title>Deploy WordPress on CS Lab Server</title>
  <link>https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/</link>
  <description><![CDATA[ 




<p>This tutorial walks through how you can deploy WordPress on a CS Department Server. <img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adfa793d0d41.png" class="img-fluid"></p>
<section id="download-wordpress" class="level1">
<h1>Download WordPress</h1>
<ol type="1">
<li>Connect to CS Linux server</li>
</ol>
<p>If you are using Mac or UNIX system, use the <code>ssh</code> command:</p>
<p><code>ssh cs-username@best-linux.wisc.edu</code></p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5ae8c341ebc3f.png" class="img-fluid"></p>
<p>If you are using Windows, download&nbsp;PuTTY to connect to the server</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5ae8c38f9e1a9.png" class="img-fluid"></p>
<p>Note: Replace <code>cs-username</code> with your username</p>
<p>2. Change directory to&nbsp;~/public/html</p>
<p>Type&nbsp;<code>cd ~/public/html</code> in the terminal, and press enter</p>
<p>Everything in this&nbsp;directory will be public at <a href="http://pages.cs.wisc.edu/~cs-username/" class="uri">http://pages.cs.wisc.edu/~cs-username/</a></p>
<p>3. Download WordPress file and unzip it</p>
<p>Use&nbsp;<code>wget</code> to download the file from wordpress.org</p>
<p><code>wget https://wordpress.org/latest.zip</code></p>
<p>And use <code>unzip</code> to unzip the files</p>
<p><code>unzip latest.zip</code></p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf9258a6485.png" class="img-fluid"></p>
<p>4. Wordpress installed!</p>
<p>Open&nbsp;http://pages.cs.wisc.edu/~cs-username/wordpress/, and you should see the page below</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf8e01dc296.png" class="img-fluid"></p>
</section>
<section id="create-a-database-on-gearhost" class="level1">
<h1>Create a Database on GearHost</h1>
<p>1. Create an account on GearHost</p>
<p><a href="https://my.gearhost.com/account/signup" class="uri">https://my.gearhost.com/account/signup</a></p>
<p>2. Create a database</p>
<p>Open the “Database” tab on the left</p>
<p>Click “Create Database” button</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf8ee744652.png" class="img-fluid"></p>
<p>Choose a database name, and select MySQL (free)</p>
<p>Click “Create Empty Database”</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf90014ca2d.png" class="img-fluid"></p>
<p>Record your database name, database server, username, and password</p>
</section>
<section id="configure-wordpress" class="level1">
<h1>Configure&nbsp;WordPress</h1>
<p>1. Open&nbsp;http://pages.cs.wisc.edu/~cs-username/wordpress/ <img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf8e01dc296.png" class="img-fluid"> 2. Fill in the database information, and click submit</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf90cf2c475.png" class="img-fluid"></p>
<p>3. Copy the content of wp-config.php</p>
<p>Since we don’t have the write permission to the server through webpage, we have to manually create this file</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf913114728.png" class="img-fluid"></p>
<p>4.&nbsp;Go back to CS Linux server, and change directory to&nbsp;~/public/html/wordpress</p>
<p><code>cd ~/public/html/wordpress</code></p>
<p>5. Open text editor, and create the file&nbsp;<code>wp-config.php</code></p>
<p><code>nano wp-config.php</code></p>
<p>Note: You can also use&nbsp;<code>vi</code> or&nbsp;<code>vim</code>&nbsp;instead of&nbsp;<code>nano</code>, but&nbsp;<code>nano</code>&nbsp;is easier to use.</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf92319686a.png" class="img-fluid"></p>
<p>6. Paste the content</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf92a93ecd4.png" class="img-fluid"></p>
<p>7. Control+X and press enter to save the file</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf9307d8915.png" class="img-fluid"></p>
<p>8. Go back to the website and press install</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf937b54ab7-1.png" class="img-fluid"> 9. WordPress installed!!!</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf93c1e24f8.png" class="img-fluid"></p>
</section>
<section id="add-write-permission" class="level1">
<h1>Add Write Permission</h1>
<p>1. Connect to CS Linux server and install “SSH SFTP Updater Support” plugin</p>
<p><code>ssh cs-username@best-linux.wisc.edu</code></p>
<p><code>cd ~/public/html/wordpress/wp-content/plugins/</code></p>
<p><code>wget https://downloads.wordpress.org/plugin/ssh-sftp-updater-support.0.7.3.zip</code></p>
<p><code>unzip&nbsp;ssh-sftp-updater-support.0.7.3.zip</code></p>
<p>2. Enable the plugin on Dashboard</p>
<p>Open&nbsp;http://pages.cs.wisc.edu/~cs-username/wordpress/wp-admin/plugins.php</p>
<p>Click “activate” below “SSH SFTP Updater Support”</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf97356bbf7.png" class="img-fluid"></p>
<p>3. Configure&nbsp;“SSH SFTP Updater Support” Plugin</p>
<p>Open&nbsp;http://pages.cs.wisc.edu/~cs-username/wordpress/wp-admin/theme-install.php?browse=featured</p>
<p>Choose any theme you want, and click the install button</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf99d24b26c.png" class="img-fluid"></p>
<p>You will see a pop window called “Connection Information”</p>
<p>This is because we don’t have permission to write file to the server through webpage</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adf9a6b5e43a.png" class="img-fluid"></p>
<p>Choose SSH2 for connection type, since our CS server do not support FTP</p>
<p>Use best-linux.cs.wisc.edu for hostname, and your&nbsp;CS account for username and password.</p>
<p>Leave everything else blank, and click proceed.</p>
<p>Now you have the permission to write file to the server</p>
</section>
<section id="congratulations" class="level1">
<h1>Congratulations!</h1>
<p>You have deployed WordPress on CS Linux Server!</p>
<p><img src="https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/img_5adfa793d0d41.png" class="img-fluid"></p>
<p># EOF</p>


</section>

 ]]></description>
  <category>Tutorials</category>
  <guid>https://shawnzhong.com/blog/deploy-wordpress-on-pages-cs-wisc-edu/</guid>
  <pubDate>Tue, 24 Apr 2018 15:33:13 GMT</pubDate>
</item>
</channel>
</rss>
