Skip to content

Commit fb09db9

Browse files
authored
Add debugger code sample - Fib (#1383)
Add debugger code sample - Fib
1 parent 92d0dec commit fb09db9

File tree

7 files changed

+202
-0
lines changed

7 files changed

+202
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "(gdb) Launch",
6+
"preLaunchTask": "build",
7+
"type": "cppdbg",
8+
"request": "launch",
9+
"args": [],
10+
"stopAtEntry": true,
11+
"cwd": "${workspaceRoot}",
12+
"environment": [],
13+
"externalConsole": true,
14+
"setupCommands": [
15+
{
16+
"description": "Enable pretty-printing for gdb",
17+
"text": "-enable-pretty-printing",
18+
"ignoreFailures": true
19+
}
20+
],
21+
"logging": {
22+
"engineLogging": false,
23+
"trace": false
24+
},
25+
"windows": {
26+
"program": "${workspaceRoot}/fib.exe",
27+
"MIMode": "gdb",
28+
"miDebuggerPath": "<Path/To/GDB>" // Path to gdb on windows
29+
},
30+
"linux": {
31+
"program": "${workspaceRoot}/fib.out",
32+
"MIMode": "gdb"
33+
},
34+
"osx": {
35+
"program": "${workspaceRoot}/fib.out",
36+
"MIMode": "lldb"
37+
}
38+
}
39+
]
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"taskName": "build",
8+
"type": "shell",
9+
"group": {
10+
"kind": "build",
11+
"isDefault": true
12+
},
13+
"presentation": {
14+
"echo": true,
15+
"reveal": "always",
16+
"focus": false,
17+
"panel": "shared"
18+
},
19+
"windows": {
20+
"command": "${workspaceRoot}/build.cmd",
21+
"args": [
22+
"<Path/To/MinGW/Cygwin/Bin/Folder>", // Path to the bin folder containing g++ to compile
23+
"fib.exe" // Output executable name
24+
]
25+
},
26+
"linux": {
27+
"command": "g++",
28+
"args": [
29+
"-g",
30+
"*.cpp",
31+
"-lpthread",
32+
"--std=c++11",
33+
"-o",
34+
"fib.out"
35+
]
36+
},
37+
"osx": {
38+
"command": "g++",
39+
"args": [
40+
"-g",
41+
"*.cpp",
42+
"-lpthread",
43+
"--std=c++11",
44+
"-o",
45+
"fib.out"
46+
]
47+
}
48+
}
49+
]
50+
}

Code Samples/Fib/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Fib
2+
3+
This code sample is to show debugging. Update `launch.json` and `tasks.json` in the `.vscode` folder to use your setup to build and debug.

Code Samples/Fib/build.cmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SET PATH=%PATH%;%1
2+
g++ -g *.cpp -lpthread --std=c++11 -O0 -o %2

Code Samples/Fib/main.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <unistd.h>
3+
#include <sys/types.h>
4+
#include <pthread.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <time.h>
8+
#include <string.h>
9+
10+
#include "thread.h"
11+
12+
#define THREAD_COUNT 10
13+
14+
static char block[] = "--block";
15+
int test = 0;
16+
17+
int main(int argc, char **argv)
18+
{
19+
srand(time(NULL));
20+
21+
static char pidText[] = "PID: ";
22+
std::string helpText = "Attach a debugger and execute 'set foo=0' to continue";
23+
char helloText[] = "Hello World!";
24+
25+
std::cout << helloText << std::endl;
26+
27+
pthread_t threads[THREAD_COUNT];
28+
29+
if (argc == 2 && !strcmp(block, argv[1]))
30+
{
31+
std::cout << helpText << std::endl;
32+
volatile int foo = 1;
33+
while (foo)
34+
;
35+
}
36+
37+
if (argc == 2 && !strcmp("--crash", argv[1]))
38+
{
39+
int foo = 0;
40+
int bar = 1 / foo;
41+
}
42+
43+
for (int i = 0; i < THREAD_COUNT; i++)
44+
{
45+
std::cout << "Test " << i << std::endl;
46+
pthread_create(&threads[i], NULL, &thread_proc, NULL);
47+
}
48+
49+
for (int i = 0; i < THREAD_COUNT; i++)
50+
{
51+
pthread_join(threads[i], NULL);
52+
test++;
53+
}
54+
55+
std::cout << "All threads exited!" << std::endl;
56+
57+
return 1;
58+
}

Code Samples/Fib/thread.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
#include <unistd.h>
3+
#include <sys/types.h>
4+
#include <pthread.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <time.h>
8+
#include <string.h>
9+
10+
#include "thread.h"
11+
12+
static int g_tid = 0;
13+
14+
static int fib(int n){
15+
switch (n) {
16+
case 0: return 1;
17+
case 1: return 1;
18+
default: return (fib(n-2) + fib(n-1));
19+
}
20+
}
21+
22+
void * thread_proc(void* ctx)
23+
{
24+
int tid = g_tid++;
25+
26+
char thread_name[16];
27+
sprintf(thread_name, "Thread %d", tid);
28+
#ifdef __APPLE__
29+
pthread_setname_np(thread_name);
30+
#else
31+
pthread_setname_np(pthread_self(), thread_name);
32+
#endif
33+
34+
// Random delay, 0 - 0.5 sec
35+
timespec ts;
36+
ts.tv_sec = 0;
37+
ts.tv_nsec = 500000000 + ((float)rand() / (float)RAND_MAX) * 500000000;
38+
nanosleep(&ts, NULL);
39+
40+
volatile int i = 0;
41+
while (i <= 30) {
42+
std::cout << "Thread " << tid << ": fib(" << i << ") = " << fib(i) << std::endl;
43+
i++;
44+
nanosleep(&ts, NULL);
45+
}
46+
47+
std::cout << thread_name << " exited!" << std::endl;
48+
}

Code Samples/Fib/thread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void * thread_proc(void* ctx);

0 commit comments

Comments
 (0)