A4: GDB and Mario Life
Please read over the entire assignment before starting to get a sense of what you will need to get done in the next week. REMEMBER: Everyone procrastinates but it is important to know what you are procrastinating and still leave yourself enough time to finish.
This assignment must be run on ieng6. We are using custom libraries that are only available on ieng6 for this assignment. Use Cloudlabs or ssh: You will not be able to compile or run the assignment otherwise.
Part 1: GDB Core Dump 汇编课业代做
When dealing with tools such as GDB you are free to search online to find any information that may help you. GDB is an extremely important tool and will save you HOURS of time debugging.
Learning Goals
- Gain practice using the gdb interface and commands
- Understand how useful gdb is
- Use gdb in Part 2 of this assignment and all future work in C and Assembly!
- Reading and understanding code written in C
1.What is GDB? 汇编课业代做
GDB is the GNU debugger. It allows you to see what is going on inside another program while it executes or the moment the program crashes. (https://www.gnu.org/software/gdb/)
Why use GDB? Can’t I just use print statements?
Well, you can and should use print statements in some cases BUT gdb is an extremely useful tool for debugging that can help you determine exactly where errors are occuring. It allows you to execute a program step by step, print intermediate values, and set breakpoints to pause code execution at a specific line.
2.Getting Started 汇编课业代做
For this assignment you will need to run a simple program and determine what is causing a segmentation fault when you run it.
Compile the program:
$ gcc -g -O0 -Wall -Wextra -o <exe file> <src files>
Argument | Description |
gcc | Runs the GNU Compiler Collection (GCC) |
-g | Flag to produce debugging information |
-O0 | Used to reduce compiler optimizations |
-Wall (optional) | This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid |
-Wextra (optional) | This enables some extra warning flags on top of -Wall |
-o <exe file> (optional)
|
Name of the output executable (will be a.out if unspecified) |
<src files> | Name of the source code files (just coredump.c in our case) |
3.How the program works
For this assignment, you will run the program located in coredump.c. This is a small program that, when run, will lead to a segmentation fault. It is your job to use gdb to debug this program and determine what is causing the program to segfault. The starter code can be found here:
https://github.com/CSE30FA21/a4gdb
When you compile and run this program, the output will be:
Segmentation fault (core dumped)
We have provided you with an example of the commands you should run in gdb to find the issue but expect you to spend time playing around with gdb as well.
GDB Commands You Will Use 汇编课业代做
https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gnat_ug_unx/Introduction-to-GDB-Commands.html
Argument | Description |
run | executes the program from the beginning |
where | shows the next statement that will be executed |
up | used to examine the content of frames |
p (or print) | print the value of a given expression |
q (or quit) | used to quit out of gdb |
What You Should Run
$ gdb a.out (gdb) run Starting program: /home/core/a.out Program received signal SIGSEGV, Segmentation fault. strlen () at ../sysdeps/x86_64/strlen.S:106 106 ../sysdeps/x86_64/strlen.S: No such file or directory. (gdb) (gdb) where #0 strlen () at ../sysdeps/x86_64/strlen.S:106 #1 0x00007ffff7a7c69c in _IO_puts (str=0x0) at ioputs.c:35 #2 0x00005555555546ee in printListFwd (curPt=0x555555756010) at coredump.c:26 #3 0x0000555555554786 in main (argc=1, argv=0x7fffffffdc78) at coredump.c:41 (gdb) up #1 0x00007ffff7a7c69c in _IO_puts (str=0x0) at ioputs.c:35 35 ioputs.c: No such file or directory. (gdb) up #2 0x00005555555546ee in printListFwd (curPt=0x555555756010) at coredump.c:26 26 //! This function returns a pointer to the new record which becomes (gdb) print curPt->name … what is printed here?
In the above example, the where command prints which functions have been called to get to the failure point. Typing up moves gdb to the function that called a function. The error occurs in an internal library’s functions (strlen() and _IO_puts), which are called from our program. So we type up twice to get to our code.
4.Submission and Grading 汇编课业代做
Submitting
You will submit a gdb.txt file along with part 2 solution to the “A4: LIFE” assignment on Gradescope. The txt should contain:
- A copy of the terminal output where you ran gdb (it should look similar to what is shown above). Followed by a line of ‘#’ to separate item 2 for example:
Terminal output
############
Answer to item 2
- Provide a short explanation on what is causing the program to have a segmentation fault. What does the final command you run (print curPt->name) print? What does it mean? What does it say about the issue with our program? How can the program be fixed?
Grading Breakdown
- [5 point] Copy of GDB output and explanation of why the segmentation fault is occurring
Part 2: Mario Life 汇编课业代做
Learning Goals
- Dynamic memory allocation and deallocation
- Array processing
- Structs and Pointers
- File I/O
1.Getting Started
We’ve provided you with a Makefile and starter code at this repo. You can either clone the repo or download the zip. (if you are sshing into ieng6, you can git clone in ssh.)
- Clone or download the repo to get the starter code
- If you plan on using the Graphics mode while working remotely:
Windows – install MobaXterm and then ssh into ieng6.
Mac – Install XQuartz from a terminal window, ssh -X into ieng6. 1
Linux – You already have it!! ssh -X into ieng6.
All OS – cloudlabs.ucsd.edu virtual desktop.
1 We have found that the X11 tunneling doesn’t always stay active so you might need to log out and log back in. For example, we have found that life -g will work and then fail to open the display sometime later. If you see this, log out and log back in to reestablish the X11 connection.
Makefile: 汇编课业代做
The Makefile provided will create a life executable from the source files provided. Compile it by typing make into the terminal. By default, it will run with warnings turned on. To compile without warnings, run make no_warnings instead. Run make clean to remove files generated by make. Use make debug to make with debug flags for gdb (make sure to make clean before hand if you already have an existing executable).
Starter Code:
We have provided you with some starter code. There are a few initial config files that you can load into the program. The main function that handles optional/interactive commands is provided. You’ll notice that main calls many extern functions that are not defined in any of your .c files. We’ve included those functions as a library on ieng6 called /home/linux/ieng6/cs30wi21/public/local/x86/lib/libcse30life.a 2
2 Libraries are precompiled routines that may be used by multiple programs. You’ve already used functions from the c-standard library like “printf” and “strcmp”. We made our own library of some cse30 functions for this simulation. The function prototypes are in cse30life.h.
Solution Executable: 汇编课业代做
We have also provided you with life_sol executable. You may run this executable to see what the expected output is. Note: If you run into permission issues with life_sol try running chmod a+x life_sol
gdb:
To run with gdb, the command is gdb [executable] to start gdb with your executable. r [arguments] will run your executable with [arguments]
valgrind:
to run with valgrind, the command is valgrind ./[executable] [arguments] for more details see the testing section.
2.The Game of Life 汇编课业代做
Developed by John Conway, the Game of Life is a mathematical simulation of a simplified evolution process. The simulation occurs on a two-dimensional grid of cells over a series of discrete timesteps. At any given timestep, each cell will either be “alive” or “dead” (encoded as 1 and 0 respectively), and each cell’s value for the next timestep will be computed based on the current values of its 8 direct neighbors. The next-state rules are defined as follows:
For an alive cell:
If 0 or 1 neighbors are alive, it will die in the next timestep from loneliness.
If 2 or 3 neighbors are alive, it will remain alive in the next timestep.
If 4+ neighbors are alive, it will die in the next timestep from overpopulation.
For a dead cell: 汇编课业代做
If exactly 3 neighbors are alive, it will become alive in the next timestep.
Otherwise, it will remain dead in the next timestep.
See this site for an interactive example of the Game of Life. See the Wikipedia page on Example Patterns to see moving gifs of common patterns (toad, glider, etc).
The original life algorithm assumes an infinite plane of cells. However, we are working with limited memory and will simplify the game by working with a finite board and assuming that all the board edges can never sustain life. For example, for a board of 100 x 200 cells, only the inner 98 x 198 active cells could potentially support life. Thus the boards you implement will schematically look like the figure below:
3.How the Program Works
- This program loads an input file, defining the initial board layout of dead/alive cells.
- You are able to interactively step through the program to see how the board evolves through each timestep.
- It can also be run in both ASCII and graphical mode.
- All argument processing is handled by the provided main.c. Please do not edit main.c as we will be testing with our own copy of the file. See arguments andinteractive commands below for details.
Arguments
Format for calling this executable with arguments: ./life filename [-g#]
Arguments | Description |
filename (required) | The name of the input file to initialize the dead/alive cells of the array. See below for details on the format. |
-g# (optional) | -g specifies the graphics option with a # scale factor. If -g is not specified, the game will print ASCII output to the terminal. If -g is specified, the game will show in a pop up window.
Note: Graphics mode requires ssh-ing with ssh -X |
Interactive Commands 汇编课业代做
After running the executable in either mode, interact with it by entering the following commands:
Command | Description |
s N | Simulate N steps, displaying the intermediate results.
Note: use of -g recommended as intermediate steps may display too quickly to be visible in ascii mode. |
n N | Simulate N steps without displaying intermediate results. |
d filename | Dump the current state of the board to filename. The file can then be as input when running the executable. |
q | Quit the program. |
The Input File 汇编课业代做
The input file for generating a board has the following format:
[numRows] [numCols] [row col] ← only lists row-col coordinates of alive cells ... [row col]
Ex: A file that contains the following
4 3 1 1 2 1
produces a 4 row x 3 col game board with the cells at (1, 1) or (row 1, col 1) and (2, 1) or (row 2, col 1) starting out alive. Note: Remember row is the y coordinate and col is the x coordinate and by convention (0,0) is upper left corner.
See starter code config.small, config.big, config.fancy, config.mario, and config.mushroom to see examples of input files.
If you would like to draw your own boards refer to the Draw Your Board section under testing.
Note that all config files will have numRows and numCols with a minimum value of 3. That is you should not consider board sizes smaller than 3 rows by 3 columns. Although we will not define a maximum value, at some point your program will not have enough memory to hold the state of the board. Because of this we will not test with excessively large boards.
The boards_t Struct
You will be using a boards_t struct, defined in boards.h. Read the following carefully:
- belem – Each cell is represented with a custom belem type, which we have defined to be an unsigned char.
- bufferA / bufferB – Pointers to contiguous blocks of memory that we will treat as arrays of belem’s. These buffers represent the board state. You will need to allocate memory for these on the heap using either malloc() or calloc().
Hint: Dynamically allocating memory returns a 1D contiguous block, yet we think of game boards as 2D grids. We can index into a 1D array using 2D row-column notation.
Notice that for any 2-dimensional position on the board given a 2D location as (row, column). you can access it in the 1D array at index = row * numCols + col . (For more details, refer to this link.)
- currentBuffer – A pointer to the buffer representing the “current” board state. It should be a pointer to existing malloc’d memory (Consider bufferA/B).
- nextBuffer – A pointer to the buffer representing the “next” board state. It should just be a pointer to existing malloc’d memory (Consider bufferA/B).
Hint: We want to store a cell’s next value separately from its current value so the cell’s neighbors can be computed correctly. So you will need two game buffers and a way to differentiate between the “current” and “next” board. (Why do you think this is? What would happen if we updated each cell as we went only using one buffer?) After computing the “next” buffer, you only need to swap the currentBuffer and nextBuffer pointers to “advance” to the next generation.
- numRows/numCols/gen – the number of rows, number of columns, and current generation (timestep) respectively [ stored as 32-bit unsigned integer type uint32_t]
4.Functions to Implement [45 pt] 汇编课业代做
Please implement the functions below. We will be testing functions individually. Do not change the function headers.
Part 1 Board.c Functions
clearBoard [5 pts]
void clearBoard(boards_t *self)
clearBoard() takes a pointer to a boards_t struct and zeroes-out both buffers in the struct.
getIndex [3 pts]
int getIndex(boards_t *self, int row, int col)
The 2D game board is represented using a 1D array buffer. getIndex() takes in a particular row and column. It then calculates and returns the index value of that row-col in the 1D array. Remember: index = row * numCols + col . (For more details, refer to link.)
createBoard [10 pts] 汇编课业代做
boards_t * createBoard(char *initFileName)
createBoard() allocates memory for a boards_t struct on the heap (malloc) and initializes the struct elements. It returns a boards_t struct pointer. See boards.h for details on the struct. To initialize the struct, createBoard() must do the following:
- Allocate memory for the boards_t struct on the heap. What size should the struct be?
- Read in the number of columns and number of rows from the input file initFileName and populate the struct values. Hint: use fopen() and fscanf() to open the file. See example over the page.
- Allocate memory for and initialize the struct’s bufferA and . What size should the buffers be?
- Initialize currentBuffer and nextBuffer to bufferA/bufferB and clear them.
- Read in the input file and initialize the appropriate cells in currentBuffer to be alive. [Hint: Use fscanf() see example below]
- Zero out the border cells in currentBuffer to ensure borders are zero (make sure you do this after reading in the input file as an input file might set a border cell).
- Set gen the generation number to 0
- If createBoard fails for any reason it should return a NULL pointer
deleteBoard [5 pts]
void deleteBoard(boards_t **bptr)
deleteBoard() takes a pointer to a pointer to a boards_t struct and deallocates (frees) all the memory associated with the struct. Then it sets the pointer to the boards_t struct to null.
swapBuffers [3 pts]
void swapBuffers(boards_t *self)
swapBuffers() takes a boards_t struct pointer and swaps the current buffer with the next buffer.
Sim.c Functions:
doRow [10 pts] 汇编课业代做
static void doRow(belem *dest, belem *srcStart, belem *srcEnd, uint32_t cols)
Parameters:
– belem *dest – pointer to the first destination cell in the next buffer
– belem *srcStart – pointer to the first cell to process 3 in the current row
– belem *srcEnd – pointer to the last cell to process 3 in the current row
– uint32_t cols – number of columns in the board 4
3 Note: first and last cells to process are the cells after and before the boundary respectively.
4 Number of cols is the number of cols in the board including the boundary cells.
doRow() processes one row of a board. You must process one row of the buffer at a time. At a high level the algorithm is as follows: For each cell in the row, calculate whether that cell is going to be dead or alive in the next time step based on the rules outlined above in Section 2: The Game of Life. Put the value that the cell will be in the next iteration into nextBuffer. A 0 value indicates “dead” and a 1 value indicates “alive”.
Note: doRow can be completed without calling any other functions. We recommend not using any other functions within doRow() since a future assignment will involve translating this C code into assembly. However we will not be enforcing this.
simLoop [5 pts] 汇编课业代做
void simLoop(boards_t *self, uint32_t steps)
Parameters:
– boards_t *self – pointer to the boards_t struct for processing
– uint32_t steps – the number of steps to take
simLoop() performs the simulation for running the game boards through each “step.” It should advance the board through steps number of generations. At each step, it should call doRow() enough times to process all rows in the board and calculate cells of the next generation. (Remember that the edges of the board are always dead!) Then it should call swapBuffer() to swap the current and next buffers and finally increment the generation count in the boards_t struct. Note: You must call doRow on each row in the active region of the board in simLoop or you will fail our tests
Testing 汇编课业代做
Test createBoard:
To test that your loading code works as expected, run your executable with some config file A and dump the board into file B without simulating anything. Then do the same on the same config file A with the solution executable and dump it into file C. The contents of B and C should be identical. We have provided a few initial files for you to test with.
Testing Memory:
Run valgrind to check for memory leaks and memory errors. When running valgrind do not execute your program using -g flag as this will generate memory leaks due to one of the libraries used by main. To check for memory leaks or errors you may run valgrind as follows.
valgrind ./life filename
If you would like more details about the errors or leaks you may also try adding the
–leak-check=full and/or –show-leak-kinds=all flags.
valgrind –leak-check=full –show-leak-kinds=all ./life filename
General Testing: 汇编课业代做
Run your code and solution executable on the same input file, simulate some steps, and dump the output. Make sure that your code’s output and the reference’s output are the same. Note, you may have chosen to dump the board in a different order than we did, in which case you may have to sort the board dump files to diff them. The files provided are not comprehensive, try writing your own testing input. Pay special attention to boundary cases.
Draw Your Board:
Frank (a former cse30 tutor) developed a website where you can draw an initial board state. To try it out visit this link. Simply draw the initial state and copy the coordinates in the right textbox to a file. Just remember that the file starts with a line containing the number of rows followed by a line containing the number of columns. If you would like to share your file with the class, please upload your file to this drive along with a screenshot of the board (either from the browser or from the program). Give both files the same name followed by b to denote the board configuration and s to denote the screenshot. For example “filenameb” and “filenames”.
Hints and Tips 汇编课业代做
- This assignment deals with malloc and frees so there is the potential for memory leaks. It’s highly recommended for you to run valgrind to check for memory leaks.
- We have provided a library for some of the boilerplate code. You can see their function headers in cse30life.h.
- To test, we recommend that you write a small example input file, and step through it to see if it functions as expected.
- Think about test cases on the “edge” of your board.
6.Submission, Style, and Grading
Style Guidelines
No points are given for style, but teaching staff won’t be able to provide assistance or regrades unless code is readable. Please take a look at the following Style Guidelines.
Submitting: [45 points] 汇编课业代做
Make sure to check the autograder output after submitting! Your assignment must compile correctly with the provided Makefile. We will be running additional tests after the deadline passes to determine your final grade. Any assignment that does not compile will receive 0 credit.
- Submit your files to Gradescope under the assignment titled A4: Life. You will submit the following files:
- board.c
- Sim.c
- gdb.txt from part1
To upload multiple files to Gradescope, zip all of the files and upload the zip to the assignment, or you can multi-select all files and drag and drop. Ensure that the files you submit are not in a nested folder..
- After submitting, the autograder will run a few tests:
- Checks that all required files were submitted
- Checks that your source code compiles
- Runs test input on your program and compares your output to ours
- Runs valgrind to check for memory leaks
Grading Breakdown
- clearBoard – 5 pts
- getIndex – 3 pts
- createBoard – 10 pts
- deleteBoard – 5 pts
- swapBuffers – 3 pts
- doRow – 10 pts
- simLoop – 5 pts
- No memory leaks and no memory errors – 4 pts
Part 3: Survey 汇编课业代做
[3 point] Complete this survey about your experiences this term. Double check your PID when entering to ensure you receive credit.
更多代写:cs澳大利亚Online exam代考价格 GRE家考作弊 英国物理学网课代修 北美MBA 论文代写 澳大利亚金融网课代上 论文同行评审代写