diff --git a/Code Samples/Fib/.vscode/launch.json b/Code Samples/Fib/.vscode/launch.json new file mode 100644 index 000000000..301f8c5f2 --- /dev/null +++ b/Code Samples/Fib/.vscode/launch.json @@ -0,0 +1,40 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "preLaunchTask": "build", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": true, + "cwd": "${workspaceRoot}", + "environment": [], + "externalConsole": true, + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "logging": { + "engineLogging": false, + "trace": false + }, + "windows": { + "program": "${workspaceRoot}/fib.exe", + "MIMode": "gdb", + "miDebuggerPath": "" // Path to gdb on windows + }, + "linux": { + "program": "${workspaceRoot}/fib.out", + "MIMode": "gdb" + }, + "osx": { + "program": "${workspaceRoot}/fib.out", + "MIMode": "lldb" + } + } + ] +} \ No newline at end of file diff --git a/Code Samples/Fib/.vscode/tasks.json b/Code Samples/Fib/.vscode/tasks.json new file mode 100644 index 000000000..30ee79c32 --- /dev/null +++ b/Code Samples/Fib/.vscode/tasks.json @@ -0,0 +1,50 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "taskName": "build", + "type": "shell", + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "echo": true, + "reveal": "always", + "focus": false, + "panel": "shared" + }, + "windows": { + "command": "${workspaceRoot}/build.cmd", + "args": [ + "", // Path to the bin folder containing g++ to compile + "fib.exe" // Output executable name + ] + }, + "linux": { + "command": "g++", + "args": [ + "-g", + "*.cpp", + "-lpthread", + "--std=c++11", + "-o", + "fib.out" + ] + }, + "osx": { + "command": "g++", + "args": [ + "-g", + "*.cpp", + "-lpthread", + "--std=c++11", + "-o", + "fib.out" + ] + } + } + ] +} \ No newline at end of file diff --git a/Code Samples/Fib/README.md b/Code Samples/Fib/README.md new file mode 100644 index 000000000..7e53f5914 --- /dev/null +++ b/Code Samples/Fib/README.md @@ -0,0 +1,3 @@ +# Fib + +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. \ No newline at end of file diff --git a/Code Samples/Fib/build.cmd b/Code Samples/Fib/build.cmd new file mode 100644 index 000000000..821c62747 --- /dev/null +++ b/Code Samples/Fib/build.cmd @@ -0,0 +1,2 @@ +SET PATH=%PATH%;%1 +g++ -g *.cpp -lpthread --std=c++11 -O0 -o %2 \ No newline at end of file diff --git a/Code Samples/Fib/main.cpp b/Code Samples/Fib/main.cpp new file mode 100644 index 000000000..e7ab0c4c9 --- /dev/null +++ b/Code Samples/Fib/main.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "thread.h" + +#define THREAD_COUNT 10 + +static char block[] = "--block"; +int test = 0; + +int main(int argc, char **argv) +{ + srand(time(NULL)); + + static char pidText[] = "PID: "; + std::string helpText = "Attach a debugger and execute 'set foo=0' to continue"; + char helloText[] = "Hello World!"; + + std::cout << helloText << std::endl; + + pthread_t threads[THREAD_COUNT]; + + if (argc == 2 && !strcmp(block, argv[1])) + { + std::cout << helpText << std::endl; + volatile int foo = 1; + while (foo) + ; + } + + if (argc == 2 && !strcmp("--crash", argv[1])) + { + int foo = 0; + int bar = 1 / foo; + } + + for (int i = 0; i < THREAD_COUNT; i++) + { + std::cout << "Test " << i << std::endl; + pthread_create(&threads[i], NULL, &thread_proc, NULL); + } + + for (int i = 0; i < THREAD_COUNT; i++) + { + pthread_join(threads[i], NULL); + test++; + } + + std::cout << "All threads exited!" << std::endl; + + return 1; +} diff --git a/Code Samples/Fib/thread.cpp b/Code Samples/Fib/thread.cpp new file mode 100644 index 000000000..f24717b63 --- /dev/null +++ b/Code Samples/Fib/thread.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "thread.h" + +static int g_tid = 0; + +static int fib(int n){ + switch (n) { + case 0: return 1; + case 1: return 1; + default: return (fib(n-2) + fib(n-1)); + } +} + +void * thread_proc(void* ctx) +{ + int tid = g_tid++; + + char thread_name[16]; + sprintf(thread_name, "Thread %d", tid); +#ifdef __APPLE__ + pthread_setname_np(thread_name); +#else + pthread_setname_np(pthread_self(), thread_name); +#endif + + // Random delay, 0 - 0.5 sec + timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 500000000 + ((float)rand() / (float)RAND_MAX) * 500000000; + nanosleep(&ts, NULL); + + volatile int i = 0; + while (i <= 30) { + std::cout << "Thread " << tid << ": fib(" << i << ") = " << fib(i) << std::endl; + i++; + nanosleep(&ts, NULL); + } + + std::cout << thread_name << " exited!" << std::endl; +} \ No newline at end of file diff --git a/Code Samples/Fib/thread.h b/Code Samples/Fib/thread.h new file mode 100644 index 000000000..6af19e651 --- /dev/null +++ b/Code Samples/Fib/thread.h @@ -0,0 +1 @@ +void * thread_proc(void* ctx);