このトピックでは、EGL デバッガーにおけるプログラムのデバッグの、基本的なステップについて説明します。
本書では、アプリケーションのデバッグの方針については詳述しませんが、 一般にデバッグ・プロセスにおいては、コード内の問題の原因を特定することが 中心となります。例えば、プログラムが異常終了した場合に、 デバッガーを使用してコードをステップスルーすることで、プログラム内で障害が発生している 場所を特定することができます。予期しない値がプログラムから出力される場合は、 デバッガーを使用して変数の値を追跡することで、予期しない値が出力されている 場所を特定することができます。
デバッガーがデバッグを開始する場所は、常にプログラム・パーツです。 他の論理パーツ (ライブラリーなど) をデバッグしたい場合は、 プログラムから対象の論理パーツにステップインする必要があります。 場合によっては、デバッグしたい論理パーツを呼び出す関数のみを持つ、 シンプルなプログラムを作成すると便利です。
program myDebugTestPgm type BasicProgram
function main()
//Provide some initial values for the array of items.
customerItems items[3];
customerItems[1].itemNumber=1;
customerItems[2].itemNumber=2;
customerItems[3].itemNumber=3;
customerItems[1].itemCost=12.50;
customerItems[2].itemCost=200;
customerItems[3].itemCost=49.95;
customerItems[1].itemQuantity=30;
customerItems[2].itemQuantity=10;
customerItems[3].itemQuantity=60;
counter int;
orderTotal float=0;
//Calculate the total cost of the items.
//Use the discountPrice function to get the discounted cost of each item.
for (counter from 1 to customerItems.getSize() by 1)
orderTotal += discountPrice(customerItems[counter].itemCost,
customerItems[counter].itemQuantity);
end // for loop
//Write the output to the console.
SysLib.writeStderr("The total cost for the order is $" + orderTotal);
end // main
//Return a total price for a group of items
//based on the item price and a quantity discount.
function discountPrice(itemCost float in, itemQuantity int in) returns(float)
discountRate float=0;
quantCost float=0;
//Determine the discount for each quantity.
//Discount 20% for more than 50 items.
//Discount 5% for more than 20 items.
case
when (itemQuantity > 50)
discountRate = 1/5;
when (itemQuantity > 20 && itemQuantity <= 50)
discountRate = 1/20;
otherwise
//bug - division by zero
discountRate = 1/0;
end
//Multiply the cost of the item, the number of items,
//and the discounted price.
quantCost = itemCost*itemQuantity*(1-discountRate);
quantCost = MathLib.round(quantCost, -2);
return (quantCost);
end // function discountPrice
end // program
record items type BasicRecord
itemNumber int;
itemCost float;
itemQuantity int;
end
このプログラムを生成して実行すると、EGL は
、discountPrice 関数と 1/0 式をポイントしたエラーを返します。
今回のケースでは、エラーの特定は簡単ですが、
これほど容易にエラーを検出できないケースもあります。
エラーの原因を特定する場合、最初のステップとして、
デバッガーでブレークポイントを設定してプログラムを実行し、
プログラムで障害が発生する箇所の特定を行います。コード内の 1 つ以上の行に ブレークポイント のマークを付けることができます。デバッガーがブレークポイントに到達すると、関連するコード行を実行する前に一時停止します。ユーザーは、 デバッガーに次の実行を指示する前に、プログラム変数の現行値をチェックすることができます。 ブレークポイントは、デバッグ・プロセスの実行時にのみ使用され、 生成されたソースにはいかなる影響も及ぼしません。
ブレークポイントを追加するには、EGL エディター内で、コードの左側にあるグレーの余白部をダブルクリックします。 前出の例では、エラー・メッセージによって discountPrice 関数でエラーが発生していることが通知されているため、この関数全体にブレークポイントを設定することが必要となります。 ブレークポイントは、このグレーの余白部に青色の円でマークされます。

myString = "Hello";
myInt int = 5;
SysLib.writeStderr("Hello");
if (myInt == 5)
ブレークポイントの詳細な使用方法については 、EGL デバッガーでのブレークポイントの使用を参照してください。
ブレークポイントを使用せずに、プログラムをデバッグすることもできます。 の設定にチェックマークを付けると、プログラムの main() 関数内の最初の実行可能行に、ブレークポイントを設定した場合と同じ意味になります。 このポイントから、後続のコード行をステップスルーまたは迂回 (1 行実行するごとに一時停止) することができます。ステップ・コマンドについての詳細は、EGL デバッガーのコントロールを参照してください。
プログラムに ブレークポイントを追加するか、「先頭行で停止」オプションを設定した後に (このトピック内の『ブレークポイントの追加』(前述) を参照)、プログラムをデバッガーで実行できます。
このサンプル・プログラムでは、 デバッガーが discountRate = 1/0; の行に到達するまで、 ブレークポイントから次のブレークポイントまでプログラムを実行できます。 デバッガーがこの行に到達すると、 プログラムの実行時にコンソールに表示されるものと同じエラーを返します。