LoginSignup
2
1

More than 3 years have passed since last update.

AWS SDK for C++のサンプルをVisual Studio Codeでデバッグする

Last updated at Posted at 2019-07-05

C++でAWSのサービスを使う必要が出てきたのですが、幸いにも各言語ごとのサンプルコードが提供されているので、お勉強のために以前作った開発環境でデバッグしてみました。

はじめに

以前の記事と同様ですが

注意 Docker上でgdbを使うためにセキュリティレベルを下げる設定を追加しています。外部に公開される環境では用いないようにしてください。

作業概要

Visual Studio CodeのRemote ContainersでC++開発環境構築 - Qiita で作成したプロジェクトをベースに環境を作っていきます

変更するのは以下2点です。

  1. DockerfileにAWS SDKのビルド設定を追加する
  2. docker-compose.ymlのvolumesを変更する
    • マウントするパスの変更
    • AWSにアクセスするために$HOME/.awsをマウント

プロジェクトをダウンロード

# ベースとなるプロジェクトをクローン
git clone https://github.com/dbgso/vscode-cpp-devcontainer.git vscode-remote-cpp-aws-sdkaws
# サンプルソースをダウンロード
git clone https://github.com/awsdocs/aws-doc-sdk-examples.git vscode-remote-cpp-aws-sdkaws/data/aws-doc-sdk-examples
# Visual Studio Codeで開く
code vscode-remote-cpp-aws-sdkaws

ここから、必要な設定を追加していきます

差分については https://github.com/dbgso/vscode-cpp-devcontainer/commit/c17e6ac33cc5512385ef3b1583d83c796bf10f9e をみてもらっても同じ内容です

Dockerfile修正

変更内容は以下です。
AWS SDK for CPPの依存パッケージをインストールしてビルドしています。

Dockerfile.diff
    apt-get install -y git build-essential cmake clang libssl-dev && \
    apt-get install -y cpputest libsqlite3-dev clang-format gdb

+# AWS dependency
+RUN apt-get install -y libcurl4-openssl-dev libssl-dev uuid-dev zlib1g-dev libpulse-dev
+
+RUN git clone https://github.com/aws/aws-sdk-cpp.git --depth 1 /tmp/aws-sdk-cpp
+RUN cd /tmp/aws-sdk-cpp && cmake -DBUILD_ONLY="s3" && make && make install

参考
Setting Up the AWS SDK for C++ - AWS SDK for C++
DockerとMinioでAWS SDK for C++の開発環境を構築する - Qiita

docker-compose.yml修正

現状のdevcontainer.jsonの設定ではworkingFolder/dataとしているので、/dataにソースのルートを持ってくる必要があります。
ですので、docker-compose.ymlのvolumesでマウントするディレクトリを変更します。
Dockerfileでも-DBUILD_ONLY="s3"としたので今回はs3のサンプルを試していきます。
マウントするパスは/aws-doc-sdk-examples/cpp/example_code/s3/となります。(あるいはdevcontainer.jsonを変更して"workspaceFolder": "/data/aws-doc-sdk-examples/cpp/example_code/s3/"としても良いです。こっちの方が見通しが良いかな?)

version: '3.3'

services:
  cpp:
    build: .
    volumes:
-      - ../data:/data
+      - ../data/aws-doc-sdk-examples/cpp/example_code/s3/:/data
+      - $HOME/.aws/:/root/.aws
    working_dir: /data
    tty: true
    # to use gdb
    cap_add:
      - SYS_PTRACE
    security_opt:
      - seccomp:unconfined

起動

起動方法は前回の記事と変わりません。

reopenincontainer

Reopen in Containerを選択するか、コマンドパレットからRemote-Containers: Reopen Folder in Containerを実行します。

起動するとAWS SDKのビルドが始まりますが、結構長いのでしばらく待機します。(PCのスペックにもよりますが、5分〜15分程度?かかります)

ビルド

コマンドパレットからCMake: buildを実行します。
No CMake kits are availableと聞かれたらDo not use a kitで良さそうです。(前回は選択してましたが、結局CMakeにお任せしていたので意味なかった?です)

こんな感じでログがつらつら流れれば成功です。

[cmake] The C compiler identification is GNU 7.4.0
[cmake] The CXX compiler identification is GNU 7.4.0
[cmake] Check for working C compiler: /usr/bin/cc
[cmake] Check for working C compiler: /usr/bin/cc -- works
[cmake] Detecting C compiler ABI info
[cmake] Detecting C compiler ABI info - done
[cmake] Detecting C compile features
[cmake] Detecting C compile features - done
[cmake] Check for working CXX compiler: /usr/bin/c++
[cmake] Check for working CXX compiler: /usr/bin/c++ -- works
[cmake] Detecting CXX compiler ABI info
[cmake] Detecting CXX compiler ABI info - done
[cmake] Detecting CXX compile features
[cmake] Detecting CXX compile features - done
[cmake] Found AWS SDK for C++, Version: 1.7.138, Install Root:/usr/local, Platform Prefix:, Platform Dependent Libraries: pthread;crypto;ssl;z;curl
[cmake] Components specified for AWSSDK: s3
[cmake] Try finding aws-cpp-sdk-core
[cmake] Found aws-cpp-sdk-core
[cmake] Try finding aws-cpp-sdk-s3
[cmake] Found aws-cpp-sdk-s3
[cmake] Configuring done
[cmake] Generating done
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /data/build --config Debug --target list_buckets -- -j 6
[build] Scanning dependencies of target list_buckets
[build] [ 50%] Building CXX object CMakeFiles/list_buckets.dir/list_buckets.cpp.o
[build] [100%] Linking CXX executable list_buckets
[build] [100%] Built target list_buckets
[build] Build finished with exit code 0

実行とブレークポイント

バケット一覧取得

一番お手軽そうなlist_buckets.cppでバケットのリストを取得してみます。

まず、確認用に適当なバケットを作ってファイルを置いておきます(これもC++でやればいいんですが、記事にするのが面倒臭い)

$ aws s3 mb s3://cppbuckettest
make_bucket: cppbuckettest
$ echo hello > world.txt
$ aws s3 cp world.txt  s3://cppbuckettest
upload: ./world.txt to s3://cppbuckettest/world.txt           
$ aws s3 ls s3://cppbuckettest/
2019-07-04 13:06:47          6 world.txt

ブレークポイントを設定してステップ実行していくと、ちゃんとcppbuckettestの存在を確認できました。

alt

オブジェクトの取得

せっかくファイルをアップロードしたので、バケットのファイル一覧を取得します。
サンプルのソースはlist_objects.cppです。

このファイルはコマンドライン引数にバケット名を渡して、そのバケットのファイル名一覧を取得しているみたいです。
ここで、ちょっとハマりました。
コマンドライン引数はどこに渡すの?って思ったんですが、普通にlaunch.jsonの設定に書けばよかったです。

launch.jsonを開いて、C/C++: (gdb) Launchを選択して、テンプレートを生成します。

以下のように、programに実行ファイル、argsにコマンドライン引数を入れればOKです。

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
-           "program": "enter program name, for example ${workspaceFolder}/a.out",
+           "program": "${workspaceFolder}/build/list_objects",
-            "args": [],
+            "args": ["cppbuckettest"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }

F5キーで実行します

alt

ちゃんとブレークして、該当のバケット情報が取れています。
コンソールにも正しく表示されました

Objects in S3 bucket: cppbuckettest
* world.txt

備考

.awsディレクトリのマウント漏れ

ちなみに、.awsディレクトリをマウントし忘れると以下のように接続できなくて怒られるので注意しましょう

ListBuckets error:  - Unable to connect to endpoint
[1] + Done                       /usr/bin/gdb --interpreter=mi --tty=${DbgTerm} 0</tmp/Microsoft-MIEngine-In-2xmmhqio.v6u 1>/tmp/Microsoft-MIEngine-Out-l2adjy68.7nb

CMake拡張機能を使ってのコマンドライン引数

本文では引数指定ができないと書いたのですが、すみません、できました。
ただ、使い勝手的にはlaunch.jsonに書いてしまうのが良いと思うので、参考までに記録しておきます。

方法はVisual Studio Code側の設定に引数を設定してしまう方法です。

Preferences: Open Workspace SettingsからWorkspaceごとの設定を開き、以下のようにargsに設定します。

setting.json
{    
    "cmake.debugConfig": {
        "args": [
            "bucket_name"
        ]
    }
}

あとは、通常通りDebugボタンを押せば引数付きでデバッグできます

alt

ただ、おそらく設定は一つしかできないです。
Workspaceに設定するのでそれでも良さそうですが、使い勝手は悪そう。
ちなみにシステム側の設定にもできてしまいますが普通しませんよね。
おとなしく、launch.jsonが良いかなと思いました。お手軽実行の時はすごく楽で良いんですが

参考 Passing command line arguments to a debug target · Issue #121 · vector-of-bool/vscode-cmake-tools

参考

Setting Up the AWS SDK for C++ - AWS SDK for C++

DockerとMinioでAWS SDK for C++の開発環境を構築する - Qiita

Passing command line arguments to a debug target · Issue #121 · vector-of-bool/vscode-cmake-tools

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1